Added Collection APIs for the excludeFieldNames as suggested at the end of LANG-226

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@416487 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2006-06-22 22:20:44 +00:00
parent b7d3fd9e4d
commit d3c425d6f1
4 changed files with 87 additions and 4 deletions

View File

@ -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 @@ public static int reflectionCompare(Object lhs, Object rhs, boolean compareTrans
*
* @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>

View File

@ -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;
/**
@ -124,6 +125,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));
}
/**
* <p>This method uses reflection to determine if the two <code>Object</code>s
* are equal.</p>

View File

@ -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;
/**
@ -153,6 +154,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));
}
/**
* <p>This method uses reflection to build a valid hash code.</p>
*

View File

@ -405,7 +405,7 @@ public static String toStringExclude(Object object, final String excludeFieldNam
* 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 @@ public static String toStringExclude(Object object, Collection excludeFieldNames
* 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 @@ private static String[] toNoNullStringArray(Collection collection) {
* 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];