Remove Class and Package comparators - too specific set of use cases

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@138001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-12-19 22:35:38 +00:00
parent 74d20911d4
commit d669920cb2
2 changed files with 9 additions and 165 deletions

View File

@ -16,7 +16,6 @@
package org.apache.commons.lang;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -32,7 +31,7 @@ import java.util.Map;
* @author Gary Gregory
* @author Norm Deane
* @since 2.0
* @version $Id: ClassUtils.java,v 1.33 2004/10/21 01:18:33 ggregory Exp $
* @version $Id: ClassUtils.java,v 1.34 2004/12/19 22:35:38 scolebourne Exp $
*/
public class ClassUtils {
@ -497,71 +496,5 @@ public class ClassUtils {
}
return (cls.getName().indexOf(INNER_CLASS_SEPARATOR_CHAR) >= 0);
}
//-----------------------------------------------------------------------
/**
* Compares two <code>Class</code>s by name.
*/
private static class ClassNameComparator implements Comparator {
/**
* Compares two <code>Class</code>s by name.
* @param o1 The receiver of the comparison call to {@link String#compareTo(String)}.
* @param o2 The argument of the comparison call to {@link String#compareTo(String)}.
* @return The return value from {@link String#compareTo(String)}
*
* @throws ClassCastException
* If <code>o1</code> or <code>o2</code> are not <code>Class</code>
* instances.
*/
public int compare(Object o1, Object o2) {
Class class1 = (Class) o1;
Class class2 = (Class) o2;
if (class1 == null) {
return class2 == null ? 0 : -1;
}
if (class2 == null) {
return 1;
}
return class1.getName().compareTo(class2.getName());
}
}
/**
* Compares two <code>Class</code>s by name.
*/
public static final Comparator CLASS_NAME_COMPARATOR = new ClassNameComparator();
/**
* Compares two <code>Package</code>s by name.
*/
private static class PackageNameComparator implements Comparator {
/**
* Compares two <code>Package</code>s by name.
*
* @param o1 The receiver of the comparison call to {@link String#compareTo(String)}.
* @param o2 The argument of the comparison call to {@link String#compareTo(String)}.
* @return The return value from {@link String#compareTo(String)}
* @throws ClassCastException
* If <code>o1</code> or <code>o2</code> are not <code>Package</code>
* instances.
*/
public int compare(Object o1, Object o2) {
Package package1 = (Package) o1;
Package package2 = (Package) o2;
if (package1 == null) {
return package2 == null ? 0 : -1;
}
if (package2 == null) {
return 1;
}
return package1.getName().compareTo(package2.getName());
}
}
/**
* Compares two <code>Package</code>s by name.
*/
public static final Comparator PACKAGE_NAME_COMPARATOR = new PackageNameComparator();
}

View File

