Debian Codegraph @

src/main/java/ezvcard/parameter/VCardParameter.java

raw symbols SCIP
1 package ezvcard.parameter;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Modifier;
5
6 import ezvcard.SupportedVersions;
7 import ezvcard.VCardVersion;
8
9 /*
10 Copyright (c) 2012-2023, Michael Angstadt
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15
16 1. Redistributions of source code must retain the above copyright notice, this
17 list of conditions and the following disclaimer.
18 2. Redistributions in binary form must reproduce the above copyright notice,
19 this list of conditions and the following disclaimer in the documentation
20 and/or other materials provided with the distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 The views and conclusions contained in the software and documentation are those
34 of the authors and should not be interpreted as representing official policies,
35 either expressed or implied, of the FreeBSD Project.
36 */
37
38 /**
39 * Represents a vCard parameter (aka "sub type") whose values are pre-defined.
40 * @author Michael Angstadt
41 */
42 public class VCardParameter {
43 /**
44 * The value (for example, "home").
45 */
46 protected final String value;
47
48 /**
49 * Creates a new parameter.
50 * @param value the value
51 */
52 public VCardParameter(String value) {
53 this(value, false);
54 }
55
56 /**
57 * Creates a new parameter.
58 * @param value the value
59 * @param preserveCase true to preserve the case of the value, false convert
60 * it to lower-case
61 */
62 protected VCardParameter(String value, boolean preserveCase) {
63 this.value = (value == null || preserveCase) ? value : value.toLowerCase();
64 }
65
66 /**
67 * Gets the value of the parameter.
68 * @return the value of the parameter (e.g. "home")
69 */
70 public String getValue() {
71 return value;
72 }
73
74 /**
75 * <p>
76 * Gets the vCard versions that support this parameter value.
77 * </p>
78 * <p>
79 * The supported versions are defined by assigning a
80 * {@link SupportedVersions} annotation to the parameter value's static
81 * field (for example, {@link AddressType#DOM}). Dynamically-created
82 * parameter values (i.e. non-standard values) are considered to be
83 * supported by all versions.
84 * </p>
85 * @return the vCard versions that support this parameter.
86 */
87 public VCardVersion[] getSupportedVersions() {
88 for (Field field : getClass().getFields()) {
89 if (!Modifier.isStatic(field.getModifiers())) {
90 continue;
91 }
92
93 Object fieldValue;
94 try {
95 fieldValue = field.get(null);
96 } catch (IllegalArgumentException e) {
97 //should never be thrown because we check for the static modified
98 continue;
99 } catch (IllegalAccessException e) {
100 continue;
101 }
102
103 if (fieldValue == this) {
104 SupportedVersions supportedVersionsAnnotation = field.getAnnotation(SupportedVersions.class);
105 return (supportedVersionsAnnotation == null) ? VCardVersion.values() : supportedVersionsAnnotation.value();
106 }
107 }
108
109 return VCardVersion.values();
110 }
111
112 /**
113 * <p>
114 * Determines if this parameter value is supported by the given vCard
115 * version.
116 * </p>
117 * <p>
118 * The supported versions are defined by assigning a
119 * {@link SupportedVersions} annotation to the parameter value's static
120 * field (for example, {@link AddressType#DOM}). Dynamically-created
121 * parameter values (i.e. non-standard values) are considered to be
122 * supported by all versions.
123 * </p>
124 * @param version the vCard version
125 * @return true if it is supported, false if not
126 */
127 public boolean isSupportedBy(VCardVersion version) {
128 for (VCardVersion supportedVersion : getSupportedVersions()) {
129 if (supportedVersion == version) {
130 return true;
131 }
132 }
133 return false;
134 }
135
136 @Override
137 public String toString() {
138 return value;
139 }
140
141 @Override
142 public int hashCode() {
143 final int prime = 31;
144 int result = 1;
145 result = prime * result + ((value == null) ? 0 : value.hashCode());
146 return result;
147 }
148
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj) return true;
152 if (obj == null) return false;
153 if (getClass() != obj.getClass()) return false;
154 VCardParameter other = (VCardParameter) obj;
155 if (value == null) {
156 if (other.value != null) return false;
157 } else if (!value.equals(other.value)) return false;
158 return true;
159 }
160 }