View Javadoc
1   /*
2    * #%L
3    * HAPI FHIR - Core Library
4    * %%
5    * Copyright (C) 2014 - 2018 University Health Network
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package ca.uhn.fhir.parser.json;
21  
22  /**
23   * This is the generalization of anything that is a "value"
24   * element in a JSON structure. This could be a JSON object,
25   * a JSON array, a scalar value (number, string, boolean),
26   * or a null.
27   * 
28   */
29  public abstract class JsonLikeValue {
30  	
31  	public enum ValueType {
32  		ARRAY, OBJECT, SCALAR, NULL
33  	};
34  	
35  	public enum ScalarType {
36  		NUMBER, STRING, BOOLEAN
37  	}
38  	
39  	public abstract ValueType getJsonType ();
40  	
41  	public abstract ScalarType getDataType ();
42  	
43  	public abstract Object getValue ();
44  	
45  	public boolean isArray () {
46  		return this.getJsonType() == ValueType.ARRAY;
47  	}
48  	
49  	public boolean isObject () {
50  		return this.getJsonType() == ValueType.OBJECT;
51  	}
52  	
53  	public boolean isScalar () {
54  		return this.getJsonType() == ValueType.SCALAR;
55  	}
56  	
57  	public boolean isString () {
58  		return this.getJsonType() == ValueType.SCALAR && this.getDataType() == ScalarType.STRING;
59  	}
60  	
61  	public boolean isNumber () {
62  		return this.getJsonType() == ValueType.SCALAR && this.getDataType() == ScalarType.NUMBER;
63  	}
64  	
65  	public boolean isNull () {
66  		return this.getJsonType() == ValueType.NULL;
67  	}
68  	
69  	public JsonLikeArray getAsArray () {
70  		return null;
71  	}
72  	public JsonLikeObject getAsObject () {
73  		return null;
74  	}
75  	public String getAsString () {
76  		return this.toString();
77  	}
78  	public Number getAsNumber () {
79  		return this.isNumber() ? (Number)this.getValue() : null;
80  	}
81  	public boolean getAsBoolean () {
82  		return !isNull();
83  	}
84  	
85  	public static JsonLikeArray asArray (JsonLikeValue element) {
86  		if (element != null) {
87  			return element.getAsArray();
88  		}
89  		return null;
90  	}
91  	public static JsonLikeObject asObject (JsonLikeValue element) {
92  		if (element != null) {
93  			return element.getAsObject();
94  		}
95  		return null;
96  	}
97  	public static String asString (JsonLikeValue element) {
98  		if (element != null) {
99  			return element.getAsString();
100 		}
101 		return null;
102 	}
103 	public static boolean asBoolean (JsonLikeValue element) {
104 		if (element != null) {
105 			return element.getAsBoolean();
106 		}
107 		return false;
108 	}
109 	
110 
111     public static final JsonLikeValue NULL = new JsonLikeValue() {
112         @Override
113         public ValueType getJsonType() {
114             return ValueType.NULL;
115         }
116 
117         @Override
118 		public ScalarType getDataType() {
119 			return null;
120 		}
121 
122 		@Override
123 		public Object getValue() {
124 			 return null;
125 		}
126 
127 		@Override
128         public boolean equals (Object obj) {
129             if (this == obj){
130                 return true;
131             }
132             if (obj instanceof JsonLikeValue) {
133                 return getJsonType().equals(((JsonLikeValue)obj).getJsonType());
134             }
135             return false;
136         }
137 
138         @Override
139         public int hashCode() {
140             return "null".hashCode();
141         }
142 
143         @Override
144         public String toString() {
145             return "null";
146         }
147     };
148 
149     public static final JsonLikeValue TRUE = new JsonLikeValue() {
150         @Override
151         public ValueType getJsonType() {
152         	return ValueType.SCALAR;
153         }
154         
155         @Override
156         public ScalarType getDataType() {
157             return ScalarType.BOOLEAN;
158         }
159 
160 		  @Override
161 		  public Object getValue() {
162 			   return Boolean.TRUE;
163 		  }
164 
165         @Override
166         public boolean equals(Object obj) {
167             if (this == obj){
168                 return true;
169             }
170             if (obj instanceof JsonLikeValue) {
171                 return getJsonType().equals(((JsonLikeValue)obj).getJsonType())
172                 	&& getDataType().equals(((JsonLikeValue)obj).getDataType())
173                 	&& toString().equals(((JsonLikeValue)obj).toString());
174             }
175             return false;
176         }
177 
178         @Override
179         public int hashCode() {
180             return "true".hashCode();
181         }
182 
183         @Override
184         public String toString() {
185             return "true";
186         }
187     };
188 
189     public static final JsonLikeValue FALSE = new JsonLikeValue() {
190         @Override
191         public ValueType getJsonType() {
192         	return ValueType.SCALAR;
193         }
194         
195         @Override
196         public ScalarType getDataType() {
197             return ScalarType.BOOLEAN;
198         }
199 
200 		  @Override
201 		  public Object getValue() {
202 			   return Boolean.FALSE;
203 		  }
204 
205         @Override
206         public boolean equals(Object obj) {
207             if (this == obj){
208                 return true;
209             }
210             if (obj instanceof JsonLikeValue) {
211                 return getJsonType().equals(((JsonLikeValue)obj).getJsonType())
212                     	&& getDataType().equals(((JsonLikeValue)obj).getDataType())
213                     	&& toString().equals(((JsonLikeValue)obj).toString());
214             }
215             return false;
216         }
217 
218         @Override
219         public int hashCode() {
220             return "false".hashCode();
221         }
222 
223         @Override
224         public String toString() {
225             return "false";
226         }
227     };
228 }