1 /*
 2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package oracle.code.json;
27 
28 import oracle.code.json.impl.JsonArrayImpl;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * The interface that represents JSON array.
35  * <p>
36  * A {@code JsonArray} can be produced by {@link Json#parse(String)}.
37  * <p> Alternatively, {@link #of(List)} can be used to obtain a {@code JsonArray}.
38  *
39  * @since 99
40  */
41 public non-sealed interface JsonArray extends JsonValue {
42 
43     /**
44      * {@return an unmodifiable list of the {@code JsonValue} elements in
45      * this {@code JsonArray}}
46      */
47     List<JsonValue> values();
48 
49     /**
50      * {@return the {@code JsonArray} created from the given
51      * list of {@code JsonValue}s}
52      *
53      * @param src the list of {@code JsonValue}s. Non-null.
54      * @throws NullPointerException if {@code src} is {@code null}, or contains
55      *      any values that are {@code null}
56      */
57     static JsonArray of(List<? extends JsonValue> src) {
58         var values = new ArrayList<JsonValue>(src); // implicit null check
59         if (values.contains(null)) {
60             throw new NullPointerException("src contains null value(s)");
61         }
62         return new JsonArrayImpl(values);
63     }
64 
65     /**
66      * {@return {@code true} if the given object is also a {@code JsonArray}
67      * and the two {@code JsonArray}s represent the same elements} Two
68      * {@code JsonArray}s {@code ja1} and {@code ja2} represent the same
69      * elements if {@code ja1.values().equals(ja2.values())}.
70      *
71      * @see #values()
72      */
73     @Override
74     boolean equals(Object obj);
75 
76     /**
77      * {@return the hash code value for this {@code JsonArray}} The hash code of a
78      * {@code JsonArray} is calculated by {@code Objects.hash(JsonArray.values()}.
79      * Thus, for two {@code JsonArray}s {@code ja1} and {@code ja2},
80      * {@code ja1.equals(ja2)} implies that {@code ja1.hashCode() == ja2.hashCode()}
81      * as required by the general contract of {@link Object#hashCode}.
82      *
83      * @see #values()
84      */
85     @Override
86     int hashCode();
87 }