Implemented equals and hashcode for LazilyParsedNumber to fix
https://github.com/google/gson/issues/627
This commit is contained in:
parent
93605e7145
commit
3daf585931
@ -1,78 +1,100 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2011 Google Inc.
|
* Copyright (C) 2011 Google Inc.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.google.gson.internal;
|
package com.google.gson.internal;
|
||||||
|
|
||||||
import java.io.ObjectStreamException;
|
import java.io.ObjectStreamException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class holds a number value that is lazily converted to a specific number type
|
* This class holds a number value that is lazily converted to a specific number type
|
||||||
*
|
*
|
||||||
* @author Inderjeet Singh
|
* @author Inderjeet Singh
|
||||||
*/
|
*/
|
||||||
public final class LazilyParsedNumber extends Number {
|
public final class LazilyParsedNumber extends Number {
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
public LazilyParsedNumber(String value) {
|
public LazilyParsedNumber(String value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int intValue() {
|
public int intValue() {
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(value);
|
return Integer.parseInt(value);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
try {
|
try {
|
||||||
return (int) Long.parseLong(value);
|
return (int) Long.parseLong(value);
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
return new BigDecimal(value).intValue();
|
return new BigDecimal(value).intValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long longValue() {
|
public long longValue() {
|
||||||
try {
|
try {
|
||||||
return Long.parseLong(value);
|
return Long.parseLong(value);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return new BigDecimal(value).longValue();
|
return new BigDecimal(value).longValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float floatValue() {
|
public float floatValue() {
|
||||||
return Float.parseFloat(value);
|
return Float.parseFloat(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double doubleValue() {
|
public double doubleValue() {
|
||||||
return Double.parseDouble(value);
|
return Double.parseDouble(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If somebody is unlucky enough to have to serialize one of these, serialize
|
* If somebody is unlucky enough to have to serialize one of these, serialize
|
||||||
* it as a BigDecimal so that they won't need Gson on the other side to
|
* it as a BigDecimal so that they won't need Gson on the other side to
|
||||||
* deserialize it.
|
* deserialize it.
|
||||||
*/
|
*/
|
||||||
private Object writeReplace() throws ObjectStreamException {
|
private Object writeReplace() throws ObjectStreamException {
|
||||||
return new BigDecimal(value);
|
return new BigDecimal(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return value == null ? 0 : value.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null || !(obj instanceof LazilyParsedNumber)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
LazilyParsedNumber other = (LazilyParsedNumber) obj;
|
||||||
|
return equals(value, other.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean equals(Object obj1, Object obj2) {
|
||||||
|
if (obj1 == null) return obj2 == null;
|
||||||
|
return obj1 == obj2 || obj1.equals(obj2);
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.google.gson.internal;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
public class LazilyParsedNumberTest extends TestCase {
|
||||||
|
public void testHashCode(){
|
||||||
|
LazilyParsedNumber n1 = new LazilyParsedNumber("1");
|
||||||
|
LazilyParsedNumber n1Another = new LazilyParsedNumber("1");
|
||||||
|
assertEquals(n1.hashCode(), n1Another.hashCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testEquals(){
|
||||||
|
LazilyParsedNumber n1 = new LazilyParsedNumber("1");
|
||||||
|
LazilyParsedNumber n1Another = new LazilyParsedNumber("1");
|
||||||
|
assertTrue(n1.equals(n1Another));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user