Refactoring code

This commit is contained in:
Patrick Gotthard 2013-11-15 22:13:45 +01:00
parent 438b4677d1
commit 4315a3aba2

View file

@ -23,7 +23,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -36,9 +35,8 @@ import org.slf4j.LoggerFactory;
/** /**
* Provides deep <b>Bean</b> clonning support. * Provides deep <b>Bean</b> clonning support.
* <p> * <p>
* It works on all read/write properties, recursively. It support all primitive * It works on all read/write properties, recursively. It support all primitive types, Strings, Collections, Cloneable objects and multi-dimensional arrays of
* types, Strings, Collections, Cloneable objects and multi-dimensional arrays * any of them.
* of any of them.
* <p> * <p>
* *
* @author Alejandro Abdelnur * @author Alejandro Abdelnur
@ -49,12 +47,25 @@ public class CloneableBean implements Serializable, Cloneable {
private static final long serialVersionUID = -6520053043831187823L; private static final long serialVersionUID = -6520053043831187823L;
private static final Logger LOG = LoggerFactory.getLogger(CloneableBean.class); private static final Logger LOG = LoggerFactory.getLogger(CloneableBean.class);
private static final Set<Class<?>> BASIC_TYPES = new HashSet<Class<?>>();
private static final Class<?>[] NO_PARAMS_DEF = new Class[0]; private static final Class<?>[] NO_PARAMS_DEF = new Class[0];
private static final Object[] NO_PARAMS = new Object[0]; private static final Object[] NO_PARAMS = new Object[0];
private final Object obj; private final Object obj;
private Set<String> ignoreProperties; private Set<String> ignoreProperties;
static {
BASIC_TYPES.add(Boolean.class);
BASIC_TYPES.add(Byte.class);
BASIC_TYPES.add(Character.class);
BASIC_TYPES.add(Double.class);
BASIC_TYPES.add(Float.class);
BASIC_TYPES.add(Integer.class);
BASIC_TYPES.add(Long.class);
BASIC_TYPES.add(Short.class);
BASIC_TYPES.add(String.class);
}
/** /**
* Default constructor. * Default constructor.
* <p> * <p>
@ -97,13 +108,9 @@ public class CloneableBean implements Serializable, Cloneable {
/** /**
* Creates a CloneableBean to be used in a delegation pattern. * Creates a CloneableBean to be used in a delegation pattern.
* <p> * <p>
* The property names in the ignoreProperties Set will not be copied into * The property names in the ignoreProperties Set will not be copied into the cloned instance. This is useful for cases where the Bean has convenience
* the cloned instance. This is useful for cases where the Bean has * properties (properties that are actually references to other properties or properties of properties). For example SyndFeed and SyndEntry beans have
* convenience properties (properties that are actually references to other * convenience properties, publishedDate, author, copyright and categories all of them mapped to properties in the DC Module.
* properties or properties of properties). For example SyndFeed and
* SyndEntry beans have convenience properties, publishedDate, author,
* copyright and categories all of them mapped to properties in the DC
* Module.
* <p> * <p>
* *
* @param obj object bean to clone. * @param obj object bean to clone.
@ -122,15 +129,13 @@ public class CloneableBean implements Serializable, Cloneable {
/** /**
* Makes a deep bean clone of the object. * Makes a deep bean clone of the object.
* <p> * <p>
* To be used by classes extending CloneableBean. Although it works also for * To be used by classes extending CloneableBean. Although it works also for classes using CloneableBean in a delegation pattern, for correctness those
* classes using CloneableBean in a delegation pattern, for correctness * classes should use the
* those classes should use the
* *
* @see #beanClone() beanClone method. * @see #beanClone() beanClone method.
* <p> * <p>
* @return a clone of the object bean. * @return a clone of the object bean.
* @throws CloneNotSupportedException thrown if the object bean could not be * @throws CloneNotSupportedException thrown if the object bean could not be cloned.
* cloned.
* *
*/ */
@Override @Override
@ -146,8 +151,7 @@ public class CloneableBean implements Serializable, Cloneable {
* @see #CloneableBean(Object) constructor. * @see #CloneableBean(Object) constructor.
* *
* @return a clone of the object bean. * @return a clone of the object bean.
* @throws CloneNotSupportedException thrown if the object bean could not be * @throws CloneNotSupportedException thrown if the object bean could not be cloned.
* cloned.
* *
*/ */
public Object beanClone() throws CloneNotSupportedException { public Object beanClone() throws CloneNotSupportedException {
@ -207,7 +211,7 @@ public class CloneableBean implements Serializable, Cloneable {
if (value != null) { if (value != null) {
final Class<?> vClass = value.getClass(); final Class<?> vClass = value.getClass();
if (vClass.isArray()) { if (vClass.isArray()) {
value = (T) cloneArray((T) value); value = cloneArray(value);
} else if (value instanceof Collection) { } else if (value instanceof Collection) {
value = (T) cloneCollection((Collection<Object>) value); value = (T) cloneCollection((Collection<Object>) value);
} else if (value instanceof Map) { } else if (value instanceof Map) {
@ -262,45 +266,8 @@ public class CloneableBean implements Serializable, Cloneable {
return newMap; return newMap;
} }
private static final Set<Class<?>> BASIC_TYPES = new HashSet<Class<?>>();
static {
BASIC_TYPES.add(Boolean.class);
BASIC_TYPES.add(Byte.class);
BASIC_TYPES.add(Character.class);
BASIC_TYPES.add(Double.class);
BASIC_TYPES.add(Float.class);
BASIC_TYPES.add(Integer.class);
BASIC_TYPES.add(Long.class);
BASIC_TYPES.add(Short.class);
BASIC_TYPES.add(String.class);
}
private static final Map<Class<?>, Class<?>[]> CONSTRUCTOR_BASIC_TYPES = new HashMap<Class<?>, Class<?>[]>();
static {
CONSTRUCTOR_BASIC_TYPES.put(Boolean.class, new Class[] { Boolean.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Byte.class, new Class[] { Byte.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Character.class, new Class[] { Character.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Double.class, new Class[] { Double.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Float.class, new Class[] { Float.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Integer.class, new Class[] { Integer.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Long.class, new Class[] { Long.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(Short.class, new Class[] { Short.TYPE });
CONSTRUCTOR_BASIC_TYPES.put(String.class, new Class[] { String.class });
}
private boolean isBasicType(final Class<?> vClass) { private boolean isBasicType(final Class<?> vClass) {
return BASIC_TYPES.contains(vClass); return BASIC_TYPES.contains(vClass);
} }
// THIS IS NOT NEEDED, BASIC TYPES ARE INMUTABLE, TUCU 20040710
/**
* private Object cloneBasicType(Object value) throws Exception { Class
* pClass = value.getClass(); Class[] defType = (Class[])
* CONSTRUCTOR_BASIC_TYPES.get(pClass); Constructor cons =
* pClass.getDeclaredConstructor(defType); value = cons.newInstance(new
* Object[]{value}); return value; }
**/
} }