*****************ObjectComparator.java***************
package com.comparators.test_objcompartor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
/**
* Hello world!
*
*/
public class ObjectComparator{
@SuppressWarnings("rawtypes")
public static void main(String args[]){
ClassA a1Obj= new ClassA();
ClassA a2Obj= new ClassA();
List diffList = new ArrayList();
ObjectComparator bkp = new ObjectComparator();
a2Obj.setXint(13);
a2Obj.setStr("Crazy");
a1Obj.bObj.setP(16);
a2Obj.bObj.setP(19);
System.out.println("\n **Job "+ bkp.compare(a1Obj, a2Obj, diffList));
}
@SuppressWarnings("unchecked")
public String compare(Object a1Obj,Object a2Obj, List diffList){
Object nextA = null;
Object nextB = null;
boolean flag = false;
Field field2 = null;
try{
for(Field field1:a1Obj.getClass().getDeclaredFields()){
field2 = a2Obj.getClass().getDeclaredField(field1.getName());
System.out.println(" field name is "+field1.getName());
if(field1.getAnnotation(Embedded.class) instanceof Embedded ){
nextA = PropertyUtils.getProperty(a1Obj, field1.getName());
nextB = PropertyUtils.getProperty(a2Obj, field2.getName());
flag = true;
continue;
}
if(field1.getType() == boolean.class){
if(field1.getBoolean(a1Obj) == field2.getBoolean(a2Obj)){
continue;
}else{
diffList.add(" In "+a1Obj.getClass()+ " field of diff "+field1.getName());
diffList.add(" Value in first : "+field1.getBoolean(a1Obj));
diffList.add(" Value in second :"+field2.getBoolean(a2Obj));
}
}if(field1.getType() == int.class && (field1.getInt(a1Obj) == field2.getInt(a2Obj))){
System.out.println(" Int is "+field1.getInt(a1Obj));
continue;
}if(field1.getType() == long.class && (field1.getLong(a1Obj) == field2.getLong(a2Obj))){
System.out.println(" Long is "+field1.getLong(a1Obj));
continue;
}if(field1.getType() == char.class && (field1.getChar(a1Obj) == field2.getChar(a2Obj))){
System.out.println(" Char is "+field1.getChar(a1Obj));
continue;
}if(field1.getType() == short.class && (field1.getShort(a1Obj) == field2.getShort(a2Obj))){
System.out.println(" Short is "+field1.getShort(a1Obj));
continue;
}if(field1.getType() == float.class && (field1.getFloat(a1Obj) == field2.getFloat(a2Obj))){
System.out.println(" Float is "+field1.getFloat(a1Obj));
continue;
}if(field1.getType() == double.class && (field1.getDouble(a1Obj) == field2.getDouble(a2Obj))){
System.out.println(" Double is "+field1.getDouble(a1Obj));
continue;
}if(field1.getType() == byte.class && (field1.getByte(a1Obj) == field2.getByte(a2Obj))){
System.out.println(" Byte is "+field1.getByte(a1Obj));
continue;
}if(field1.getType() == String.class && (BeanUtils.getProperty(a1Obj, field1.getName()) == BeanUtils.getProperty(a2Obj, field1.getName()))){
System.out.println(" String is "+ BeanUtils.getProperty(a1Obj, field1.getName()));
continue;
}if(field1.getType() == Date.class && (BeanUtils.getProperty(a1Obj, field1.getName()) == BeanUtils.getProperty(a2Obj, field1.getName()))){
System.out.println(" String is "+ BeanUtils.getProperty(a1Obj, field1.getName()));
continue;
}
}
}catch(Exception e){
e.printStackTrace();
}
if(flag){
compare(nextA, nextB, diffList);
}
return " complete";
}
}
************************ClassA****************************
package com.comparators.test_objcompartor;
public class ClassA {
public int xint = 10;
public int yint=11;
public boolean bool = true;
public Boolean b1=Boolean.FALSE;
public String str = "Super";
byte m = 1;
@Embedded
public ClassB bObj = new ClassB();
public int getXint() {
return xint;
}
public void setXint(int xint) {
this.xint = xint;
}
public int getYint() {
return yint;
}
public void setYint(int yint) {
this.yint = yint;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public byte getM() {
return m;
}
public void setM(byte m) {
this.m = m;
}
public ClassB getbObj() {
return bObj;
}
public void setbObj(ClassB bObj) {
this.bObj = bObj;
}
}
***************ClassB******************
package com.comparators.test_objcompartor;
public class ClassB {
String s1;
int p;
@Embedded
ClassC cObj = new ClassC();
public ClassC getcObj() {
return cObj;
}
public void setcObj(ClassC cObj) {
this.cObj = cObj;
}
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public int getP() {
return p;
}
public void setP(int p) {
this.p = p;
}
}
***********ClassC**************
package com.comparators.test_objcompartor;
public class ClassC {
boolean y = true;
int q = 12;
String strrr = "Great";
public boolean isY() {
return y;
}
public void setY(boolean y) {
this.y = y;
}
public int getQ() {
return q;
}
public void setQ(int q) {
this.q = q;
}
public String getStrrr() {
return strrr;
}
public void setStrrr(String strrr) {
this.strrr = strrr;
}
}
************************Embedded.java************************
package com.comparators.test_objcompartor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface Embedded {
//String str();
//int val();
}
*******************pom.xml**********
4.0.0
com.comparators
test-objcompartor
0.0.1-SNAPSHOT
jar
test-objcompartor
http://maven.apache.org
UTF-8
junit
junit
3.8.1
test
commons-lang
commons-lang
2.4
commons-beanutils
commons-beanutils
1.8.0
********************************
package com.comparators.test_objcompartor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
/**
* Hello world!
*
*/
public class ObjectComparator{
@SuppressWarnings("rawtypes")
public static void main(String args[]){
ClassA a1Obj= new ClassA();
ClassA a2Obj= new ClassA();
List diffList = new ArrayList();
ObjectComparator bkp = new ObjectComparator();
a2Obj.setXint(13);
a2Obj.setStr("Crazy");
a1Obj.bObj.setP(16);
a2Obj.bObj.setP(19);
System.out.println("\n **Job "+ bkp.compare(a1Obj, a2Obj, diffList));
}
@SuppressWarnings("unchecked")
public String compare(Object a1Obj,Object a2Obj, List diffList){
Object nextA = null;
Object nextB = null;
boolean flag = false;
Field field2 = null;
try{
for(Field field1:a1Obj.getClass().getDeclaredFields()){
field2 = a2Obj.getClass().getDeclaredField(field1.getName());
System.out.println(" field name is "+field1.getName());
if(field1.getAnnotation(Embedded.class) instanceof Embedded ){
nextA = PropertyUtils.getProperty(a1Obj, field1.getName());
nextB = PropertyUtils.getProperty(a2Obj, field2.getName());
flag = true;
continue;
}
if(field1.getType() == boolean.class){
if(field1.getBoolean(a1Obj) == field2.getBoolean(a2Obj)){
continue;
}else{
diffList.add(" In "+a1Obj.getClass()+ " field of diff "+field1.getName());
diffList.add(" Value in first : "+field1.getBoolean(a1Obj));
diffList.add(" Value in second :"+field2.getBoolean(a2Obj));
}
}if(field1.getType() == int.class && (field1.getInt(a1Obj) == field2.getInt(a2Obj))){
System.out.println(" Int is "+field1.getInt(a1Obj));
continue;
}if(field1.getType() == long.class && (field1.getLong(a1Obj) == field2.getLong(a2Obj))){
System.out.println(" Long is "+field1.getLong(a1Obj));
continue;
}if(field1.getType() == char.class && (field1.getChar(a1Obj) == field2.getChar(a2Obj))){
System.out.println(" Char is "+field1.getChar(a1Obj));
continue;
}if(field1.getType() == short.class && (field1.getShort(a1Obj) == field2.getShort(a2Obj))){
System.out.println(" Short is "+field1.getShort(a1Obj));
continue;
}if(field1.getType() == float.class && (field1.getFloat(a1Obj) == field2.getFloat(a2Obj))){
System.out.println(" Float is "+field1.getFloat(a1Obj));
continue;
}if(field1.getType() == double.class && (field1.getDouble(a1Obj) == field2.getDouble(a2Obj))){
System.out.println(" Double is "+field1.getDouble(a1Obj));
continue;
}if(field1.getType() == byte.class && (field1.getByte(a1Obj) == field2.getByte(a2Obj))){
System.out.println(" Byte is "+field1.getByte(a1Obj));
continue;
}if(field1.getType() == String.class && (BeanUtils.getProperty(a1Obj, field1.getName()) == BeanUtils.getProperty(a2Obj, field1.getName()))){
System.out.println(" String is "+ BeanUtils.getProperty(a1Obj, field1.getName()));
continue;
}if(field1.getType() == Date.class && (BeanUtils.getProperty(a1Obj, field1.getName()) == BeanUtils.getProperty(a2Obj, field1.getName()))){
System.out.println(" String is "+ BeanUtils.getProperty(a1Obj, field1.getName()));
continue;
}
}
}catch(Exception e){
e.printStackTrace();
}
if(flag){
compare(nextA, nextB, diffList);
}
return " complete";
}
}
************************ClassA****************************
package com.comparators.test_objcompartor;
public class ClassA {
public int xint = 10;
public int yint=11;
public boolean bool = true;
public Boolean b1=Boolean.FALSE;
public String str = "Super";
byte m = 1;
@Embedded
public ClassB bObj = new ClassB();
public int getXint() {
return xint;
}
public void setXint(int xint) {
this.xint = xint;
}
public int getYint() {
return yint;
}
public void setYint(int yint) {
this.yint = yint;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public byte getM() {
return m;
}
public void setM(byte m) {
this.m = m;
}
public ClassB getbObj() {
return bObj;
}
public void setbObj(ClassB bObj) {
this.bObj = bObj;
}
}
***************ClassB******************
package com.comparators.test_objcompartor;
public class ClassB {
String s1;
int p;
@Embedded
ClassC cObj = new ClassC();
public ClassC getcObj() {
return cObj;
}
public void setcObj(ClassC cObj) {
this.cObj = cObj;
}
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
public int getP() {
return p;
}
public void setP(int p) {
this.p = p;
}
}
***********ClassC**************
package com.comparators.test_objcompartor;
public class ClassC {
boolean y = true;
int q = 12;
String strrr = "Great";
public boolean isY() {
return y;
}
public void setY(boolean y) {
this.y = y;
}
public int getQ() {
return q;
}
public void setQ(int q) {
this.q = q;
}
public String getStrrr() {
return strrr;
}
public void setStrrr(String strrr) {
this.strrr = strrr;
}
}
************************Embedded.java************************
package com.comparators.test_objcompartor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface Embedded {
//String str();
//int val();
}
*******************pom.xml**********
********************************
0 comments:
Post a Comment