| 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 | |
| 12 | /* |
| 13 | Copyright (c) 2012-2023, Michael Angstadt |
| 14 | All rights reserved. |
| 15 | |
| 16 | Redistribution and use in source and binary forms, with or without |
| 17 | modification, are permitted provided that the following conditions are met: |
| 18 | |
| 19 | 1. Redistributions of source code must retain the above copyright notice, this |
| 20 | list of conditions and the following disclaimer. |
| 21 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 22 | this list of conditions and the following disclaimer in the documentation |
| 23 | and/or other materials provided with the distribution. |
| 24 | |
| 25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 26 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 27 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 29 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 32 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | |
| 36 | The views and conclusions contained in the software and documentation are those |
| 37 | of the authors and should not be interpreted as representing official policies, |
| 38 | either expressed or implied, of the FreeBSD Project. |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * <p> |
| 43 | * Defines the person's sex. |
| 44 | * </p> |
| 45 | * |
| 46 | * <p> |
| 47 | * <b>Code sample (creating)</b> |
| 48 | * </p> |
| 49 | * |
| 50 | * <pre class="brush:java"> |
| 51 | * VCard vcard = new VCard(); |
| 52 | * |
| 53 | * Gender gender = Gender.male(); |
| 54 | * vcard.setGender(gender); |
| 55 | * </pre> |
| 56 | * |
| 57 | * <p> |
| 58 | * <b>Code sample (retrieving)</b> |
| 59 | * </p> |
| 60 | * |
| 61 | * <pre class="brush:java"> |
| 62 | * VCard vcard = ... |
| 63 | * Gender gender = vcard.getGender(); |
| 64 | * if (gender.isMale()) { |
| 65 | * //gender is male |
| 66 | * } else if (gender.isFemale()) { |
| 67 | * //gender is female |
| 68 | * } |
| 69 | * //etc |
| 70 | * </pre> |
| 71 | * |
| 72 | * <p> |
| 73 | * <b>Property name:</b> {@code GENDER} |
| 74 | * </p> |
| 75 | * <p> |
| 76 | * <b>Supported versions:</b> {@code 4.0} |
| 77 | * </p> |
| 78 | * @author Michael Angstadt |
| 79 | * @see <a href="http://tools.ietf.org/html/rfc6350#page-32">RFC 6350 p.32</a> |
| 80 | */ |
| 81 | @SupportedVersions(VCardVersion.V4_0) |
| 82 | public class Gender extends VCardProperty { |
| 83 | public static final String MALE = "M"; |
| 84 | public static final String FEMALE = "F"; |
| 85 | public static final String OTHER = "O"; |
| 86 | public static final String NONE = "N"; |
| 87 | public static final String UNKNOWN = "U"; |
| 88 | |
| 89 | private String gender; |
| 90 | private String text; |
| 91 | |
| 92 | /** |
| 93 | * Creates a gender property. Use of this constructor is discouraged. Please |
| 94 | * use one of the static factory methods to create a new GENDER property. |
| 95 | * @param gender the gender value (e.g. "F") |
| 96 | */ |
| 97 | public Gender(String gender) { |
| 98 | this.gender = gender; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Copy constructor. |
| 103 | * @param original the property to make a copy of |
| 104 | */ |
| 105 | public Gender(Gender original) { |
| 106 | super(original); |
| 107 | gender = original.gender; |
| 108 | text = original.text; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Gets the additional text associated with this property. |
| 113 | * @return the additional text or null if there is no text |
| 114 | */ |
| 115 | public String getText() { |
| 116 | return text; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets the additional text associated with this property. |
| 121 | * @param text additional text or null to remove |
| 122 | */ |
| 123 | public void setText(String text) { |
| 124 | this.text = text; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets the gender value. |
| 129 | * @return the gender value (see static strings for the possible values) |
| 130 | */ |
| 131 | public String getGender() { |
| 132 | return gender; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Sets the gender value. |
| 137 | * @param gender the gender value (see static strings for the possible |
| 138 | * values) |
| 139 | */ |
| 140 | public void setGender(String gender) { |
| 141 | this.gender = gender; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Determines if the gender is "male" or not. |
| 146 | * @return true if the gender is "male", false if not |
| 147 | */ |
| 148 | public boolean isMale() { |
| 149 | return MALE.equals(gender); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Determines if the gender is "female" or not. |
| 154 | * @return true if the gender is "female", false if not |
| 155 | */ |
| 156 | public boolean isFemale() { |
| 157 | return FEMALE.equals(gender); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Determines if the gender is "other" or not. |
| 162 | * @return true if the gender is "other", false if not |
| 163 | */ |
| 164 | public boolean isOther() { |
| 165 | return OTHER.equals(gender); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Determines if the gender is "none" or not. A group, organization, or |
| 170 | * location may have this gender property. |
| 171 | * @return true if the gender is "none", false if not |
| 172 | */ |
| 173 | public boolean isNone() { |
| 174 | return NONE.equals(gender); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Determines if the gender is "unknown" or not. |
| 179 | * @return true if the gender is "unknown", false if not |
| 180 | */ |
| 181 | public boolean isUnknown() { |
| 182 | return UNKNOWN.equals(gender); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Creates a gender property whose value is set to "male". |
| 187 | * @return a "male" gender property |
| 188 | */ |
| 189 | public static Gender male() { |
| 190 | return new Gender(MALE); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Creates a gender property whose value is set to "female". |
| 195 | * @return a "female" gender property |
| 196 | */ |
| 197 | public static Gender female() { |
| 198 | return new Gender(FEMALE); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Creates a gender property whose value is set to "other". |
| 203 | * @return an "other" gender property |
| 204 | */ |
| 205 | public static Gender other() { |
| 206 | return new Gender(OTHER); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Creates a gender property whose value is set to "none". Groups, |
| 211 | * organizations, and locations should be given this gender property. |
| 212 | * @return a "none" gender property |
| 213 | */ |
| 214 | public static Gender none() { |
| 215 | return new Gender(NONE); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Creates a gender property whose value is set to "unknown". |
| 220 | * @return a "unknown" gender property |
| 221 | */ |
| 222 | public static Gender unknown() { |
| 223 | return new Gender(UNKNOWN); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 228 | if (gender == null) { |
| 229 | warnings.add(new ValidationWarning(8)); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | protected Map<String, Object> toStringValues() { |
| 235 | Map<String, Object> values = new LinkedHashMap<>(); |
| 236 | values.put("gender", gender); |
| 237 | values.put("text", text); |
| 238 | return values; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public Gender copy() { |
| 243 | return new Gender(this); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public int hashCode() { |
| 248 | final int prime = 31; |
| 249 | int result = super.hashCode(); |
| 250 | result = prime * result + ((gender == null) ? 0 : gender.hashCode()); |
| 251 | result = prime * result + ((text == null) ? 0 : text.hashCode()); |
| 252 | return result; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public boolean equals(Object obj) { |
| 257 | if (this == obj) return true; |
| 258 | if (!super.equals(obj)) return false; |
| 259 | Gender other = (Gender) obj; |
| 260 | if (gender == null) { |
| 261 | if (other.gender != null) return false; |
| 262 | } else if (!gender.equals(other.gender)) return false; |
| 263 | if (text == null) { |
| 264 | if (other.text != null) return false; |
| 265 | } else if (!text.equals(other.text)) return false; |
| 266 | return true; |
| 267 | } |
| 268 | } |