Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import java.util.List;
4 import java.util.Map;
5
6 /*
7 Copyright (c) 2012-2023, Michael Angstadt
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 1. Redistributions of source code must retain the above copyright notice, this
14 list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * Represents a JSON value, array, or object.
33 * @author Michael Angstadt
34 */
35 public class JsonValue {
36 private final boolean isNull;
37 private final Object value;
38 private final List<JsonValue> array;
39 private final Map<String, JsonValue> object;
40
41 /**
42 * Creates a JSON value (such as a string or integer).
43 * @param value the value
44 */
45 public JsonValue(Object value) {
46 this.value = value;
47 array = null;
48 object = null;
49 isNull = (value == null);
50 }
51
52 /**
53 * Creates a JSON array.
54 * @param array the array elements
55 */
56 public JsonValue(List<JsonValue> array) {
57 this.array = array;
58 value = null;
59 object = null;
60 isNull = (array == null);
61 }
62
63 /**
64 * Creates a JSON object.
65 * @param object the object fields
66 */
67 public JsonValue(Map<String, JsonValue> object) {
68 this.object = object;
69 value = null;
70 array = null;
71 isNull = (object == null);
72 }
73
74 /**
75 * Gets the JSON value.
76 * @return the value or null if it's not a JSON value
77 */
78 public Object getValue() {
79 return value;
80 }
81
82 /**
83 * Gets the JSON array elements.
84 * @return the array elements or null if it's not a JSON array
85 */
86 public List<JsonValue> getArray() {
87 return array;
88 }
89
90 /**
91 * Gets the JSON object.
92 * @return the object or null if it's not a JSON object
93 */
94 public Map<String, JsonValue> getObject() {
95 return object;
96 }
97
98 /**
99 * Determines if the value is "null" or not.
100 * @return true if the value is "null", false if not
101 */
102 public boolean isNull() {
103 return isNull;
104 }
105
106 @Override
107 public int hashCode() {
108 final int prime = 31;
109 int result = 1;
110 result = prime * result + ((array == null) ? 0 : array.hashCode());
111 result = prime * result + (isNull ? 1231 : 1237);
112 result = prime * result + ((object == null) ? 0 : object.hashCode());
113 result = prime * result + ((value == null) ? 0 : value.hashCode());
114 return result;
115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj)
120 return true;
121 if (obj == null)
122 return false;
123 if (getClass() != obj.getClass())
124 return false;
125 JsonValue other = (JsonValue) obj;
126 if (array == null) {
127 if (other.array != null)
128 return false;
129 } else if (!array.equals(other.array))
130 return false;
131 if (isNull != other.isNull)
132 return false;
133 if (object == null) {
134 if (other.object != null)
135 return false;
136 } else if (!object.equals(other.object))
137 return false;
138 if (value == null) {
139 if (other.value != null)
140 return false;
141 } else if (!value.equals(other.value))
142 return false;
143 return true;
144 }
145
146 @Override
147 public String toString() {
148 if (isNull) {
149 return "NULL";
150 }
151
152 if (value != null) {
153 return "VALUE = " + value;
154 }
155
156 if (array != null) {
157 return "ARRAY = " + array;
158 }
159
160 if (object != null) {
161 return "OBJECT = " + object;
162 }
163
164 return "";
165 }
166 }