Author: bayard Date: Thu Jun 22 15:20:44 2006 New Revision: 416487 URL: http://svn.apache.org/viewvc?rev=416487&view=rev Log: Added Collection APIs for the excludeFieldNames as suggested at the end of LANG-226
Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/CompareToBuilder.java jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/CompareToBuilder.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/CompareToBuilder.java?rev=416487&r1=416486&r2=416487&view=diff ============================================================================== --- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/CompareToBuilder.java (original) +++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/CompareToBuilder.java Thu Jun 22 15:20:44 2006 @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.Collection; import java.util.Comparator; import java.util.List; @@ -192,7 +193,39 @@ * * @param lhs left-hand object * @param rhs right-hand object - * @param excludeFields fields to exclude + * @param excludeFields Collection of String fields to exclude + * @return a negative integer, zero, or a positive integer as <code>lhs</code> + * is less than, equal to, or greater than <code>rhs</code> + * @throws NullPointerException if either <code>lhs</code> or <code>rhs</code> + * (but not both) is <code>null</code> + * @throws ClassCastException if <code>rhs</code> is not assignment-compatible + * with <code>lhs</code> + */ + public static int reflectionCompare(Object lhs, Object rhs, Collection /*String*/ excludeFields) { + return reflectionCompare(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields)); + } + + /** + * <p>Compares two <code>Object</code>s via reflection.</p> + * + * <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code> + * is used to bypass normal access control checks. This will fail under a + * security manager unless the appropriate permissions are set.</p> + * + * <ul> + * <li>Static fields will not be compared</li> + * <li>If <code>compareTransients</code> is <code>true</code>, + * compares transient members. Otherwise ignores them, as they + * are likely derived fields.</li> + * <li>Superclass fields will be compared</li> + * </ul> + * + * <p>If both <code>lhs</code> and <code>rhs</code> are <code>null</code>, + * they are considered equal.</p> + * + * @param lhs left-hand object + * @param rhs right-hand object + * @param excludeFields array of fields to exclude * @return a negative integer, zero, or a positive integer as <code>lhs</code> * is less than, equal to, or greater than <code>rhs</code> * @throws NullPointerException if either <code>lhs</code> or <code>rhs</code> Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java?rev=416487&r1=416486&r2=416487&view=diff ============================================================================== --- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java (original) +++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java Thu Jun 22 15:20:44 2006 @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.Collection; import java.util.List; /** @@ -122,6 +123,29 @@ */ public static boolean reflectionEquals(Object lhs, Object rhs) { return reflectionEquals(lhs, rhs, false, null, null); + } + + /** + * <p>This method uses reflection to determine if the two <code>Object</code>s + * are equal.</p> + * + * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private + * fields. This means that it will throw a security exception if run under + * a security manager, if the permissions are not set up correctly. It is also + * not as efficient as testing explicitly.</p> + * + * <p>Transient members will be not be tested, as they are likely derived + * fields, and not part of the value of the Object.</p> + * + * <p>Static fields will not be tested. Superclass fields will be included.</p> + * + * @param lhs <code>this</code> object + * @param rhs the other object + * @param excludeFields Collection of String field names to exclude from testing + * @return <code>true</code> if the two Objects have tested equals. + */ + public static boolean reflectionEquals(Object lhs, Object rhs, Collection /*String*/ excludeFields) { + return reflectionEquals(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields)); } /** Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java?rev=416487&r1=416486&r2=416487&view=diff ============================================================================== --- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java (original) +++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java Thu Jun 22 15:20:44 2006 @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.Collection; import java.util.List; /** @@ -151,6 +152,31 @@ */ public static int reflectionHashCode(Object object) { return reflectionHashCode(17, 37, object, false, null, null); + } + + /** + * <p>This method uses reflection to build a valid hash code.</p> + * + * <p>This constructor uses two hard coded choices for the constants + * needed to build a hash code.</p> + * + * <p>It uses <code>AccessibleObject.setAccessible</code> to gain access to private + * fields. This means that it will throw a security exception if run under + * a security manager, if the permissions are not set up correctly. It is + * also not as efficient as testing explicitly.</p> + * + * <p>Transient members will be not be used, as they are likely derived + * fields, and not part of the value of the <code>Object</code>.</p> + * + * <p>Static fields will not be tested. Superclass fields will be included.</p> + * + * @param object the Object to create a <code>hashCode</code> for + * @param excludeFields Collection of String field names to exclude from use in calculation of hash code + * @return int hash code + * @throws IllegalArgumentException if the object is <code>null</code> + */ + public static int reflectionHashCode(Object object, Collection /*String*/ excludeFields) { + return reflectionHashCode(object, ReflectionToStringBuilder.toNoNullStringArray(excludeFields)); } /** Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java?rev=416487&r1=416486&r2=416487&view=diff ============================================================================== --- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java (original) +++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java Thu Jun 22 15:20:44 2006 @@ -405,7 +405,7 @@ * The field names to exclude. Null excludes nothing. * @return The toString value. */ - public static String toStringExclude(Object object, Collection excludeFieldNames) { + public static String toStringExclude(Object object, Collection /*String*/ excludeFieldNames) { return toStringExclude(object, toNoNullStringArray(excludeFieldNames)); } @@ -418,7 +418,7 @@ * The collection to convert * @return A new array of Strings. */ - private static String[] toNoNullStringArray(Collection collection) { + static String[] toNoNullStringArray(Collection collection) { if (collection == null) { return ArrayUtils.EMPTY_STRING_ARRAY; } @@ -434,7 +434,7 @@ * The array to check * @return The given array or a new array without null. */ - private static String[] toNoNullStringArray(Object[] array) { + static String[] toNoNullStringArray(Object[] array) { ArrayList list = new ArrayList(array.length); for (int i = 0; i < array.length; i++) { Object e = array[i]; --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]