@ -19,7 +19,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@ -33,7 +32,7 @@ import junit.textui.TestRunner;
*
* @author Stephen Colebourne
* @author Gary D. Gregory
* @version $Id: ClassUtilsTest.java,v 1.13 2004/06/30 18:33:58 ggregory Exp $
* @version $Id: ClassUtilsTest.java,v 1.14 2004/12/19 22:35:38 scolebourne Exp $
*/
public class ClassUtilsTest extends TestCase {
@ -389,102 +388,14 @@ public class ClassUtilsTest extends TestCase {
// This used to return the exact same array, but no longer does.
assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
}
public void testClassComparator() {
Comparator comparator = ClassUtils.CLASS_NAME_COMPARATOR;
Class smallClass = java.lang.Boolean.class;
Class bigClass = java.util.Set.class;
assertTrue(comparator.compare(smallClass, smallClass) == 0);
assertTrue(comparator.compare(bigClass, smallClass) > 0);
assertTrue(comparator.compare(smallClass, bigClass) < 0);
assertTrue(comparator.compare(smallClass, null) > 0);
assertTrue(comparator.compare(null, smallClass) < 0);
assertComparatorContract(comparator, smallClass, smallClass);
assertComparatorContract(comparator, bigClass, bigClass);
assertComparatorContract(comparator, smallClass, bigClass);
}
public void testPackageComparator() {
Comparator comparator = ClassUtils.PACKAGE_NAME_COMPARATOR;
Package smallPackage = java.lang.Boolean.class.getPackage();
Package bigPackage = java.util.Set.class.getPackage();
assertTrue(comparator.compare(smallPackage, smallPackage) == 0);
assertTrue(comparator.compare(bigPackage, smallPackage) > 0);
assertTrue(comparator.compare(smallPackage, bigPackage) < 0);
assertTrue(comparator.compare(smallPackage, null) > 0);
assertTrue(comparator.compare(null, smallPackage) < 0);
assertComparatorContract(comparator, smallPackage, smallPackage);
assertComparatorContract(comparator, bigPackage, bigPackage);
assertComparatorContract(comparator, smallPackage, bigPackage);
}
public void testPackageNameComparatorWithDifferentClassLoaders() throws SecurityException, IllegalArgumentException, ClassNotFoundException {
Comparator comparator = ClassUtils.PACKAGE_NAME_COMPARATOR;
Package p1 = java.lang.Boolean.class.getPackage();
Package p2 = java.util.Set.class.getPackage();
ClassLoader classLoader = newSystemClassLoader();
Object p1Other = this.getPackage(classLoader, "java.lang.Boolean");
Object p2Other = this.getPackage(classLoader, "java.util.Set");
// all here
assertComparatorContract(comparator, p1, p1);
assertComparatorContract(comparator, p2, p2);
assertComparatorContract(comparator, p1, p2);
// all other
assertComparatorContract(comparator, p1Other, p1Other);
assertComparatorContract(comparator, p2Other, p2Other);
assertComparatorContract(comparator, p1Other, p2Other);
// p1 and p1Other
assertComparatorContract(comparator, p1, p1Other);
assertComparatorContract(comparator, p2, p2);
assertComparatorContract(comparator, p1Other, p2);
// p2 and p2Other
assertComparatorContract(comparator, p1, p1);
assertComparatorContract(comparator, p2, p2Other);
assertComparatorContract(comparator, p1, p2Other);
}
Object getPackage(ClassLoader classLoader, String className) throws ClassNotFoundException, SecurityException,
IllegalArgumentException {
// Sanity check:
assertNotNull(Package.getPackage("java.lang"));
Package.getPackage("java.lang").equals(Package.getPackage("java.lang"));
// set up:
assertNotNull(classLoader);
Class otherClass = classLoader.loadClass(className);
assertNotNull(otherClass);
Object otherPackage = otherClass.getPackage();
assertNotNull(otherPackage);
return otherPackage;
}
/**
* The ordering imposed by a Comparator c on a set of elements S is said to
* be consistent with equals if and only if (compare((Object)e1,
* (Object)e2)==0) has the same boolean value as e1.equals((Object)e2) for
* every e1 and e2 in S.
*
* http://java.sun.com/j2se/1.3/docs/api/java/util/Comparator.html
*/
public void assertComparatorContract(Comparator comparator, Object e1, Object e2) {
assertEquals(comparator.compare(e1, e2) == 0, e1.equals(e2));
}
public static ClassLoader newSystemClassLoader() throws SecurityException, IllegalArgumentException {
ClassLoader scl = ClassLoader.getSystemClassLoader();
if (!(scl instanceof URLClassLoader)) {
fail("Need a better test set up.");
}
URLClassLoader urlScl = (URLClassLoader)scl;
return URLClassLoader.newInstance(urlScl.getURLs(), null);
ClassLoader scl = ClassLoader.getSystemClassLoader();
if (!(scl instanceof URLClassLoader)) {
fail("Need a better test set up.");
}
URLClassLoader urlScl = (URLClassLoader)scl;
return URLClassLoader.newInstance(urlScl.getURLs(), null);
}
// public static List getAssignableFrom(List classes, Class superclass) {
// public static boolean isAssignable(Class[] classArray, Class[] toClassArray) {
// public static boolean isAssignable(Class cls, Class toClass) {
}