import java.lang.reflect.field;import java.lang.reflect.method;import java.util.hashmap;import java.util.Map; Import Org.apache.log4j.Logger; Public classDomainequals {/** * Log Operation class*/ Private StaticLogger Logger = Logger.getlogger (domainequals.class); Publicdomainequals () {}/** Compare the values of two bean or map objects * If the bean is compared to the map object, the key value in map should be the same as the Bean's property value name and the number of fields will be consistent * @param source * @param t Arget * @return*/ Public StaticBoolean Domainequals (object source, Object target) {if(Source = =NULL|| target = =NULL) { return false; } Boolean RV=true; if(source instanceof Map) {RV=mapofsrc (source, target, RV); } Else{RV=classofsrc (source, target, RV); } logger.info ("The EQUALS RESULT is"+RV); returnRV; } /** * source target is map type * @param source * @param target * @param RV * @return*/ Private StaticBoolean Mapofsrc (object source, Object target, Boolean RV) {HashMap<string, string> map =NewHashmap<string, string>(); Map=(HASHMAP) source; for(String key:map.keySet ()) {if(target instanceof Map) {HashMap<string, string> tarmap =NewHashmap<string, string>(); Tarmap=(HASHMAP) target; if(Tarmap.Get(key) = =NULL) {RV=false; Break; } if(!map.Get(key). Equals (Tarmap.Get(key))) {RV=false; Break; } } Else{String Tarvalue= Getclassvalue (target, key) = =NULL?"": Getclassvalue (target, key). ToString (); if(!tarvalue.equals (map.Get(key))) {RV=false; Break; } } } returnRV; } /** * source target is non-map type * @param source * @param target * @param RV * @return*/ Private StaticBoolean Classofsrc (object source, Object target, Boolean RV) {Class<?> Srcclass =Source.getclass (); Field[] Fields=Srcclass.getdeclaredfields (); for(Field field:fields) {String Namekey=Field.getname (); if(target instanceof Map) {HashMap<string, string> tarmap =NewHashmap<string, string>(); Tarmap=(HASHMAP) target; String Srcvalue= Getclassvalue (source, namekey) = =NULL?"": Getclassvalue (source, Namekey). toString (); if(Tarmap.Get(namekey) = =NULL) {RV=false; Break; } if(!tarmap.Get(Namekey). Equals (Srcvalue)) {RV=false; Break; } } Else{String Srcvalue= Getclassvalue (source, namekey) = =NULL?"": Getclassvalue (source, Namekey). toString (); String Tarvalue= Getclassvalue (target, namekey) = =NULL?"": Getclassvalue (target, Namekey). toString (); if(!srcvalue.equals (Tarvalue)) {RV=false; Break; } } } returnRV; } /** * based on field name * @param obj * @param fieldName * @return*/ Public Staticobject Getclassvalue (Object obj, String fieldName) {if(obj = =NULL) { return NULL; } Try{Class Beanclass=Obj.getclass (); Method[] Ms=Beanclass.getmethods (); for(inti =0; i < ms.length; i++) { //non-Get method does not take if(!ms[i].getname (). StartsWith ("Get")) { Continue; } Object ObjValue=NULL; Try{ObjValue= Ms[i].invoke (obj,Newobject[] {}); } Catch(Exception e) {logger.info ("Error in reflection value:"+e.tostring ()); Continue; } if(ObjValue = =NULL) { Continue; } if(Ms[i].getname (). toUpperCase (). Equals (Fieldname.touppercase ())|| Ms[i].getname (). SUBSTRING (3). toUpperCase (). Equals (Fieldname.touppercase ())) {returnObjValue; } Else if(Fieldname.touppercase (). Equals ("SID") && (Ms[i].getname (). toUpperCase (). Equals ("ID") || Ms[i].getname (). SUBSTRING (3). toUpperCase (). Equals ("ID"))) { returnObjValue; } } } Catch(Exception e) {//Logger.info ("Error fetching Method! "+ e.tostring ()); } return NULL; } Public Static voidMain (String args[]) {}}
Java uses reflection to compare two Bean object property values for equality