Rabbit-R1/switch port/java/sources/com/google/gson/JsonPrimitive.java
2024-05-21 17:08:36 -04:00

169 lines
5.3 KiB
Java

package com.google.gson;
import com.google.gson.internal.C$Gson$Preconditions;
import com.google.gson.internal.LazilyParsedNumber;
import java.math.BigDecimal;
import java.math.BigInteger;
/* loaded from: classes3.dex */
public final class JsonPrimitive extends JsonElement {
private final Object value;
@Override // com.google.gson.JsonElement
public JsonPrimitive deepCopy() {
return this;
}
public JsonPrimitive(Boolean bool) {
this.value = C$Gson$Preconditions.checkNotNull(bool);
}
public JsonPrimitive(Number number) {
this.value = C$Gson$Preconditions.checkNotNull(number);
}
public JsonPrimitive(String str) {
this.value = C$Gson$Preconditions.checkNotNull(str);
}
public JsonPrimitive(Character ch) {
this.value = ((Character) C$Gson$Preconditions.checkNotNull(ch)).toString();
}
public boolean isBoolean() {
return this.value instanceof Boolean;
}
@Override // com.google.gson.JsonElement
public boolean getAsBoolean() {
if (isBoolean()) {
return ((Boolean) this.value).booleanValue();
}
return Boolean.parseBoolean(getAsString());
}
public boolean isNumber() {
return this.value instanceof Number;
}
@Override // com.google.gson.JsonElement
public Number getAsNumber() {
Object obj = this.value;
return obj instanceof String ? new LazilyParsedNumber((String) this.value) : (Number) obj;
}
public boolean isString() {
return this.value instanceof String;
}
@Override // com.google.gson.JsonElement
public String getAsString() {
if (isNumber()) {
return getAsNumber().toString();
}
if (isBoolean()) {
return ((Boolean) this.value).toString();
}
return (String) this.value;
}
@Override // com.google.gson.JsonElement
public double getAsDouble() {
return isNumber() ? getAsNumber().doubleValue() : Double.parseDouble(getAsString());
}
@Override // com.google.gson.JsonElement
public BigDecimal getAsBigDecimal() {
Object obj = this.value;
return obj instanceof BigDecimal ? (BigDecimal) obj : new BigDecimal(this.value.toString());
}
@Override // com.google.gson.JsonElement
public BigInteger getAsBigInteger() {
Object obj = this.value;
return obj instanceof BigInteger ? (BigInteger) obj : new BigInteger(this.value.toString());
}
@Override // com.google.gson.JsonElement
public float getAsFloat() {
return isNumber() ? getAsNumber().floatValue() : Float.parseFloat(getAsString());
}
@Override // com.google.gson.JsonElement
public long getAsLong() {
return isNumber() ? getAsNumber().longValue() : Long.parseLong(getAsString());
}
@Override // com.google.gson.JsonElement
public short getAsShort() {
return isNumber() ? getAsNumber().shortValue() : Short.parseShort(getAsString());
}
@Override // com.google.gson.JsonElement
public int getAsInt() {
return isNumber() ? getAsNumber().intValue() : Integer.parseInt(getAsString());
}
@Override // com.google.gson.JsonElement
public byte getAsByte() {
return isNumber() ? getAsNumber().byteValue() : Byte.parseByte(getAsString());
}
@Override // com.google.gson.JsonElement
public char getAsCharacter() {
return getAsString().charAt(0);
}
public int hashCode() {
long doubleToLongBits;
if (this.value == null) {
return 31;
}
if (isIntegral(this)) {
doubleToLongBits = getAsNumber().longValue();
} else {
Object obj = this.value;
if (obj instanceof Number) {
doubleToLongBits = Double.doubleToLongBits(getAsNumber().doubleValue());
} else {
return obj.hashCode();
}
}
return (int) ((doubleToLongBits >>> 32) ^ doubleToLongBits);
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
JsonPrimitive jsonPrimitive = (JsonPrimitive) obj;
if (this.value == null) {
return jsonPrimitive.value == null;
}
if (isIntegral(this) && isIntegral(jsonPrimitive)) {
return getAsNumber().longValue() == jsonPrimitive.getAsNumber().longValue();
}
Object obj2 = this.value;
if ((obj2 instanceof Number) && (jsonPrimitive.value instanceof Number)) {
double doubleValue = getAsNumber().doubleValue();
double doubleValue2 = jsonPrimitive.getAsNumber().doubleValue();
if (doubleValue != doubleValue2) {
return Double.isNaN(doubleValue) && Double.isNaN(doubleValue2);
}
return true;
}
return obj2.equals(jsonPrimitive.value);
}
private static boolean isIntegral(JsonPrimitive jsonPrimitive) {
Object obj = jsonPrimitive.value;
if (!(obj instanceof Number)) {
return false;
}
Number number = (Number) obj;
return (number instanceof BigInteger) || (number instanceof Long) || (number instanceof Integer) || (number instanceof Short) || (number instanceof Byte);
}
}