Debian Codegraph @

src/main/java/ezvcard/io/json/JCardValue.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import ezvcard.property.Categories;
9 import ezvcard.property.Note;
10 import ezvcard.property.StructuredName;
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 * Holds the data type and value of a jCard property.
43 * @author Michael Angstadt
44 */
45 public class JCardValue {
46 private final List<JsonValue> values;
47
48 /**
49 * Creates a new jCard value.
50 * @param values the values
51 */
52 public JCardValue(List<JsonValue> values) {
53 this.values = Collections.unmodifiableList(values);
54 }
55
56 /**
57 * Creates a new jCard value.
58 * @param values the values
59 */
60 public JCardValue(JsonValue... values) {
61 this.values = Arrays.asList(values); //unmodifiable
62 }
63
64 /**
65 * Creates a single-valued value.
66 * @param value the value
67 * @return the jCard value
68 */
69 public static JCardValue single(Object value) {
70 return new JCardValue(new JsonValue(value));
71 }
72
73 /**
74 * Creates a multi-valued value.
75 * @param values the values
76 * @return the jCard value
77 */
78 public static JCardValue multi(Object... values) {
79 return multi(Arrays.asList(values));
80 }
81
82 /**
83 * Creates a multi-valued value.
84 * @param values the values
85 * @return the jCard value
86 */
87 public static JCardValue multi(List<?> values) {
88 List<JsonValue> multiValues = new ArrayList<>(values.size());
89 for (Object value : values) {
90 multiValues.add(new JsonValue(value));
91 }
92 return new JCardValue(multiValues);
93 }
94
95 /**
96 * <p>
97 * Creates a structured value.
98 * </p>
99 * <p>
100 * This method accepts a vararg of {@link Object} instances. {@link List}
101 * objects will be treated as multi-valued components. Null objects will be
102 * treated as empty components.
103 * </p>
104 * @param values the values
105 * @return the jCard value
106 */
107 public static JCardValue structured(Object... values) {
108 List<List<?>> valuesList = new ArrayList<>(values.length);
109 for (Object value : values) {
110 List<?> list = (value instanceof List) ? (List<?>) value : Collections.singletonList(value);
111 valuesList.add(list);
112 }
113 return structured(valuesList);
114 }
115
116 /**
117 * Creates a structured value.
118 * @param values the values
119 * @return the jCard value
120 */
121 public static JCardValue structured(List<List<?>> values) {
122 List<JsonValue> array = new ArrayList<>(values.size());
123
124 for (List<?> list : values) {
125 if (list.isEmpty()) {
126 array.add(new JsonValue(""));
127 continue;
128 }
129
130 if (list.size() == 1) {
131 Object value = list.get(0);
132 if (value == null) {
133 value = "";
134 }
135 array.add(new JsonValue(value));
136 continue;
137 }
138
139 List<JsonValue> subArray = new ArrayList<>(list.size());
140 for (Object value : list) {
141 if (value == null) {
142 value = "";
143 }
144 subArray.add(new JsonValue(value));
145 }
146 array.add(new JsonValue(subArray));
147 }
148
149 return new JCardValue(new JsonValue(array));
150 }
151
152 /**
153 * Gets all the JSON values.
154 * @return the JSON values
155 */
156 public List<JsonValue> getValues() {
157 return values;
158 }
159
160 /**
161 * Gets the value of a single-valued property (such as {@link Note}).
162 * @return the value or empty string if not found
163 */
164 public String asSingle() {
165 if (values.isEmpty()) {
166 return "";
167 }
168
169 JsonValue first = values.get(0);
170 if (first.isNull()) {
171 return "";
172 }
173
174 Object obj = first.getValue();
175 if (obj != null) {
176 return obj.toString();
177 }
178
179 //get the first element of the array
180 List<JsonValue> array = first.getArray();
181 if (array != null && !array.isEmpty()) {
182 obj = array.get(0).getValue();
183 if (obj != null) {
184 return obj.toString();
185 }
186 }
187
188 return "";
189 }
190
191 /**
192 * Gets the value of a structured property (such as {@link StructuredName}).
193 * @return the values or empty list if not found
194 */
195 public List<List<String>> asStructured() {
196 if (values.isEmpty()) {
197 return Collections.emptyList();
198 }
199
200 JsonValue first = values.get(0);
201
202 //["gender", {}, "text", ["M", "text"] ]
203 List<JsonValue> array = first.getArray();
204 if (array != null) {
205 List<List<String>> components = new ArrayList<>(array.size());
206 for (JsonValue value : array) {
207 if (value.isNull()) {
208 components.add(Collections.<String>emptyList());
209 continue;
210 }
211
212 Object obj = value.getValue();
213 if (obj != null) {
214 String s = obj.toString();
215 List<String> component = s.isEmpty() ? Collections.<String>emptyList() : Collections.singletonList(s);
216 components.add(component);
217 continue;
218 }
219
220 List<JsonValue> subArray = value.getArray();
221 if (subArray != null) {
222 List<String> component = new ArrayList<>(subArray.size());
223 for (JsonValue subArrayValue : subArray) {
224 if (subArrayValue.isNull()) {
225 component.add("");
226 continue;
227 }
228
229 obj = subArrayValue.getValue();
230 if (obj != null) {
231 component.add(obj.toString());
232 continue;
233 }
234 }
235
236 if (component.size() == 1 && component.get(0).isEmpty()) {
237 component.clear();
238 }
239 components.add(component);
240 }
241 }
242 return components;
243 }
244
245 //get the first value if it's not enclosed in an array
246 //["gender", {}, "text", "M"]
247 Object obj = first.getValue();
248 if (obj != null) {
249 List<List<String>> components = new ArrayList<>(1);
250 String s = obj.toString();
251 List<String> component = s.isEmpty() ? Collections.<String>emptyList() : Collections.singletonList(s);
252 components.add(component);
253 return components;
254 }
255
256 //["gender", {}, "text", null]
257 if (first.isNull()) {
258 List<List<String>> components = new ArrayList<>(1);
259 components.add(Collections.<String>emptyList());
260 return components;
261 }
262
263 return Collections.emptyList();
264 }
265
266 /**
267 * Gets the value of a multi-valued property (such as {@link Categories} ).
268 * @return the values or empty list if not found
269 */
270 public List<String> asMulti() {
271 if (values.isEmpty()) {
272 return Collections.emptyList();
273 }
274
275 List<String> multi = new ArrayList<>(values.size());
276 for (JsonValue value : values) {
277 if (value.isNull()) {
278 multi.add("");
279 continue;
280 }
281
282 Object obj = value.getValue();
283 if (obj != null) {
284 multi.add(obj.toString());
285 continue;
286 }
287 }
288 return multi;
289 }
290
291 @Override
292 public boolean equals(Object o) {
293 if (this == o) {
294 return true;
295 }
296 if (o == null || getClass() != o.getClass()) {
297 return false;
298 }
299
300 JCardValue that = (JCardValue) o;
301
302 if (values != null ? !values.equals(that.values) : that.values != null) {
303 return false;
304 }
305
306 return true;
307 }
308
309 @Override
310 public int hashCode() {
311 return values != null ? values.hashCode() : 0;
312 }
313 }