| 1 | package ezvcard.property; |
| 2 | |
| 3 | import java.util.LinkedHashMap; |
| 4 | import java.util.List; |
| 5 | import java.util.Map; |
| 6 | |
| 7 | import ezvcard.SupportedVersions; |
| 8 | import ezvcard.VCard; |
| 9 | import ezvcard.VCardVersion; |
| 10 | import ezvcard.ValidationWarning; |
| 11 | import ezvcard.parameter.Pid; |
| 12 | import ezvcard.parameter.RelatedType; |
| 13 | import ezvcard.util.TelUri; |
| 14 | |
| 15 | /* |
| 16 | Copyright (c) 2012-2023, Michael Angstadt |
| 17 | All rights reserved. |
| 18 | |
| 19 | Redistribution and use in source and binary forms, with or without |
| 20 | modification, are permitted provided that the following conditions are met: |
| 21 | |
| 22 | 1. Redistributions of source code must retain the above copyright notice, this |
| 23 | list of conditions and the following disclaimer. |
| 24 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 25 | this list of conditions and the following disclaimer in the documentation |
| 26 | and/or other materials provided with the distribution. |
| 27 | |
| 28 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 29 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 30 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 31 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 32 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 33 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 34 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 35 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 37 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | |
| 39 | The views and conclusions contained in the software and documentation are those |
| 40 | of the authors and should not be interpreted as representing official policies, |
| 41 | either expressed or implied, of the FreeBSD Project. |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * <p> |
| 46 | * Defines someone that the person is related to. |
| 47 | * </p> |
| 48 | * |
| 49 | * <p> |
| 50 | * <b>Code sample</b> |
| 51 | * </p> |
| 52 | * |
| 53 | * <pre class="brush:java"> |
| 54 | * VCard vcard = new VCard(); |
| 55 | * |
| 56 | * //static factory methods |
| 57 | * Related related = Related.email("[email protected]"); |
| 58 | * related.getTypes().add(RelatedType.CO_WORKER); |
| 59 | * related.getTypes().add(RelatedType.FRIEND); |
| 60 | * vcard.addRelated(related); |
| 61 | * |
| 62 | * //free-form text |
| 63 | * related = new Related(); |
| 64 | * related.setText("Edna Smith"); |
| 65 | * related.getTypes().add(RelatedType.SPOUSE); |
| 66 | * vcard.addRelated(related); |
| 67 | * |
| 68 | * //reference another vCard by putting its UID property here |
| 69 | * related = new Related("urn:uuid:03a0e51f-d1aa-4385-8a53-e29025acd8af"); |
| 70 | * related.getTypes().add(RelatedType.SIBLING); |
| 71 | * vcard.addRelated(related); |
| 72 | * </pre> |
| 73 | * |
| 74 | * <p> |
| 75 | * <b>Property name:</b> {@code RELATED} |
| 76 | * </p> |
| 77 | * <p> |
| 78 | * <b>Supported versions:</b> {@code 4.0} |
| 79 | * </p> |
| 80 | * @author Michael Angstadt |
| 81 | * @see <a href="http://tools.ietf.org/html/rfc6350#page-42">RFC 6350 p.42</a> |
| 82 | */ |
| 83 | @SupportedVersions(VCardVersion.V4_0) |
| 84 | public class Related extends VCardProperty implements HasAltId { |
| 85 | private String uri; |
| 86 | private String text; |
| 87 | |
| 88 | /** |
| 89 | * Creates a related property |
| 90 | */ |
| 91 | public Related() { |
| 92 | //empty |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a related property. |
| 97 | * @param uri the URI representing the person |
| 98 | */ |
| 99 | public Related(String uri) { |
| 100 | setUri(uri); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Copy constructor. |
| 105 | * @param original the property to make a copy of |
| 106 | */ |
| 107 | public Related(Related original) { |
| 108 | super(original); |
| 109 | uri = original.uri; |
| 110 | text = original.text; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates a related property whose value is an email address. |
| 115 | * @param email the email address |
| 116 | * @return the property |
| 117 | */ |
| 118 | public static Related email(String email) { |
| 119 | return new Related("mailto:" + email); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Creates a related property whose value is an instant messenger handle. |
| 124 | * @param protocol the instant messenger protocol (e.g. "aim") |
| 125 | * @param handle the instant messenger handle (e.g. "johndoe") |
| 126 | * @return the property |
| 127 | */ |
| 128 | public static Related im(String protocol, String handle) { |
| 129 | return new Related(protocol + ":" + handle); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Creates a related property whose value is a telephone number. |
| 134 | * @param telUri the telephone number |
| 135 | * @return the property |
| 136 | */ |
| 137 | public static Related telephone(TelUri telUri) { |
| 138 | return new Related(telUri.toString()); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Gets the URI value. |
| 143 | * @return the URI value or null if no URI value is set |
| 144 | */ |
| 145 | public String getUri() { |
| 146 | return uri; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sets the URI. |
| 151 | * @param uri the URI |
| 152 | */ |
| 153 | public void setUri(String uri) { |
| 154 | this.uri = uri; |
| 155 | text = null; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Gets the text value. |
| 160 | * @return the text value or null if no text value is set |
| 161 | */ |
| 162 | public String getText() { |
| 163 | return text; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Sets the value to free-form text instead of a URI. |
| 168 | * @param text the text |
| 169 | */ |
| 170 | public void setText(String text) { |
| 171 | this.text = text; |
| 172 | uri = null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Gets the list that stores this property's relationship types (TYPE |
| 177 | * parameters). |
| 178 | * @return the relationship types (e.g. "child", "co-worker") (this list is |
| 179 | * mutable) |
| 180 | */ |
| 181 | public List<RelatedType> getTypes() { |
| 182 | return parameters.new TypeParameterList<RelatedType>() { |
| 183 | @Override |
| 184 | protected RelatedType _asObject(String value) { |
| 185 | return RelatedType.get(value); |
| 186 | } |
| 187 | }; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public List<Pid> getPids() { |
| 192 | return super.getPids(); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Integer getPref() { |
| 197 | return super.getPref(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void setPref(Integer pref) { |
| 202 | super.setPref(pref); |
| 203 | } |
| 204 | |
| 205 | //@Override |
| 206 | public String getAltId() { |
| 207 | return parameters.getAltId(); |
| 208 | } |
| 209 | |
| 210 | //@Override |
| 211 | public void setAltId(String altId) { |
| 212 | parameters.setAltId(altId); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 217 | if (uri == null && text == null) { |
| 218 | warnings.add(new ValidationWarning(8)); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | protected Map<String, Object> toStringValues() { |
| 224 | Map<String, Object> values = new LinkedHashMap<>(); |
| 225 | values.put("uri", uri); |
| 226 | values.put("text", text); |
| 227 | return values; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Related copy() { |
| 232 | return new Related(this); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public int hashCode() { |
| 237 | final int prime = 31; |
| 238 | int result = super.hashCode(); |
| 239 | result = prime * result + ((text == null) ? 0 : text.hashCode()); |
| 240 | result = prime * result + ((uri == null) ? 0 : uri.hashCode()); |
| 241 | return result; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public boolean equals(Object obj) { |
| 246 | if (this == obj) return true; |
| 247 | if (!super.equals(obj)) return false; |
| 248 | Related other = (Related) obj; |
| 249 | if (text == null) { |
| 250 | if (other.text != null) return false; |
| 251 | } else if (!text.equals(other.text)) return false; |
| 252 | if (uri == null) { |
| 253 | if (other.uri != null) return false; |
| 254 | } else if (!uri.equals(other.uri)) return false; |
| 255 | return true; |
| 256 | } |
| 257 | } |