comparators moved from the commons-sandbox/util component
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130615 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1391e31e55
commit
361f3483b5
|
@ -0,0 +1,125 @@
|
|||
package org.apache.commons.collections.comparators;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.lang.Comparable;
|
||||
|
||||
/**
|
||||
* A Comparator that compares Comparable objects.
|
||||
* Throws ClassCastExceptions if the objects are not
|
||||
* Comparable, or if they are null.
|
||||
* Throws ClassCastException if the compareTo of both
|
||||
* objects do not provide an inverse result of each other
|
||||
* as per the Comparable javadoc.
|
||||
*
|
||||
* @author bayard@generationjava.com
|
||||
* @version $Id: ComparableComparator.java,v 1.1 2002/02/26 22:42:31 morgand Exp $
|
||||
*/
|
||||
public class ComparableComparator implements Comparator {
|
||||
|
||||
public ComparableComparator() {
|
||||
}
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
if( (o1 == null) || (o2 == null) ) {
|
||||
throw new ClassCastException(
|
||||
"There were nulls in the arguments for this method: "+
|
||||
"compare("+o1 + ", " + o2 + ")"
|
||||
);
|
||||
}
|
||||
|
||||
if(o1 instanceof Comparable) {
|
||||
if(o2 instanceof Comparable) {
|
||||
int result1 = ((Comparable)o1).compareTo(o2);
|
||||
int result2 = ((Comparable)o2).compareTo(o1);
|
||||
|
||||
// enforce comparable contract
|
||||
if(result1 == 0 && result2 == 0) {
|
||||
return 0;
|
||||
} else
|
||||
if(result1 < 0 && result2 > 0) {
|
||||
return result1;
|
||||
} else
|
||||
if(result1 > 0 && result2 < 0) {
|
||||
return result1;
|
||||
} else {
|
||||
// results inconsistent
|
||||
throw new ClassCastException("o1 not comparable to o2");
|
||||
}
|
||||
} else {
|
||||
// o2 wasn't comparable
|
||||
throw new ClassCastException(
|
||||
"The first argument of this method was not a Comparable: " +
|
||||
o2.getClass().getName()
|
||||
);
|
||||
}
|
||||
} else
|
||||
if(o2 instanceof Comparable) {
|
||||
// o1 wasn't comparable
|
||||
throw new ClassCastException(
|
||||
"The second argument of this method was not a Comparable: " +
|
||||
o1.getClass().getName()
|
||||
);
|
||||
} else {
|
||||
// neither were comparable
|
||||
throw new ClassCastException(
|
||||
"Both arguments of this method were not Comparables: " +
|
||||
o1.getClass().getName() + " and " + o2.getClass().getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,224 @@
|
|||
package org.apache.commons.collections.comparators;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* A Comparator which deals with alphabet characters 'naturally', but
|
||||
* deals with numerics numerically. Leading 0's are ignored numerically,
|
||||
* but do come into play if the number is equal. Thus aaa119yyyy comes before
|
||||
* aaa0119xxxx regardless of x or y.
|
||||
*
|
||||
* The comparison should be very performant as it only ever deals with
|
||||
* issues at a character level and never tries to consider the
|
||||
* numerics as numbers.
|
||||
*
|
||||
* @author bayard@generationjava.com
|
||||
* @version $Id: NumericStringComparator.java,v 1.1 2002/02/26 22:42:31 morgand Exp $
|
||||
*/
|
||||
public class NumericStringComparator implements Comparator {
|
||||
|
||||
public NumericStringComparator() {
|
||||
}
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
if(o1 == null) {
|
||||
return 1;
|
||||
} else
|
||||
if(o2 == null) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
String s1 = o1.toString();
|
||||
String s2 = o2.toString();
|
||||
|
||||
// find the first digit.
|
||||
int idx1 = getFirstDigitIndex(s1);
|
||||
int idx2 = getFirstDigitIndex(s2);
|
||||
|
||||
if( ( idx1 == -1 ) ||
|
||||
( idx2 == -1 ) ||
|
||||
( !s1.substring(0,idx1).equals(s2.substring(0,idx2)) )
|
||||
)
|
||||
{
|
||||
return s1.compareTo(s2);
|
||||
}
|
||||
|
||||
// find the last digit
|
||||
int edx1 = getLastDigitIndex(s1, idx1);
|
||||
int edx2 = getLastDigitIndex(s2, idx2);
|
||||
|
||||
String sub1 = null;
|
||||
String sub2 = null;
|
||||
|
||||
if(edx1 == -1) {
|
||||
sub1 = s1.substring(idx1);
|
||||
} else {
|
||||
sub1 = s1.substring(idx1, edx1);
|
||||
}
|
||||
|
||||
if(edx2 == -1) {
|
||||
sub2 = s2.substring(idx2);
|
||||
} else {
|
||||
sub2 = s2.substring(idx2, edx2);
|
||||
}
|
||||
|
||||
// deal with zeros at start of each number
|
||||
int zero1 = countZeroes(sub1);
|
||||
int zero2 = countZeroes(sub2);
|
||||
|
||||
sub1 = sub1.substring(zero1);
|
||||
sub2 = sub2.substring(zero2);
|
||||
|
||||
// if equal, then recurse with the rest of the string
|
||||
// need to deal with zeroes so that 00119 appears after 119
|
||||
if(sub1.equals(sub2)) {
|
||||
int ret = 0;
|
||||
if(zero1 > zero2) {
|
||||
ret = 1;
|
||||
} else
|
||||
if(zero1 < zero2) {
|
||||
ret = -1;
|
||||
}
|
||||
if(edx1 != -1) {
|
||||
int comp = compare(s1.substring(edx1), s2.substring(edx2));
|
||||
if(comp != 0) {
|
||||
ret = comp;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
// if a numerical string is smaller in length than another
|
||||
// then it must be less.
|
||||
if(sub1.length() != sub2.length()) {
|
||||
return ( sub1.length() < sub2.length() ) ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// now we get to do the string based numerical thing :)
|
||||
// going to assume that the individual character for the
|
||||
// number has the right order. ie) '9' > '0'
|
||||
// possibly bad in i18n.
|
||||
char[] chr1 = sub1.toCharArray();
|
||||
char[] chr2 = sub2.toCharArray();
|
||||
|
||||
int sz = chr1.length;
|
||||
for(int i=0; i<sz; i++) {
|
||||
// this should give better speed
|
||||
if(chr1[i] != chr2[i]) {
|
||||
return (chr1[i] < chr2[i]) ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// TODO: Consider moving these to a lang helper class.
|
||||
private int getFirstDigitIndex(String str) {
|
||||
return getFirstDigitIndex(str, 0);
|
||||
}
|
||||
private int getFirstDigitIndex(String str, int start) {
|
||||
return getFirstDigitIndex(str.toCharArray(), start);
|
||||
}
|
||||
// Get the index of the first digit (number) in the array
|
||||
// of characters, starting at the specified digit.
|
||||
private int getFirstDigitIndex(char[] chrs, int start) {
|
||||
int sz = chrs.length;
|
||||
|
||||
for(int i=start; i<sz; i++) {
|
||||
if(Character.isDigit(chrs[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int getLastDigitIndex(String str, int start) {
|
||||
return getLastDigitIndex(str.toCharArray(), start);
|
||||
}
|
||||
// Get the index of the digit at the end of a collection
|
||||
// of digit characters in a character array, starting
|
||||
// at the provided start index.
|
||||
private int getLastDigitIndex(char[] chrs, int start) {
|
||||
int sz = chrs.length;
|
||||
|
||||
for(int i=start; i<sz; i++) {
|
||||
if(!Character.isDigit(chrs[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int countZeroes(String str) {
|
||||
int count = 0;
|
||||
|
||||
// assuming str is small...
|
||||
for(int i=0; i<str.length(); i++) {
|
||||
if(str.charAt(i) == '0') {
|
||||
count++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
package org.apache.commons.collections.comparators;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Sorts java package names.
|
||||
* Packages are grouped into java, javax, and other.
|
||||
* Inside each one they are alphabetical.
|
||||
*
|
||||
* @author bayard@generationjava.com
|
||||
* @version $Id: PackageNameComparator.java,v 1.1 2002/02/26 22:42:31 morgand Exp $
|
||||
*/
|
||||
public class PackageNameComparator implements Comparator {
|
||||
|
||||
static private int JAVA = 1;
|
||||
static private int JAVAX = 2;
|
||||
static private int OTHER = 3;
|
||||
|
||||
public int compare(Object obj1, Object obj2) {
|
||||
if( (obj1 instanceof String) && (obj2 instanceof String) ) {
|
||||
String str1 = (String)obj1;
|
||||
String str2 = (String)obj2;
|
||||
int type1 = getType(str1);
|
||||
int type2 = getType(str2);
|
||||
|
||||
if(type1 == JAVA) {
|
||||
if(type2 == JAVA) {
|
||||
str1 = str1.substring(4);
|
||||
str2 = str2.substring(4);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} else
|
||||
if(type2 == JAVA) {
|
||||
return 1;
|
||||
} else
|
||||
if(type1 == JAVAX) {
|
||||
if(type2 == JAVAX) {
|
||||
str1 = str1.substring(5);
|
||||
str2 = str2.substring(5);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} else
|
||||
if(type2 == JAVAX) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return str1.compareTo(str2);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static int getType(String str) {
|
||||
if(str.startsWith("java")) {
|
||||
if(str.charAt(4) == '.') {
|
||||
return JAVA;
|
||||
} else
|
||||
if( (str.charAt(4) == 'x') && (str.charAt(5) == '.') ) {
|
||||
return JAVAX;
|
||||
}
|
||||
}
|
||||
return OTHER;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package org.apache.commons.collections.comparators;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Reverses the order of another comparator.
|
||||
*
|
||||
* @author bayard@generationjava.com
|
||||
* @version $Id: ReverseComparator.java,v 1.1 2002/02/26 22:42:31 morgand Exp $
|
||||
*/
|
||||
public class ReverseComparator implements Comparator {
|
||||
|
||||
private Comparator comparator;
|
||||
|
||||
/**
|
||||
* Creates a reverse comparator that will invert any list.
|
||||
*/
|
||||
public ReverseComparator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a reverse comparator that inverts the comparison
|
||||
* of the passed in comparator.
|
||||
*/
|
||||
public ReverseComparator(Comparator comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
if(comparator == null) {
|
||||
return -1;
|
||||
} else {
|
||||
return -1*comparator.compare(o1,o2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package org.apache.commons.collections.comparators;
|
||||
|
||||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Compares URLs. Initially ignores protocol, so
|
||||
* ftp://sun.com and * http://sun.com will compare
|
||||
* near each other. Also pays attention to ports last, so
|
||||
* http://www:8080/index.html and http://www/index.html
|
||||
* will sort near each other.
|
||||
*
|
||||
* @author bayard@generationjava.com
|
||||
* @version $Id: UrlComparator.java,v 1.1 2002/02/26 22:42:31 morgand Exp $
|
||||
*/
|
||||
public class UrlComparator implements Comparator {
|
||||
|
||||
public UrlComparator() {
|
||||
}
|
||||
|
||||
public int compare(Object o1, Object o2) {
|
||||
if( (o1 instanceof URL) && (o2 instanceof URL)) {
|
||||
URL u1 = (URL)o1;
|
||||
URL u2 = (URL)o2;
|
||||
int ret = 0;
|
||||
|
||||
ret = u1.getHost().compareTo(u2.getHost());
|
||||
if(ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = u1.getPath().compareTo(u2.getPath());
|
||||
if(ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = u1.getProtocol().compareTo(u2.getProtocol());
|
||||
if(ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if(u1.getPort() < u2.getPort()) {
|
||||
return -1;
|
||||
} else
|
||||
if(u1.getPort() < u2.getPort()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue