package net.sourceforge.javautil.common;
import java.util.Map;
import net.sourceforge.javautil.common.reflection.cache.ClassCache;
import net.sourceforge.javautil.common.reflection.cache.ClassDescriptor;
import net.sourceforge.javautil.common.reflection.cache.ClassProperty;
/**
* Common methods and routines for compacting common code.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class CodeUtil {
/**
* This will allow safe comparison in case one or both objects being null.
*
* @param o1 Object 1
* @param o2 Object 2
* @return True if both objects are equals via == or via {@link Object#equals(Object)}
*/
public static boolean equals (Object o1, Object o2) {
if (o1 == o2) return true;
if (o1 == null || o2 == null) return false;
return o1.equals(o2);
}
/**
* @param o1 An object of any type
* @param o2 An instance of the type
* @param propertyNames The properties to compare (if this is empty it will compare all public properites)
* @return True if all the properties are equal in both objects, otherwise false
*/
public static boolean propertyEquals (Object o1, Object o2, String... propertyNames) {
assert o1 != null && o2 != null : "Objects cannot be null";
ClassDescriptor<?> desc1 = ClassCache.getFor(o1.getClass());
ClassDescriptor<?> desc2 = ClassCache.getFor(o2.getClass());
if (propertyNames.length == 0) {
Map<String,ClassProperty> props = desc1.getProperties();
propertyNames = props.keySet().toArray(new String[props.size()]);
}
for (String propertyName : propertyNames) {
ClassProperty property1 = desc1.getProperty(propertyName);
assert property1 != null : "No such property: " + propertyName + " for " + desc1;
ClassProperty property2 = desc2.getProperty(propertyName);
assert property2 != null : "No such property: " + propertyName + " for " + desc2;
if (!equals( property1.getValue(o1), property2.getValue(o2) )) return false;
}
return true;
}
/**
* @param target The object to be updated
* @param source The object to update from
* @param propertyNames The names of the properties to copy from the source to the target
* @return True if the target was actually updated, otherwise false indicating that both are equal property wise
*/
public static boolean updateProperties (Object target, Object source, String... propertyNames) {
if (propertyEquals(target, source, propertyNames)) {
setProperties(target, source, propertyNames);
return true;
}
return false;
}
/**
* @param target The object to be updated
* @param source The object to update from
* @param propertyNames The names of the properties to copy from the source to the target
*/
public static void setProperties (Object target, Object source, String... propertyNames) {
assert target != null && source != null : "Objects cannot be null";
ClassDescriptor descT = ClassCache.getFor(target.getClass());
ClassDescriptor descS = ClassCache.getFor(source.getClass());
if (propertyNames.length == 0) {
Map<String,ClassProperty> props = descS.getProperties();
propertyNames = props.keySet().toArray(new String[props.size()]);
}
for (String propertyName : propertyNames) {
descT.setPropertyValue(target, propertyName, descS.getPropertyValue(source, propertyName));
}
}
}