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 hat.tools.json;
27 
28 
29 import hat.tools.json.impl.JsonArrayImpl;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /**
35  * The interface that represents JSON array.
36  * <p>
37  * A {@code JsonArray} can be produced by {@link Json#parse(String)}.
38  * <p> Alternatively, {@link #of(List)} can be used to obtain a {@code JsonArray}.
39  *
40  * @since 99
41  */
42 public non-sealed interface JsonArray extends JsonValue {
43 
44     /**
45      * {@return an unmodifiable list of the {@code JsonValue} elements in
46      * this {@code JsonArray}}
47      */
48     List<JsonValue> values();
49 
50     /**
51      * {@return the {@code JsonArray} created from the given
52      * list of {@code JsonValue}s}
53      *
54      * @param src the list of {@code JsonValue}s. Non-null.
55      * @throws NullPointerException if {@code src} is {@code null}, or contains
56      *      any values that are {@code null}
57      */
58     static JsonArray of(List<? extends JsonValue> src) {
59         var values = new ArrayList<JsonValue>(src); // implicit null check
60         if (values.contains(null)) {
61             throw new NullPointerException("src contains null value(s)");
62         }
63         return new JsonArrayImpl(values);
64     }
65 
66     /**
67      * {@return {@code true} if the given object is also a {@code JsonArray}
68      * and the two {@code JsonArray}s represent the same elements} Two
69      * {@code JsonArray}s {@code ja1} and {@code ja2} represent the same
70      * elements if {@code ja1.values().equals(ja2.values())}.
71      *
72      * @see #values()
73      */
74     @Override
75     boolean equals(Object obj);
76 
77     /**
78      * {@return the hash code value for this {@code JsonArray}} The hash code of a
79      * {@code JsonArray} is calculated by {@code Objects.hash(JsonArray.values()}.
80      * Thus, for two {@code JsonArray}s {@code ja1} and {@code ja2},
81      * {@code ja1.equals(ja2)} implies that {@code ja1.hashCode() == ja2.hashCode()}
82      * as required by the general contract of {@link Object#hashCode}.
83      *
84      * @see #values()
85      */
86     @Override
87     int hashCode();
88 }