net.sourceforge.plantuml.json.JsonObject Maven / Gradle / Ivy
Show all versions of plantuml-lgpl Show documentation
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
/* +=======================================================================
* |
* | PlantUML : a free UML diagram generator
* |
* +=======================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/liberapay (only 1€ per month!)
* https://plantuml.com/paypal
*
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see .
*
* PlantUML can occasionally display sponsored or advertising messages. Those
* messages are usually generated on welcome or error images and never on
* functional diagrams.
* See https://plantuml.com/professional if you want to remove them
*
* Images (whatever their format : PNG, SVG, EPS...) generated by running PlantUML
* are owned by the author of their corresponding sources code (that is, their
* textual description in PlantUML language). Those images are not covered by
* this LGPL license.
*
* The generated images can then be used without any reference to the LGPL license.
* It is not even necessary to stipulate that they have been generated with PlantUML,
* although this will be appreciated by the PlantUML team.
*
* There is an exception : if the textual description in PlantUML language is also covered
* by any license, then the generated images are logically covered
* by the very same license.
*
* This is the IGY distribution (Install GraphViz by Yourself).
* You have to install GraphViz and to setup the GRAPHVIZ_DOT environment variable
* (see https://plantuml.com/graphviz-dot )
*
* Icons provided by OpenIconic : https://useiconic.com/open
* Archimate sprites provided by Archi : http://www.archimatetool.com
* Stdlib AWS provided by https://github.com/milo-minderbinder/AWS-PlantUML
* Stdlib Icons provided https://github.com/tupadr3/plantuml-icon-font-sprites
* ASCIIMathML (c) Peter Jipsen http://www.chapman.edu/~jipsen
* ASCIIMathML (c) David Lippman http://www.pierce.ctc.edu/dlippman
* CafeUndZopfli ported by Eugene Klyuchnikov https://github.com/eustas/CafeUndZopfli
* Brotli (c) by the Brotli Authors https://github.com/google/brotli
* Themes (c) by Brett Schwarz https://github.com/bschwarz/puml-themes
* Twemoji (c) by Twitter at https://twemoji.twitter.com/
*
*/
package net.sourceforge.plantuml.json;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import net.sourceforge.plantuml.json.JsonObject.Member;
/**
* Represents a JSON object, a set of name/value pairs, where the names are
* strings and the values are JSON values.
*
* Members can be added using the add(String, ...)
methods which
* accept instances of {@link JsonValue}, strings, primitive numbers, and
* boolean values. To modify certain values of an object, use the
* set(String, ...)
methods. Please note that the add
* methods are faster than set
as they do not search for existing
* members. On the other hand, the add
methods do not prevent
* adding multiple members with the same name. Duplicate names are discouraged
* but not prohibited by JSON.
*
*
* Members can be accessed by their name using {@link #get(String)}. A list of
* all names can be obtained from the method {@link #names()}. This class also
* supports iterating over the members in document order using an
* {@link #iterator()} or an enhanced for loop:
*
*
*
* for (Member member : jsonObject) {
* String name = member.getName();
* JsonValue value = member.getValue();
* ...
* }
*
*
* Even though JSON objects are unordered by definition, instances of this class
* preserve the order of members to allow processing in document order and to
* guarantee a predictable output.
*
*
* Note that this class is not thread-safe. If multiple threads
* access a JsonObject
instance concurrently, while at least one of
* these threads modifies the contents of this object, access to the instance
* must be synchronized externally. Failure to do so may lead to an inconsistent
* state.
*
*
* This class is not supposed to be extended by clients.
*
*/
@SuppressWarnings("serial") // use default serial UID
public class JsonObject extends JsonValue implements Iterable {
/**
* Added for PlantUML
*/
public JsonValue cloneMe() {
final JsonObject copy = new JsonObject();
for (int i=0; i names;
private final List values;
private transient HashIndexTable table;
/**
* Creates a new empty JsonObject.
*/
public JsonObject() {
names = new ArrayList<>();
values = new ArrayList<>();
table = new HashIndexTable();
}
/**
* Creates a new JsonObject, initialized with the contents of the specified JSON
* object.
*
* @param object the JSON object to get the initial contents from, must not be
* null
*/
public JsonObject(JsonObject object) {
this(object, false);
}
private JsonObject(JsonObject object, boolean unmodifiable) {
if (object == null) {
throw new NullPointerException("object is null");
}
if (unmodifiable) {
names = Collections.unmodifiableList(object.names);
values = Collections.unmodifiableList(object.values);
} else {
names = new ArrayList<>(object.names);
values = new ArrayList<>(object.values);
}
table = new HashIndexTable();
updateHashIndex();
}
/**
* Reads a JSON object from the given reader.
*
* Characters are read in chunks and buffered internally, therefore wrapping an
* existing reader in an additional BufferedReader
does
* not improve reading performance.
*
*
* @param reader the reader to read the JSON object from
* @return the JSON object that has been read
* @throws IOException if an I/O error occurs in the reader
* @throws ParseException if the input is not valid JSON
* @throws UnsupportedOperationException if the input does not contain a JSON
* object
* @deprecated Use {@link Json#parse(Reader)}{@link JsonValue#asObject()
* .asObject()} instead
*/
@Deprecated
public static JsonObject readFrom(Reader reader) throws IOException {
return JsonValue.readFrom(reader).asObject();
}
/**
* Reads a JSON object from the given string.
*
* @param string the string that contains the JSON object
* @return the JSON object that has been read
* @throws ParseException if the input is not valid JSON
* @throws UnsupportedOperationException if the input does not contain a JSON
* object
* @deprecated Use {@link Json#parse(String)}{@link JsonValue#asObject()
* .asObject()} instead
*/
@Deprecated
public static JsonObject readFrom(String string) {
return JsonValue.readFrom(string).asObject();
}
/**
* Returns an unmodifiable JsonObject for the specified one. This method allows
* to provide read-only access to a JsonObject.
*
* The returned JsonObject is backed by the given object and reflect changes
* that happen to it. Attempts to modify the returned JsonObject result in an
* UnsupportedOperationException
.
*
*
* @param object the JsonObject for which an unmodifiable JsonObject is to be
* returned
* @return an unmodifiable view of the specified JsonObject
*/
public static JsonObject unmodifiableObject(JsonObject object) {
return new JsonObject(object, true);
}
/**
* Appends a new member to the end of this object, with the specified name and
* the JSON representation of the specified int
value.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, int value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and
* the JSON representation of the specified long
value.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, long value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and
* the JSON representation of the specified float
value.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, float value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and
* the JSON representation of the specified double
value.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, double value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and
* the JSON representation of the specified boolean
value.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, boolean value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and
* the JSON representation of the specified string.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, String value) {
add(name, Json.value(value));
return this;
}
/**
* Appends a new member to the end of this object, with the specified name and
* the specified JSON value.
*
* This method does not prevent duplicate names. Calling this
* method with a name that already exists in the object will append another
* member with the same name. In order to replace existing members, use the
* method set(name, value)
instead. However, add
* is much faster than set (because it does not need to search
* for existing members). Therefore add should be preferred when
* constructing new objects.
*
*
* @param name the name of the member to add
* @param value the value of the member to add, must not be null
* @return the object itself, to enable method chaining
*/
public JsonObject add(String name, JsonValue value) {
if (name == null) {
throw new NullPointerException("name is null");
}
if (value == null) {
throw new NullPointerException("value is null");
}
table.add(name, names.size());
names.add(name);
values.add(value);
return this;
}
/**
* Sets the value of the member with the specified name to the JSON
* representation of the specified int
value. If this object does
* not contain a member with this name, a new member is added at the end of the
* object. If this object contains multiple members with this name, only the
* last one is changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to replace
* @param value the value to set to the member
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, int value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON
* representation of the specified long
value. If this object does
* not contain a member with this name, a new member is added at the end of the
* object. If this object contains multiple members with this name, only the
* last one is changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to replace
* @param value the value to set to the member
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, long value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON
* representation of the specified float
value. If this object does
* not contain a member with this name, a new member is added at the end of the
* object. If this object contains multiple members with this name, only the
* last one is changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, float value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON
* representation of the specified double
value. If this object
* does not contain a member with this name, a new member is added at the end of
* the object. If this object contains multiple members with this name, only the
* last one is changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, double value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON
* representation of the specified boolean
value. If this object
* does not contain a member with this name, a new member is added at the end of
* the object. If this object contains multiple members with this name, only the
* last one is changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, boolean value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the JSON
* representation of the specified string. If this object does not contain a
* member with this name, a new member is added at the end of the object. If
* this object contains multiple members with this name, only the last one is
* changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to add
* @param value the value of the member to add
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, String value) {
set(name, Json.value(value));
return this;
}
/**
* Sets the value of the member with the specified name to the specified JSON
* value. If this object does not contain a member with this name, a new member
* is added at the end of the object. If this object contains multiple members
* with this name, only the last one is changed.
*
* This method should only be used to modify existing objects.
* To fill a new object with members, the method add(name, value)
* should be preferred which is much faster (as it does not need to search for
* existing members).
*
*
* @param name the name of the member to add
* @param value the value of the member to add, must not be null
* @return the object itself, to enable method chaining
*/
public JsonObject set(String name, JsonValue value) {
if (name == null) {
throw new NullPointerException("name is null");
}
if (value == null) {
throw new NullPointerException("value is null");
}
int index = indexOf(name);
if (index != -1) {
values.set(index, value);
} else {
table.add(name, names.size());
names.add(name);
values.add(value);
}
return this;
}
/**
* Removes a member with the specified name from this object. If this object
* contains multiple members with the given name, only the last one is removed.
* If this object does not contain a member with the specified name, the object
* is not modified.
*
* @param name the name of the member to remove
* @return the object itself, to enable method chaining
*/
public JsonObject remove(String name) {
if (name == null) {
throw new NullPointerException("name is null");
}
int index = indexOf(name);
if (index != -1) {
table.remove(index);
names.remove(index);
values.remove(index);
}
return this;
}
/**
* Checks if a specified member is present as a child of this object. This will
* not test if this object contains the literal null
,
* {@link JsonValue#isNull()} should be used for this purpose.
*
* @param name the name of the member to check for
* @return whether or not the member is present
*/
public boolean contains(String name) {
return names.contains(name);
}
/**
* Copies all members of the specified object into this object. When the
* specified object contains members with names that also exist in this object,
* the existing values in this object will be replaced by the corresponding
* values in the specified object.
*
* @param object the object to merge
* @return the object itself, to enable method chaining
*/
public JsonObject merge(JsonObject object) {
if (object == null) {
throw new NullPointerException("object is null");
}
for (Member member : object) {
this.set(member.name, member.value);
}
return this;
}
/**
* Copies all members of the specified object into this object. When the
* specified object contains members with names that also exist in this object,
* the existing values in this object will be replaced by the corresponding
* values in the specified object, except for the case that both values are
* JsonObjects themselves, which will trigger another merge of these objects.
*
* @param object the object to deep merge
* @return the object itself, to enable method chaining
*/
public JsonObject deepMerge(JsonObject object) {
if (object == null) {
throw new NullPointerException("object is null");
}
for (Member member : object) {
final String name = member.name;
JsonValue value = member.value;
if (value instanceof JsonObject) {
final JsonValue existingValue = this.get(member.name);
if (existingValue instanceof JsonObject) {
value = ((JsonObject) existingValue).deepMerge((JsonObject) value);
}
}
this.set(name, value);
}
return this;
}
/**
* Returns the value of the member with the specified name in this object. If
* this object contains multiple members with the given name, this method will
* return the last one.
*
* @param name the name of the member whose value is to be returned
* @return the value of the last member with the specified name, or
* null
if this object does not contain a member with that
* name
*/
public JsonValue get(String name) {
if (name == null) {
throw new NullPointerException("name is null");
}
int index = indexOf(name);
return index != -1 ? values.get(index) : null;
}
/**
* Returns the int
value of the member with the specified name in
* this object. If this object does not contain a member with this name, the
* given default value is returned. If this object contains multiple members
* with the given name, the last one will be picked. If this member's value does
* not represent a JSON number or if it cannot be interpreted as Java
* int
, an exception is thrown.
*
* @param name the name of the member whose value is to be returned
* @param defaultValue the value to be returned if the requested member is
* missing
* @return the value of the last member with the specified name, or the given
* default value if this object does not contain a member with that name
*/
public int getInt(String name, int defaultValue) {
JsonValue value = get(name);
return value != null ? value.asInt() : defaultValue;
}
/**
* Returns the long
value of the member with the specified name in
* this object. If this object does not contain a member with this name, the
* given default value is returned. If this object contains multiple members
* with the given name, the last one will be picked. If this member's value does
* not represent a JSON number or if it cannot be interpreted as Java
* long
, an exception is thrown.
*
* @param name the name of the member whose value is to be returned
* @param defaultValue the value to be returned if the requested member is
* missing
* @return the value of the last member with the specified name, or the given
* default value if this object does not contain a member with that name
*/
public long getLong(String name, long defaultValue) {
JsonValue value = get(name);
return value != null ? value.asLong() : defaultValue;
}
/**
* Returns the float
value of the member with the specified name in
* this object. If this object does not contain a member with this name, the
* given default value is returned. If this object contains multiple members
* with the given name, the last one will be picked. If this member's value does
* not represent a JSON number or if it cannot be interpreted as Java
* float
, an exception is thrown.
*
* @param name the name of the member whose value is to be returned
* @param defaultValue the value to be returned if the requested member is
* missing
* @return the value of the last member with the specified name, or the given
* default value if this object does not contain a member with that name
*/
public float getFloat(String name, float defaultValue) {
JsonValue value = get(name);
return value != null ? value.asFloat() : defaultValue;
}
/**
* Returns the double
value of the member with the specified name
* in this object. If this object does not contain a member with this name, the
* given default value is returned. If this object contains multiple members
* with the given name, the last one will be picked. If this member's value does
* not represent a JSON number or if it cannot be interpreted as Java
* double
, an exception is thrown.
*
* @param name the name of the member whose value is to be returned
* @param defaultValue the value to be returned if the requested member is
* missing
* @return the value of the last member with the specified name, or the given
* default value if this object does not contain a member with that name
*/
public double getDouble(String name, double defaultValue) {
JsonValue value = get(name);
return value != null ? value.asDouble() : defaultValue;
}
/**
* Returns the boolean
value of the member with the specified name
* in this object. If this object does not contain a member with this name, the
* given default value is returned. If this object contains multiple members
* with the given name, the last one will be picked. If this member's value does
* not represent a JSON true
or false
value, an
* exception is thrown.
*
* @param name the name of the member whose value is to be returned
* @param defaultValue the value to be returned if the requested member is
* missing
* @return the value of the last member with the specified name, or the given
* default value if this object does not contain a member with that name
*/
public boolean getBoolean(String name, boolean defaultValue) {
JsonValue value = get(name);
return value != null ? value.asBoolean() : defaultValue;
}
/**
* Returns the String
value of the member with the specified name
* in this object. If this object does not contain a member with this name, the
* given default value is returned. If this object contains multiple members
* with the given name, the last one is picked. If this member's value does not
* represent a JSON string, an exception is thrown.
*
* @param name the name of the member whose value is to be returned
* @param defaultValue the value to be returned if the requested member is
* missing
* @return the value of the last member with the specified name, or the given
* default value if this object does not contain a member with that name
*/
public String getString(String name, String defaultValue) {
JsonValue value = get(name);
return value != null ? value.asString() : defaultValue;
}
/**
* Returns the number of members (name/value pairs) in this object.
*
* @return the number of members in this object
*/
public int size() {
return names.size();
}
/**
* Returns true
if this object contains no members.
*
* @return true
if this object contains no members
*/
public boolean isEmpty() {
return names.isEmpty();
}
/**
* Returns a list of the names in this object in document order. The returned
* list is backed by this object and will reflect subsequent changes. It cannot
* be used to modify this object. Attempts to modify the returned list will
* result in an exception.
*
* @return a list of the names in this object
*/
public List names() {
return Collections.unmodifiableList(names);
}
/**
* Returns an iterator over the members of this object in document order. The
* returned iterator cannot be used to modify this object.
*
* @return an iterator over the members of this object
*/
public Iterator iterator() {
final Iterator namesIterator = names.iterator();
final Iterator valuesIterator = values.iterator();
return new Iterator() {
public boolean hasNext() {
return namesIterator.hasNext();
}
public Member next() {
String name = namesIterator.next();
JsonValue value = valuesIterator.next();
return new Member(name, value);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
void write(JsonWriter writer) throws IOException {
writer.writeObjectOpen();
Iterator namesIterator = names.iterator();
Iterator valuesIterator = values.iterator();
if (namesIterator.hasNext()) {
writer.writeMemberName(namesIterator.next());
writer.writeMemberSeparator();
valuesIterator.next().write(writer);
while (namesIterator.hasNext()) {
writer.writeObjectSeparator();
writer.writeMemberName(namesIterator.next());
writer.writeMemberSeparator();
valuesIterator.next().write(writer);
}
}
writer.writeObjectClose();
}
@Override
public boolean isObject() {
return true;
}
@Override
public JsonObject asObject() {
return this;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + names.hashCode();
result = 31 * result + values.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
JsonObject other = (JsonObject) obj;
return names.equals(other.names) && values.equals(other.values);
}
int indexOf(String name) {
int index = table.get(name);
if (index != -1 && name.equals(names.get(index))) {
return index;
}
return names.lastIndexOf(name);
}
private synchronized void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
inputStream.defaultReadObject();
table = new HashIndexTable();
updateHashIndex();
}
private void updateHashIndex() {
int size = names.size();
for (int i = 0; i < size; i++) {
table.add(names.get(i), i);
}
}
/**
* Represents a member of a JSON object, a pair of a name and a value.
*/
public static class Member {
private final String name;
private final JsonValue value;
Member(String name, JsonValue value) {
this.name = name;
this.value = value;
}
/**
* Returns the name of this member.
*
* @return the name of this member, never null
*/
public String getName() {
return name;
}
/**
* Returns the value of this member.
*
* @return the value of this member, never null
*/
public JsonValue getValue() {
return value;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + name.hashCode();
result = 31 * result + value.hashCode();
return result;
}
/**
* Indicates whether a given object is "equal to" this JsonObject. An object is
* considered equal if it is also a JsonObject
and both objects
* contain the same members in the same order.
*
* If two JsonObjects are equal, they will also produce the same JSON output.
*
*
* @param object the object to be compared with this JsonObject
* @return true if the specified object is equal to this JsonObject,
* false
otherwise
*/
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null) {
return false;
}
if (getClass() != object.getClass()) {
return false;
}
Member other = (Member) object;
return name.equals(other.name) && value.equals(other.value);
}
}
static class HashIndexTable {
private final byte[] hashTable = new byte[32]; // must be a power of two
HashIndexTable() {
}
HashIndexTable(HashIndexTable original) {
System.arraycopy(original.hashTable, 0, hashTable, 0, hashTable.length);
}
void add(String name, int index) {
int slot = hashSlotFor(name);
if (index < 0xff) {
// increment by 1, 0 stands for empty
hashTable[slot] = (byte) (index + 1);
} else {
hashTable[slot] = 0;
}
}
void remove(int index) {
for (int i = 0; i < hashTable.length; i++) {
if ((hashTable[i] & 0xff) == index + 1) {
hashTable[i] = 0;
} else if ((hashTable[i] & 0xff) > index + 1) {
hashTable[i]--;
}
}
}
int get(Object name) {
int slot = hashSlotFor(name);
// subtract 1, 0 stands for empty
return (hashTable[slot] & 0xff) - 1;
}
private int hashSlotFor(Object element) {
return element.hashCode() & hashTable.length - 1;
}
}
}