TypedValue implements equals/hashCode based on value and TypeDescriptor

Issue: SPR-11960
This commit is contained in:
Juergen Hoeller 2014-07-07 11:29:29 +02:00
parent 11532794f4
commit a8848cb670
2 changed files with 37 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* 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.
@ -17,6 +17,7 @@
package org.springframework.expression; package org.springframework.expression;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.ObjectUtils;
/** /**
* Encapsulates an object and a type descriptor that describes it. * Encapsulates an object and a type descriptor that describes it.
@ -63,18 +64,35 @@ public class TypedValue {
} }
public TypeDescriptor getTypeDescriptor() { public TypeDescriptor getTypeDescriptor() {
if (this.typeDescriptor == null) { if (this.typeDescriptor == null && this.value != null) {
this.typeDescriptor = TypeDescriptor.forObject(this.value); this.typeDescriptor = TypeDescriptor.forObject(this.value);
} }
return this.typeDescriptor; return this.typeDescriptor;
} }
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof TypedValue)) {
return false;
}
TypedValue otherTv = (TypedValue) other;
return (ObjectUtils.nullSafeEquals(this.value, otherTv.value) &&
((this.typeDescriptor == null && otherTv.typeDescriptor == null) ||
getTypeDescriptor().equals(otherTv.getTypeDescriptor())));
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.value);
}
@Override @Override
public String toString() { public String toString() {
StringBuilder str = new StringBuilder(); return "TypedValue: '" + this.value + "' of [" + getTypeDescriptor() + "]";
str.append("TypedValue: '").append(this.value).append("' of [").append(getTypeDescriptor()).append("]");
return str.toString();
} }
} }

View File

@ -97,9 +97,20 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
@Test @Test
public void testTypedValue() { public void testTypedValue() {
TypedValue tValue = new TypedValue("hello"); TypedValue tv1 = new TypedValue("hello");
assertEquals(String.class,tValue.getTypeDescriptor().getType()); TypedValue tv2 = new TypedValue("hello");
assertEquals("TypedValue: 'hello' of [java.lang.String]",tValue.toString()); TypedValue tv3 = new TypedValue("bye");
assertEquals(String.class, tv1.getTypeDescriptor().getType());
assertEquals("TypedValue: 'hello' of [java.lang.String]", tv1.toString());
assertEquals(tv1, tv2);
assertEquals(tv2, tv1);
assertNotEquals(tv1, tv3);
assertNotEquals(tv2, tv3);
assertNotEquals(tv3, tv1);
assertNotEquals(tv3, tv2);
assertEquals(tv1.hashCode(), tv2.hashCode());
assertNotEquals(tv1.hashCode(), tv3.hashCode());
assertNotEquals(tv2.hashCode(), tv3.hashCode());
} }
@Test @Test