我遇到了一个问题,以更新现有的数据与任何更改的用户。
例如,我有一个Company对象,如下所示
public class Company {
private String id;
private String name;
private String url;
//Getter and setter.....
}现在我有一个公司实例,数据如下所示
Company company = new Company();
company.setId("id12345678");
company.setName("company Old Name");
company.setUrl("company Old Url");现在,用户已经创建了另一个公司实例,以更新现有name和url的Company。
Company newCompany = new Company();
newCompany.setId("id12345678");
newCompany.setName("company New Name");
newCompany.setUrl("company New Url");可以看到,Company的新实例具有与旧实例相同的Id,但具有不同的name和url。
是否有任何实用工具/库可以简单地查找这两个对象之间的所有差异并将更改应用到company实例?(并不是真的想编写代码来手动比较每个字段)
有点像
company = AwesomeLibraryUtils.compareAndApplyDifferences(company, newCompany);在此之后,company实例保持相同的id,但使用来自newCompany实例的name和url的所有新值。
谢谢。
发布于 2015-09-28 07:33:00
没有真正的实用程序能够“找到并应用”这些差异(据我所知),但是也有反射(正如其他人所说的),比如Apache比较器(这是一个强大的类)。
至于“应用差异”,Apache (这次是BeanUtils)中也有一个工具:http://commons.apache.org/proper/commons-beanutils/,即"copyProperties(Object o1, Object o2)“wihchi将所有属性(字段)从一个对象复制到另一个对象。
因此,您只需比较这些对象,如果它们是不同的,将其中一个复制到另一个。
除了性能问题之外,我不明白为什么您只复制不同的字段(因为如果您复制相同的字段,那么就不会有任何不同)。如果您只需要复制不同的字段,我猜您将不得不自己制作工具。
发布于 2015-09-28 06:47:19
使用results查找公司和newCompany中的getter和setter的所有结果。
基本上,我在这里所做的是:
public static void compareAndApplyDifferences(Object o1, Object o2) {
if(!o1.getClass().equals(o2.getClass())) {
return;
}
// check if the result of getters in o1 and o2 are different
boolean isDiff = false;
Class<? extends Object> c1 = o1.getClass();
Class<? extends Object> c2 = o2.getClass();
Method[] methods1 = c1.getMethods();
Method[] methods2 = c2.getMethods();
Map<String, Object> hm = new HashMap<String, Object>();
for (Method method2 : methods2) {
if (isGetter(method2)) {
try {
Object getterResult = method2.invoke(o2);
hm.put(method2.getName().substring(3), getterResult);
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}
for (Method method1 : methods1) {
if (isGetter(method1)) {
try {
String getterResult = (String) method1.invoke(o1);
if (!hm.containsValue(getterResult)) {
isDiff = true;
break;
}
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}
if (isDiff) {
// set the values in o1 as the values in o2
for (Method method1 : methods1) {
if (isSetter(method1)) {
try {
method1.invoke(o1, hm.get(method1.getName().substring(3)));
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}
} else {
return;
}
}
private static boolean isGetter(Method method) {
if (!method.getName().startsWith("get"))
return false;
if (method.getParameterTypes().length != 0)
return false;
if (void.class.equals(method.getReturnType()))
return false;
return true;
}
private static boolean isSetter(Method method) {
if (!method.getName().startsWith("set"))
return false;
if (method.getParameterTypes().length != 1)
return false;
return true;
}下面是我的节目演示:
public static void main(String[] args) {
Company company = new Company();
company.setId("id12345678");
company.setName("company Old Name");
company.setUrl("company Old Url");
Company newCompany = new Company();
newCompany.setId("id12345678");
newCompany.setName("company New Name");
newCompany.setUrl("company New Url");
AwesomeLibraryUtils.compareAndApplyDifferences(company, newCompany);
System.out.println(company.getId()); // id12345678
System.out.println(company.getName()); // company New Name
System.out.println(company.getUrl()); // company New Url
}发布于 2015-09-28 05:30:50
您应该重写equals()方法。之后,您将能够检查条件company.equals(newCompany)。
著名的IDEs支持自动生成相等的方法.例如,IDEA将其生成为:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Company company = (Company) o;
if (id != null ? !id.equals(company.id) : company.id != null) return false;
if (name != null ? !name.equals(company.name) : company.name != null) return false;
return !(url != null ? !url.equals(company.url) : company.url != null);
}https://stackoverflow.com/questions/32816189
复制相似问题