- 浏览: 97919 次
- 性别:
- 来自: 武汉
最新评论
-
zljerityzljerity:
<#assign ipage=page?number&g ...
freeMark全解 -
qiankai86:
...
freeMark全解
直接上源码:
先来测试用的例子类:
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;
}
先来测试用的例子类:
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;
}
发表评论
-
complex sql search
2013-12-19 15:58 916Declare sqlStr_part1 varcha ... -
jxl 例子
2009-11-05 17:13 1114package chb.util; ... -
freeMark全解
2009-11-05 17:10 15592(1)模板 + 数据模型 = 输出 FreeMarker基 ... -
js 日期函数
2009-11-05 17:04 965Date.prototype.isLeapYear 判断闰年 ... -
Valid util
2009-09-25 14:58 1370/* * $Id: UtilValidate.java,v ... -
jfree chart test
2009-09-25 14:55 1715/* * Created on 2009-9-7 * ... -
order set
2009-09-25 14:49 918/* * $Id: OrderedMap.java,v 1 ... -
order map
2009-09-25 14:48 1097/* * $Id: OrderedMap.java,v 1 ... -
time util
2009-09-25 14:47 1115给自己的一些工具类关于时间的判断和空对象判断 /* * ...
相关推荐
使用此工具类,对于Object和byte[] 数组, 16进制和String之间的转换
面向对象 Object Thinking pdf 英文
第一册:《Head First Java》是本完整的面向对象(object-oriented,OO)程序设计和Java的学习指导。 第二册:《HeadFirst设计模式》(中文版)共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计...
the full 3D shape and pose of all object instances in the image. Our method produces a compact 3D representation of the scene, which can be readily used for applications like autonomous driving. Many ...
javascript 高级 对象 object js
如下所示: 代码如下: var person...上面的例子创建了一个名为person的对象,并为它添加了三个属性(name、age和job)和一个方法(sayName())。其中,sayName()方法用于显示this.name()的值。早期的JavaScript开发人员
object c 语言面向对象编程教程, 描述了object c语言是如何实现面向编程的
Java代码Map转Object对象
遍历任何xml成为一个对象object的demo,可以将任何的xml对象化
FunctionObject_函数对象
jQuery EasyUI 1.2.6 解决无法遮罩object对象的问题,在代码第1862行增加iframe对象遮罩object对象。
public int compare(Object o1, Object o2) { if (o1 instanceof String) { return compare( (String) o1, (String) o2); } else if (o1 instanceof Integer) { return compare( (Integer) o1, (Integer) o2);...
iOS对象转字典 object2Dictionary 从项目中提取出来的,对象转为字典,对象转为json;
实现WCF支持HTTP请求调用容易,但要实现类似MVC的ACTION及WEB API那样的灵活,那就得花费点功夫,为什么这样说呢?因为如果WCF的参数为普通类型(即:值类型),那么调用很容易,也支持HTTP的多种请求方法,比如常见...
自己实现的一个Android中将任意实体类对象转为Json的工具类。适用于Android开发。
ActiveObject 对象 Msxml2.DOMDocument 详解 asp.net ajax webservice 必懂的一点知识
本文实例讲述了JavaScript创建一个object对象并操作对象属性的用法。分享给大家供大家参考。具体分析如下: 下面的JS代码创建了一个myMovie对象,并给对象title和director属性赋值,通过Object定义对象,其属性可以...
转换为对象 xmlToObject.vo.Request(自定义) 里面还包含其他对象。 转成的对象 已JSON格式后台输出 {"body":{"class":"xmlToObject.vo.ReqBody","list":[{"class":"xmlToObject.vo.Foot","name":"红豆","rmb":"10...
本文讲到的对象,并非面向对象中的“对象”,而是指 Kotlin 中用 object 关键字声明的结构,它的功能相当强大,既可用来声明单例类,也可包装数据,还能充当匿名内部类。
【OMT介绍】 OMT(object modeling techniques)简单描述 对象模型:描述对象、类及其相互之间关系的静态数据结构;...Java编程 OMT(object modeling techniques)对象 动态 功能 10 部署图(共16页).ppt