001 package ezvcard.property;
002
003 import java.util.EnumSet;
004 import java.util.List;
005 import java.util.Set;
006
007 import ezvcard.VCard;
008 import ezvcard.VCardVersion;
009 import ezvcard.Warning;
010
011 /*
012 Copyright (c) 2013, Michael Angstadt
013 All rights reserved.
014
015 Redistribution and use in source and binary forms, with or without
016 modification, are permitted provided that the following conditions are met:
017
018 1. Redistributions of source code must retain the above copyright notice, this
019 list of conditions and the following disclaimer.
020 2. Redistributions in binary form must reproduce the above copyright notice,
021 this list of conditions and the following disclaimer in the documentation
022 and/or other materials provided with the distribution.
023
024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034
035 The views and conclusions contained in the software and documentation are those
036 of the authors and should not be interpreted as representing official policies,
037 either expressed or implied, of the FreeBSD Project.
038 */
039
040 /**
041 * Defines the person's sex.
042 *
043 * <p>
044 * <b>Setting the gender</b>
045 * </p>
046 *
047 * <pre class="brush:java">
048 * VCard vcard = new VCard();
049 * Gender gender = Gender.male();
050 * vcard.setGender(gender);
051 * </pre>
052 *
053 * <p>
054 * <b>Getting the gender</b>
055 * </p>
056 *
057 * <pre class="brush:java">
058 * VCard vcard = ...
059 * Gender gender = vcard.getGender();
060 * if (gender != null){
061 * if (gender.isMale()){
062 * ...
063 * } else if (gender.isFemale()){
064 * ...
065 * }
066 * ...
067 * }
068 * </pre>
069 *
070 * <p>
071 * <b>Property name:</b> {@code GENDER}
072 * </p>
073 * <p>
074 * <b>Supported versions:</b> {@code 4.0}
075 * </p>
076 * @author Michael Angstadt
077 */
078 public class Gender extends VCardProperty {
079 public static final String MALE = "M";
080 public static final String FEMALE = "F";
081 public static final String OTHER = "O";
082 public static final String NONE = "N";
083 public static final String UNKNOWN = "U";
084
085 private String gender;
086 private String text;
087
088 /**
089 * Creates a gender property. Use of this constructor is discouraged. Please
090 * use one of the static factory methods to create a new GENDER property.
091 * @param gender the gender value (e.g. "F")
092 */
093 public Gender(String gender) {
094 this.gender = gender;
095 }
096
097 @Override
098 public Set<VCardVersion> _supportedVersions() {
099 return EnumSet.of(VCardVersion.V4_0);
100 }
101
102 /**
103 * Gets the additional text associated with this property.
104 * @return the additional text or null if there is no text
105 */
106 public String getText() {
107 return text;
108 }
109
110 /**
111 * Sets the additional text associated with this property.
112 * @param text additional text or null to remove
113 */
114 public void setText(String text) {
115 this.text = text;
116 }
117
118 /**
119 * Gets the gender value.
120 * @return the gender value (see static strings for the possible values)
121 */
122 public String getGender() {
123 return gender;
124 }
125
126 /**
127 * Sets the gender value.
128 * @param gender the gender value (see static strings for the possible
129 * values)
130 */
131 public void setGender(String gender) {
132 this.gender = gender;
133 }
134
135 /**
136 * Determines if the gender is "male" or not.
137 * @return true if the gender is "male", false if not
138 */
139 public boolean isMale() {
140 return MALE.equals(gender);
141 }
142
143 /**
144 * Determines if the gender is "female" or not.
145 * @return true if the gender is "female", false if not
146 */
147 public boolean isFemale() {
148 return FEMALE.equals(gender);
149 }
150
151 /**
152 * Determines if the gender is "other" or not.
153 * @return true if the gender is "other", false if not
154 */
155 public boolean isOther() {
156 return OTHER.equals(gender);
157 }
158
159 /**
160 * Determines if the gender is "none" or not. A group, organization, or
161 * location may have this gender property.
162 * @return true if the gender is "none", false if not
163 */
164 public boolean isNone() {
165 return NONE.equals(gender);
166 }
167
168 /**
169 * Determines if the gender is "unknown" or not.
170 * @return true if the gender is "unknown", false if not
171 */
172 public boolean isUnknown() {
173 return UNKNOWN.equals(gender);
174 }
175
176 /**
177 * Creates a gender property whose value is set to "male".
178 * @return a "male" gender property
179 */
180 public static Gender male() {
181 return new Gender(MALE);
182 }
183
184 /**
185 * Creates a gender property whose value is set to "female".
186 * @return a "female" gender property
187 */
188 public static Gender female() {
189 return new Gender(FEMALE);
190 }
191
192 /**
193 * Creates a gender property whose value is set to "other".
194 * @return an "other" gender property
195 */
196 public static Gender other() {
197 return new Gender(OTHER);
198 }
199
200 /**
201 * Creates a gender property whose value is set to "none". Groups,
202 * organizations, and locations should be given this gender property.
203 * @return a "none" gender property
204 */
205 public static Gender none() {
206 return new Gender(NONE);
207 }
208
209 /**
210 * Creates a gender property whose value is set to "unknown".
211 * @return a "unknown" gender property
212 */
213 public static Gender unknown() {
214 return new Gender(UNKNOWN);
215 }
216
217 @Override
218 protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) {
219 if (gender == null) {
220 warnings.add(new Warning(8));
221 }
222 }
223 }