`
talentkep
  • 浏览: 97919 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

任意2个对象的比较 Object Compare

J# 
阅读更多
直接上源码:

先来测试用的例子类:
public class A {

int count;
private String name;
private A aa = null;
public A(int count){
this.count=count;
}
public A getAa() {
return aa;
}

public void setAa(A aa) {
this.aa = aa;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public A(int count,String name){
this.count=count;
this.name=name;
}

public boolean equals(Object obj){
if (obj == this){
return true;
}
if (obj!=null && obj.getClass() == A.class){
A a = (A)obj;
if (this.count == a.count){
return true;
}
}
return false;
}

public int hashCode(){
return this.count;
}
}

再来Main函数
public static void main(String[] args) {
showNameAndValue(name, value);

System.out.println("===test object compare===");
String test1 = "Peter_Keasfasdf";
String test2 = "Peter_Keasfasdf";
Double int1 = new Double(10123.25);
Double int2 = new Double(20);
Date date1= new Date();
Calendar cal1 = Calendar.getInstance();
//cal1.add(Calendar.SECOND,2);
Calendar cal2 = Calendar.getInstance();
A b1 = new A(30,"Hello_world");
A b2 = new A(40,"Hello_World");
A a1= new A(10,"Peter_Ke");
A a2= new A(10,"Peter_Ke");
a1.setAa(b1);
a2.setAa(b2);

List l1 = new ArrayList();
List l2 = new ArrayList();
l1.add("aaa");
l1.add("bbb");
l1.add("CCC");
l2.add("aaa");
l2.add("bbb");

Date date2 = new Date(cal1.getTimeInMillis());
try {
//SimpleObjectCompare(test1,test2);
if (ObjectCompare(a1,a2)) System.out.println("---final test is equal");
else System.out.println("---final test is diff");
} catch (Exception e1) {
e1.printStackTrace();
}
}//end of main

重点来了 ObjectCompare

public static void DiffHandleAction(Object oldObj, Object newObj,Object oldObjField, Object newObjField, String fieldName,String type) {
System.out.println("---DiffHandleAction");
if (type == "simple") {
System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldObj);
System.out.println("newObj is: "+newObj);
}
if (type == "complex"){
//System.out.println("oldObj is: "+oldObj);
//System.out.println("newObj is: "+newObj);
System.out.println("oldObj and newObj aren't equal on field: "+fieldName);
System.out.println("oldObj on: "+fieldName+" value is: "+oldObjField);
System.out.println("newObj on: "+fieldName+" value is: "+newObjField);
}
if (type == "NotMatchType"){
//System.out.println("oldObj is: "+oldObj);
//System.out.println("newObj is: "+newObj);
System.out.println("oldObj and newObj aren not same class !");

}
System.out.println("---end DiffHandleAction");
}

public static boolean ObjectCompare (Object oldObj,Object newObj)throws Exception{
Class clazz = newObj.getClass();
boolean equalflag = true;
if (!oldObj.getClass().isInstance(newObj)){//if oldobj not the class newObj
//System.out.println("oldObj and newObj aren't same Class!");
DiffHandleAction(oldObj,newObj,null,null,null,"NotMatchType");
return false;
}
else{
//is simple object ?
if (oldObj instanceof Number || oldObj instanceof Collection || oldObj instanceof Boolean
|| oldObj instanceof String ||oldObj instanceof Date || oldObj instanceof Calendar){
System.out.println("oldObj and newObj are same Simple Objcet Class! Type is:" + oldObj.getClass().getName());
if (!SimpleObjectCompare(oldObj,newObj)){
/*System.out.println("oldObj and newObj aren't equal Simple Objcet:");
System.out.println("oldObj is: "+oldObj);
System.out.println("newObj is: "+newObj);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}
//not simple object
else{
System.out.println("oldObj and newObj are complex Class!");
Field[] fields = oldObj.getClass().getDeclaredFields();
Object[] oldValue = new Object[fields.length];
Object[] newValue = new Object[fields.length];
String[] fieldNameArr = new String[fields.length];
//get object value if it accessable
for (int i = 0; i < fields.length; i++) {
fieldNameArr[i] = fields[i].getName();
System.out.println("check field:" + fieldNameArr[i]);
//try get field value
try {
oldValue[i] = fields[i].get(oldObj);
newValue[i] = fields[i].get(newObj);
} catch (IllegalAccessException e) {
System.out.println(fields[i].getName()+ " inaccessable but we try public get method");
//try get method
try {
Method[] methods = oldObj.getClass().getDeclaredMethods();
boolean found = false;
for (int j = 0; j < methods.length; j++) {
Method method = methods[j];
//find the get method
StringBuffer getMethodName = new StringBuffer("get");
getMethodName = getMethodName.append(fieldNameArr[i].substring(0,1).toUpperCase());
getMethodName = getMethodName.append(fieldNameArr[i].substring(1));
if (method.getName().compareTo(getMethodName.toString())==0){
found = true;
System.out.println("method.getName(): "+method.getName());
oldValue[i] = method.invoke(oldObj,null);
newValue[i] = method.invoke(newObj,null);
}
}
if (!found) System.out.println("No found get method! skip this field!");
} catch (Exception e1) {
System.out.println("Still can't get field value,skip this field!");
continue;
}
}//end try get field value

//String field
if (oldValue[i] instanceof String){
String oldString = (String) oldValue[i];
String newString = (String) newValue[i];
//not equal
if (!(oldString.compareTo(newString)==0)){
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" value is: "+oldString);
System.out.println("newObj on: "+fieldNameArr[i]+" value is: "+newString);*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}
else{
//equal on this field
continue;
}
}
//end String field

//numble field
if (oldValue[i] instanceof Number){
Number oldNum = (Number) oldValue[i];
Number newNum = (Number) newValue[i];
//not equal
if (oldNum.doubleValue() != newNum.doubleValue()){
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" value is: "+oldNum);
System.out.println("newObj on: "+fieldNameArr[i]+" value is: "+newNum);*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}
else{
// equal on this field
continue;
}
}//end numble field

//Date field
if (oldValue[i] instanceof Date){
Date oldDate = (Date) oldValue[i];
Date newDate = (Date) newValue[i];
//not equal
if (oldDate.compareTo(newDate)!=0){
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" value is: "+oldDate);
System.out.println("newObj on: "+fieldNameArr[i]+" value is: "+newDate);*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}
else{
// equal on this field
continue;
}
}//end Date field

// Calendar field
if (oldValue[i] instanceof Calendar){
System.out.println("oldObj and newObj are Calendar!");
Calendar oldCalendar = (Calendar) oldValue[i];
Calendar newCalendar = (Calendar) newValue[i];
//not equal
if (newCalendar.getTime().compareTo(oldCalendar.getTime())==0){
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" value is: "+oldCalendar);
System.out.println("newObj on: "+fieldNameArr[i]+" value is: "+newCalendar);*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}
else{
// equal on this field
continue;
}
}

//Boolean field
if (oldValue[i] instanceof Boolean){
Boolean oldBoolean = (Boolean) oldValue[i];
Boolean newBoolean = (Boolean) newValue[i];
//not equal
if (oldBoolean == newBoolean){
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" value is: "+oldBoolean);
System.out.println("newObj on: "+fieldNameArr[i]+" value is: "+newBoolean);*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}
else{
// equal on this field
continue;
}
}//end Boolean field

//Collection field
if (oldValue[i] instanceof Collection){
Collection oldCollection = (Collection) oldValue[i];
Collection newCollection = (Collection) newValue[i];

//not equal size
if (oldCollection.size()!= newCollection.size()){
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" size is: "+oldCollection.size());
System.out.println("newObj on: "+fieldNameArr[i]+" size is: "+newCollection.size());*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}

//equal size
if (oldCollection.size()== newCollection.size()){
//best way
if (oldCollection.containsAll(newCollection) && newCollection.containsAll(oldCollection)){
//Collection equeal
continue;
}
else{
/*System.out.println("oldObj and newObj aren't equal on field: "+fieldNameArr[i]);
System.out.println("oldObj on: "+fieldNameArr[i]+" value is: "+oldCollection);
System.out.println("newObj on: "+fieldNameArr[i]+" value is: "+newCollection);*/
DiffHandleAction(oldObj,newObj,oldValue[i],newValue[i],fieldNameArr[i],"complex");
equalflag = false;
continue;
}

//one way
/*Object[] newObjectArray = oldCollection.toArray();
Object[] oldObjectArray = newCollection.toArray();
for (int k = 0; k < oldObjectArray.length; k++) {
Object oldObjectElement = oldObjectArray[k];
Object newObjectElement = newObjectArray[k];
ObjectCompare(oldObjectElement,newObjectElement);
}*/

//Abandon method
/*Object[] newObjectArray = new Object[newCollection.size()];
Object[] oldObjectArray = new Object[oldCollection.size()];
int j=0;
for (Iterator iter = newCollection.iterator(); iter.hasNext();) {
Object element = (Object) iter.next();
newObjectArray[j] = element;
j++;
}
j=0;
for (Iterator iter = oldCollection.iterator(); iter.hasNext();) {
Object element = (Object) iter.next();
oldObjectArray[j] = element;
j++;
}

for (int k = 0; k < oldObjectArray.length; k++) {
Object oldObjectElement = oldObjectArray[k];
Object newObjectElement = newObjectArray[k];
ObjectCompare(oldObjectElement,newObjectElement);
}*/
}//end not equal size
}//end Collection field

//other Object field not String,not Number,not Collection,not Date,not Boolean
if (oldValue[i] instanceof Object){
//iterative
if (fieldNameArr[i].equalsIgnoreCase("class$0")) continue;
System.out.println("====iterative object here!!!! fieldName: "+fieldNameArr[i]);
Object oldObject= (Object) oldValue[i];
Object newObject = (Object) newValue[i];
if (!ObjectCompare (oldObject,newObject))equalflag = false;
System.out.println("====iterative object here!!!! fieldName: "+fieldNameArr[i]+" is:"+equalflag);
}//end other Object type

}//end for fields check
}//end not simple object judge
}//end same class type judge
return equalflag;
}// end of ObjectCompare method

public static boolean SimpleObjectCompare (Object oldObj,Object newObj)throws Exception{
if (!oldObj.getClass().isInstance(newObj)){//if oldobj not the class
   // newObj
//System.out.println("oldObj and newObj aren't same Class!");
DiffHandleAction(oldObj,newObj,null,null,null,"NotMatchType");
return false;
}
else{
//String Object
if (oldObj instanceof String){
System.out.println("oldObj and newObj are String!");
String oldString = (String) oldObj;
String newString = (String) newObj;
//not equal
if (!oldString.equalsIgnoreCase(newString)){
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldString);
System.out.println("newObj is: "+newString);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}

//numble field
if (oldObj instanceof Number){
System.out.println("oldObj and newObj are Number!");
Number oldNum = (Number) oldObj;
Number newNum = (Number) newObj;
//not equal
if (oldNum.doubleValue() != newNum.doubleValue()){
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldNum);
System.out.println("newObj is: "+newNum);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}

//Date field
if (oldObj instanceof Date){
System.out.println("oldObj and newObj are Date!");
Date oldDate = (Date) oldObj;
Date newDate = (Date) newObj;
//not equal
if (oldDate.compareTo(newDate)!=0){
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldDate);
System.out.println("newObj is: "+newDate);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}

//Calendar field
if (oldObj instanceof Calendar){
System.out.println("oldObj and newObj are Calendar!");
Calendar oldCalendar = (Calendar) oldObj;
Calendar newCalendar = (Calendar) newObj;
//not equal
if (newCalendar.getTime().compareTo(oldCalendar.getTime())!=0){
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldCalendar);
System.out.println("newObj is: "+newCalendar);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}

//Boolean field
if (oldObj instanceof Boolean){
System.out.println("oldObj and newObj are Boolean!");
Boolean oldBoolean = (Boolean) oldObj;
Boolean newBoolean = (Boolean) newObj;
//not equal
if (oldBoolean == newBoolean){
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldBoolean);
System.out.println("newObj is: "+newBoolean);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}

//Collection field
if (oldObj instanceof Collection){
System.out.println("oldObj and newObj aren Collection!");
Collection oldCollection = (Collection) oldObj;
Collection newCollection = (Collection) newObj;

//not equal size
if (oldCollection.size()!= newCollection.size()){
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+" size is: "+oldCollection.size());
System.out.println("newObj is: "+" size is: "+newCollection.size());*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}

//equal size
if (oldCollection.size()== newCollection.size()){
//best way
if (oldCollection.containsAll(newCollection) && newCollection.containsAll(oldCollection)){
//Collection equeal
return true;
}
else{
/*System.out.println("oldObj and newObj aren't equal: ");
System.out.println("oldObj is: "+oldCollection);
System.out.println("newObj is: "+newCollection);*/
DiffHandleAction(oldObj,newObj,null,null,null,"simple");
return false;
}
}
}
}
return true;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics