001 package ezvcard;
002
003 import java.util.Arrays;
004 import java.util.Collection;
005 import java.util.Collections;
006 import java.util.EnumSet;
007 import java.util.Set;
008
009 import ezvcard.util.CaseClasses;
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 data type of a property's value.
042 * @author Michael Angstadt
043 */
044 public class VCardDataType {
045 private static final CaseClasses<VCardDataType, String> enums = new CaseClasses<VCardDataType, String>(VCardDataType.class) {
046 @Override
047 protected VCardDataType create(String value) {
048 return new VCardDataType(value);
049 }
050
051 @Override
052 protected boolean matches(VCardDataType dataType, String value) {
053 return dataType.name.equalsIgnoreCase(value);
054 }
055 };
056
057 /**
058 * <b>Supported versions:</b> {@code 2.1 (p.18-9)}
059 */
060 public static final VCardDataType URL = new VCardDataType("url", VCardVersion.V2_1);
061
062 /**
063 * <b>Supported versions:</b> {@code 2.1 (p.8-9)}
064 */
065 public static final VCardDataType CONTENT_ID = new VCardDataType("content-id", VCardVersion.V2_1);
066
067 /**
068 * <b>Supported versions:</b> {@code 3.0}
069 */
070 public static final VCardDataType BINARY = new VCardDataType("binary", VCardVersion.V3_0);
071
072 /**
073 * <b>Supported versions:</b> {@code 3.0, 4.0}
074 */
075 public static final VCardDataType URI = new VCardDataType("uri", VCardVersion.V3_0, VCardVersion.V4_0);
076
077 /**
078 * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0}
079 */
080 public static final VCardDataType TEXT = new VCardDataType("text");
081
082 /**
083 * <b>Supported versions:</b> {@code 3.0, 4.0}
084 */
085 public static final VCardDataType DATE = new VCardDataType("date", VCardVersion.V3_0, VCardVersion.V4_0);
086
087 /**
088 * <b>Supported versions:</b> {@code 3.0, 4.0}
089 */
090 public static final VCardDataType TIME = new VCardDataType("time", VCardVersion.V3_0, VCardVersion.V4_0);
091
092 /**
093 * <b>Supported versions:</b> {@code 3.0, 4.0}
094 */
095 public static final VCardDataType DATE_TIME = new VCardDataType("date-time", VCardVersion.V3_0, VCardVersion.V4_0);
096
097 /**
098 * <b>Supported versions:</b> {@code 4.0}
099 */
100 public static final VCardDataType DATE_AND_OR_TIME = new VCardDataType("date-and-or-time", VCardVersion.V4_0);
101
102 /**
103 * <b>Supported versions:</b> {@code 4.0}
104 */
105 public static final VCardDataType TIMESTAMP = new VCardDataType("timestamp", VCardVersion.V4_0);
106
107 /**
108 * <b>Supported versions:</b> {@code 4.0}
109 */
110 public static final VCardDataType BOOLEAN = new VCardDataType("boolean", VCardVersion.V4_0);
111
112 /**
113 * <b>Supported versions:</b> {@code 4.0}
114 */
115 public static final VCardDataType INTEGER = new VCardDataType("integer", VCardVersion.V4_0);
116
117 /**
118 * <b>Supported versions:</b> {@code 4.0}
119 */
120 public static final VCardDataType FLOAT = new VCardDataType("float", VCardVersion.V4_0);
121
122 /**
123 * <b>Supported versions:</b> {@code 4.0}
124 */
125 public static final VCardDataType UTC_OFFSET = new VCardDataType("utc-offset", VCardVersion.V4_0);
126
127 /**
128 * <b>Supported versions:</b> {@code 4.0}
129 */
130 public static final VCardDataType LANGUAGE_TAG = new VCardDataType("language-tag", VCardVersion.V4_0);
131
132 private final String name;
133 private final Set<VCardVersion> supportedVersions;
134
135 private VCardDataType(String name, VCardVersion... supportedVersions) {
136 this.name = name;
137 if (supportedVersions.length == 0) {
138 supportedVersions = VCardVersion.values();
139 }
140
141 Set<VCardVersion> set = EnumSet.copyOf(Arrays.asList(supportedVersions));
142 this.supportedVersions = Collections.unmodifiableSet(set);
143 }
144
145 /**
146 * Gets the name of the data type.
147 * @return the name of the data type (e.g. "text")
148 */
149 public String getName() {
150 return name;
151 }
152
153 /**
154 * Determines if the data type is supported by the given vCard version.
155 * @param version the vCard version
156 * @return true if it is supported, false if not
157 */
158 public boolean isSupported(VCardVersion version) {
159 return supportedVersions.contains(version);
160 }
161
162 @Override
163 public String toString() {
164 return name;
165 }
166
167 /**
168 * Searches for a data type that is defined as a static constant in this
169 * class.
170 * @param dataType the data type name (e.g. "text")
171 * @return the data type or null if not found
172 */
173 public static VCardDataType find(String dataType) {
174 return enums.find(dataType);
175 }
176
177 /**
178 * Searches for a data type and creates one if it cannot be found. All
179 * objects are guaranteed to be unique, so they can be compared with
180 * {@code ==} equality.
181 * @param dataType data type name (e.g. "text")
182 * @return the data type
183 */
184 public static VCardDataType get(String dataType) {
185 return enums.get(dataType);
186 }
187
188 /**
189 * Gets all of the data types that are defined as static constants in this
190 * class.
191 * @return the data types
192 */
193 public static Collection<VCardDataType> all() {
194 return enums.all();
195 }
196 }