| 1 | /* |
| 2 | * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.json; |
| 18 | |
| 19 | import java.io.Serializable; |
| 20 | |
| 21 | /** |
| 22 | * Private implementation of {@link JsonValue} for simple {@link ValueType}s |
| 23 | * allowing their usage in constants which are better to implement {@link Serializable}. |
| 24 | * |
| 25 | * @author Lukas Jungmann |
| 26 | */ |
| 27 | final class JsonValueImpl implements JsonValue, Serializable { |
| 28 | |
| 29 | /** for serialization */ |
| 30 | private static final long serialVersionUID = 83723433120886104L; |
| 31 | |
| 32 | /** Type of this JsonValue. */ |
| 33 | private final ValueType valueType; |
| 34 | |
| 35 | /** |
| 36 | * Default constructor. |
| 37 | * @param valueType Type of this JsonValue |
| 38 | */ |
| 39 | JsonValueImpl(ValueType valueType) { |
| 40 | this.valueType = valueType; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the value type of this JSON value. |
| 45 | * |
| 46 | * @return JSON value type |
| 47 | */ |
| 48 | @Override |
| 49 | public ValueType getValueType() { |
| 50 | return valueType; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Compares the specified object with this {@link JsonValue} |
| 55 | * object for equality. Returns {@code true} if and only if the |
| 56 | * specified object is also a JsonValue, and their |
| 57 | * {@link #getValueType()} objects are <i>equal</i>. |
| 58 | * |
| 59 | * @param obj the object to be compared for equality with this JsonValue |
| 60 | * @return {@code true} if the specified object is equal to this |
| 61 | * JsonValue |
| 62 | */ |
| 63 | @Override |
| 64 | public boolean equals(Object obj) { |
| 65 | if (this == obj) { |
| 66 | return true; |
| 67 | } |
| 68 | if (obj instanceof JsonValue) { |
| 69 | return getValueType().equals(((JsonValue) obj).getValueType()); |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the hash code value for this {@link JsonValue} object. |
| 76 | * The hash code of the {@link JsonValue} object is defined to be |
| 77 | * its {@link #getValueType()} object's hash code. |
| 78 | * |
| 79 | * @return the hash code value for this {@link JsonValue} object |
| 80 | */ |
| 81 | @Override |
| 82 | public int hashCode() { |
| 83 | return valueType.hashCode(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String toString() { |
| 88 | return valueType.name().toLowerCase(); |
| 89 | } |
| 90 | |
| 91 | } |