View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
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 io.wcm.caravan.io.jsontransform.element;
21  
22  import org.apache.commons.lang3.builder.EqualsBuilder;
23  import org.apache.commons.lang3.builder.HashCodeBuilder;
24  import org.apache.commons.lang3.builder.ToStringBuilder;
25  import org.osgi.annotation.versioning.ProviderType;
26  
27  /**
28   * A simple bean representing a JSON stream element.
29   */
30  @ProviderType
31  public final class JsonElement {
32  
33    /**
34     * The default JSON object start element
35     */
36    public static final JsonElement DEFAULT_START_OBJECT = new JsonElement(null, null, JsonElementType.START_OBJECT);
37  
38    /**
39     * The default JSON array start element
40     */
41    public static final JsonElement DEFAULT_START_ARRAY = new JsonElement(null, null, JsonElementType.START_ARRAY);
42  
43    /**
44     * The default JSON object end element
45     */
46    public static final JsonElement DEFAULT_END_OBJECT = new JsonElement(null, null, JsonElementType.END_OBJECT);
47  
48    /**
49     * The default JSON array end element
50     */
51    public static final JsonElement DEFAULT_END_ARRAY = new JsonElement(null, null, JsonElementType.END_ARRAY);
52  
53    /**
54     * The key of the JSON stream element. If {@link JsonElementType} is END_OBJECT or END_ARRAY will be null. Otherwise can be null.
55     */
56    private final String key;
57  
58    /**
59     * The value of the JSON stream element. Only set for VALUE {@link JsonElementType}.
60     */
61    private final Object value;
62  
63    /**
64     * The JSON stream element type.
65     */
66    private final JsonElementType type;
67  
68    /**
69     * Creator for JSON value element with value being NULL.
70     * @param key JSON element name
71     * @return JSON value element with value NULL
72     */
73    public static JsonElement nullValue(String key) {
74      return new JsonElement(key, null, JsonElementType.VALUE);
75    }
76  
77    /**
78     * Creator for JSON value element with given value and no key.
79     * @param value JSON element value
80     * @return JSON value element
81     */
82    public static JsonElement value(Object value) {
83      return new JsonElement(null, value, JsonElementType.VALUE);
84    }
85  
86    /**
87     * Creator for JSON value element with given key and value
88     * @param key JSON element name
89     * @param value JSON element value
90     * @return JSON value element
91     */
92    public static JsonElement value(String key, Object value) {
93      return new JsonElement(key, value, JsonElementType.VALUE);
94    }
95  
96    /**
97     * Creator for JSON object start element with given key
98     * @param key JSON element name
99     * @return JSON object start element
100    */
101   public static JsonElement startObject(String key) {
102     return new JsonElement(key, null, JsonElementType.START_OBJECT);
103   }
104 
105   /**
106    * Creator for JSON array start element with given key
107    * @param key JSON element name
108    * @return JSON array start element
109    */
110   public static JsonElement startArray(String key) {
111     return new JsonElement(key, null, JsonElementType.START_ARRAY);
112   }
113 
114   /**
115    * @param key JSON element name
116    * @param value JSON element value
117    * @param type JSON element type
118    */
119   public JsonElement(String key, Object value, JsonElementType type) {
120     this.key = key;
121     this.value = value;
122     this.type = type;
123   }
124 
125   /**
126    * @return the key
127    */
128   public String getKey() {
129     return this.key;
130   }
131 
132   /**
133    * @return the value
134    */
135   public Object getValue() {
136     return this.value;
137   }
138 
139   /**
140    * @return the type
141    */
142   public JsonElementType getType() {
143     return this.type;
144   }
145 
146   @Override
147   public String toString() {
148     return ToStringBuilder.reflectionToString(this);
149   }
150 
151   @Override
152   public int hashCode() {
153     return HashCodeBuilder.reflectionHashCode(this, false);
154   }
155 
156   @Override
157   public boolean equals(Object obj) {
158     return EqualsBuilder.reflectionEquals(this, obj, false);
159   }
160 
161   /**
162    * @return True if is a starting element
163    */
164   public boolean isStartingElement() {
165     return JsonElementType.START_ARRAY.equals(type) || JsonElementType.START_OBJECT.equals(type);
166   }
167 
168   /**
169    * @return True if is a closing element
170    */
171   public boolean isClosingElement() {
172     return JsonElementType.END_ARRAY.equals(type) || JsonElementType.END_OBJECT.equals(type);
173   }
174 
175 }