2121import java .math .BigDecimal ;
2222import java .math .BigInteger ;
2323import java .util .ArrayList ;
24+ import java .util .Collection ;
2425import java .util .Iterator ;
2526import java .util .List ;
2627
4041 * @author Joel Leitch
4142 */
4243public final class JsonArray extends JsonElement implements Iterable <JsonElement > {
44+ /** All elements in this list are non-null. */
4345 private final ArrayList <JsonElement > elements ;
4446
47+ @ SuppressWarnings ({"deprecation" , "NonApiType" }) // superclass constructor
48+ private JsonArray (ArrayList <JsonElement > elements ) {
49+ this .elements = elements ;
50+ }
51+
4552 /** Creates an empty JsonArray. */
46- @ SuppressWarnings ("deprecation" ) // superclass constructor
4753 public JsonArray () {
48- elements = new ArrayList <>();
54+ this ( new ArrayList <>() );
4955 }
5056
5157 /**
@@ -55,9 +61,35 @@ public JsonArray() {
5561 * @throws IllegalArgumentException if the {@code capacity} is negative
5662 * @since 2.8.1
5763 */
58- @ SuppressWarnings ("deprecation" ) // superclass constructor
5964 public JsonArray (int capacity ) {
60- elements = new ArrayList <>(capacity );
65+ this (new ArrayList <>(capacity ));
66+ }
67+
68+ /**
69+ * Creates a JsonArray with the specified elements.
70+ *
71+ * @return a new JsonArray.
72+ * @since $next-version$
73+ */
74+ public static JsonArray of (JsonElement ... elements ) {
75+ JsonArray array = new JsonArray (elements .length );
76+ array .addAll (elements );
77+ return array ;
78+ }
79+
80+ /**
81+ * Creates a JsonArray with the specified elements.
82+ *
83+ * @return a new JsonArray.
84+ * @since $next-version$
85+ */
86+ public static JsonArray of (Iterable <? extends JsonElement > elements ) {
87+ JsonArray array =
88+ elements instanceof Collection
89+ ? new JsonArray (((Collection <?>) elements ).size ())
90+ : new JsonArray ();
91+ array .addAll (elements );
92+ return array ;
6193 }
6294
6395 /**
@@ -123,10 +155,7 @@ public void add(String string) {
123155 * @param element the element that needs to be added to the array.
124156 */
125157 public void add (JsonElement element ) {
126- if (element == null ) {
127- element = JsonNull .INSTANCE ;
128- }
129- elements .add (element );
158+ elements .add (element == null ? JsonNull .INSTANCE : element );
130159 }
131160
132161 /**
@@ -138,6 +167,38 @@ public void addAll(JsonArray array) {
138167 elements .addAll (array .elements );
139168 }
140169
170+ /**
171+ * Adds all the elements to self.
172+ *
173+ * @param elements the elements to be added.
174+ * @since $next-version$
175+ */
176+ public void addAll (JsonElement ... elements ) {
177+ this .elements .ensureCapacity (elements .length + this .elements .size ());
178+ for (JsonElement element : elements ) {
179+ add (element );
180+ }
181+ }
182+
183+ /**
184+ * Adds all the elements of the specified iterable to self.
185+ *
186+ * @param iterable the iterable whose elements need to be added to the array.
187+ * @since $next-version$
188+ */
189+ public void addAll (Iterable <? extends JsonElement > iterable ) {
190+ if (iterable instanceof JsonArray ) {
191+ addAll ((JsonArray ) iterable );
192+ return ;
193+ } else if (iterable instanceof Collection ) {
194+ elements .ensureCapacity (((Collection <?>) iterable ).size () + elements .size ());
195+ }
196+
197+ for (JsonElement element : iterable ) {
198+ add (element );
199+ }
200+ }
201+
141202 /**
142203 * Replaces the element at the specified position in this array with the specified element.
143204 *
0 commit comments