equals和hashCode的通用反射式辅助方法 [英] Generic reflective helper method for equals and hashCode
本文介绍了equals和hashCode的通用反射式辅助方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在考虑为equals和hashCode创建一个反射式辅助方法.
I'm thinking about to create a reflective helper method for equals and hashCode.
- 如果等于,则辅助方法将反射API查找到objectA的字段,并将它们与objectB的字段进行比较.
- 在使用hashCode的情况下,helper方法会在反射API上查找字段,并在迭代循环中计算hashCode.
好处是,我不必担心我的equals或hashCode实现中缺少字段. 坏事是我想性能.您如何看待这个想法?请分享您的意见!
The good thing is that I don't to be worry about missing fields in my equals or hashCode implementation. The bad thing is I guess performance. What do you think about this idea? Please share your opinion!
这是我的初稿:
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);
}
}
}
干杯,凯文
推荐答案
看看 Apache Commons 中的builder/EqualsBuilder.html"rel =" nofollow> EqualsBuilder ,它是
Take a look at EqualsBuilder in Apache Commons and it's reflectionEquals
methods. This library also have a HashCodeBuilder + a lot of other useful stuff.
这篇关于equals和hashCode的通用反射式辅助方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文