我正在考慮爲equals和hashCode創建反射助手方法。equals和hashCode的泛型反射助手方法
- 在等號的情況下,輔助方法將反射API查找objectA的字段並將它們與objectB的字段進行比較。
- 在hashCode的情況下,幫助器方法將反射API查找字段並在迭代循環中計算hashCode。
好的是我不必擔心在我的equals或hashCode實現中缺少字段。 壞事是我猜測性能。你對這個想法有什麼看法?請分享你的意見!
這是我第一次平等草案:
public final class ReflectiveEqualsHelper {
public static boolean isEqual(final Object a, final Object b) {
if (!isTypeEqual(a, b)) {
return false;
}
Field[] fields = getFields(a);
Object valueA;
Object valueB;
String fieldName;
for (int i = 0; i < fields.length; i++) {
fieldName = fields[i].getName();
valueA = getValueByFieldName(a, fieldName);
valueB = getValueByFieldName(b, fieldName);
if (!compare(valueA, valueB)) {
return false;
}
}
return true;
}
@SuppressWarnings("rawtypes")
private static Field[] getFields(final Object o) {
Class clazz = o.getClass();
Field[] fields = clazz.getDeclaredFields();
return fields;
}
private static Field getField(final Object o, final String name) {
try {
Field field = o.getClass().getDeclaredField(name);
return field;
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
private static Object getValueByFieldName(final Object o, final String name) {
Field field = getField(o, name);
field.setAccessible(true);
try {
Object value = field.get(o);
field.setAccessible(false);
return value;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static boolean areBothNull(final Object a, final Object b) {
return (a == null && b == null);
}
private static boolean isTypeEqual(final Object a, final Object b) {
if (areBothNull(a, b)) {
return false;
}
return a.getClass().equals(b.getClass());
}
private static boolean compare(final Object a, final Object b) {
if (a == null) {
return false;
} else if (b == null) {
return false;
}
return a.equals(b);
}
}
public class ReflectiveEqualsHelperTest {
@Test
public void testIsEqual() {
Vector a = new Vector(Long.valueOf(1L), 3L);
Vector b = new Vector(Long.valueOf(1L), 3L);
Vector c = new Vector(Long.valueOf(2L), 3L);
boolean testA = ReflectiveEqualsHelper.isEqual(a, b);
boolean testB = ReflectiveEqualsHelper.isEqual(a, c);
boolean testC = ReflectiveEqualsHelper.isEqual(b, c);
assertTrue(testA);
assertFalse(testB);
assertFalse(testC);
}
class Vector {
public static final int STATIC = 1;
private Long x;
private long y;
public Vector(Long x, long y) {
super();
this.x = x;
this.y = y;
}
public Long getX() {
return x;
}
public void setX(Long x) {
this.x = x;
}
public long getY() {
return y;
}
public void setY(long y) {
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((x == null) ? 0 : x.hashCode());
result = prime * result + (int) (y^(y >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
return ReflectiveEqualsHelper.isEqual(this, obj);
}
}
}
歡呼聲中,凱文
我這樣做與日食和它對我工作正常我只是想更靈活的方式。 – eglobetrotter 2010-12-11 14:03:53
巨大的PITA *嘗試*,並記住每次添加或刪除字段時重新生成這些方法。通常情況下,人們會忘記導致微妙,難以發現的錯誤。你當然是對付反射解決方案的代價。 – 2013-01-15 18:41:14