LimeWire collection component api

org.limewire.collection
Class Comparators

java.lang.Object
  extended by org.limewire.collection.Comparators

public final class Comparators
extends java.lang.Object

Provides a way to compare various data types with static methods.

Comparators is a utility class that uses the strategy pattern for Comparable instances. Many of these comparators are only necessary because the Java 1.1.8 versions of their classes did not implement the Comparable interface.

Comparators are helpful when using a FixedsizePriorityQueue.

   
    FixedsizePriorityQueue<String> fpq = 
        new FixedsizePriorityQueue<String>(Comparators.stringComparator(), 3);
    fpq.insert("Abby");
    fpq.insert("Bob");
    fpq.insert("Chris");
    System.out.println(fpq);
    String s = fpq.insert("Dan");
    System.out.println("Inserting another String pushes out an element (" + s + ") since the max. size was reached.");
    System.out.println(fpq);

    System.out.println("Minimum element: " + fpq.getMin());
    System.out.println("Maximum element: " + fpq.getMax());
    fpq.extractMax();
    System.out.println(fpq);

    Output:
        [Abby, Bob, Chris]
        Inserting another String pushes out an element (Abby) since the max. size was reached.
        [Bob, Chris, Dan]
        Minimum element: Bob
        Maximum element: Dan
        [Bob, Chris]

 


Nested Class Summary
static class Comparators.ByteArrayComparator
          Compares two byte arrays.
static class Comparators.CaseInsensitiveStringComparator
          Compares String objects without regard to case.
 
Method Summary
static java.util.Comparator<java.lang.String> caseInsensitiveStringComparator()
          Instance assessor for the Comparator for case insensitive Strings.
static java.util.Comparator<java.io.File> fileComparator()
          Instance assessor for the Comparator for Files.
static int intCompareTo(java.lang.Integer thisInt, java.lang.Integer anotherInt)
          Compares two Integer objects numerically.
static java.util.Comparator<java.lang.Integer> integerComparator()
          Instance assessor for the Comparator for Integers.
static java.util.Comparator<java.lang.Double> inverseDoubleComparator()
           
static java.util.Comparator<java.lang.Integer> inverseIntegerComparator()
           
static java.util.Comparator<java.lang.Long> inverseLongComparator()
          Instance assessor for the inverse Comparator for Longs.
static java.util.Comparator<java.lang.Long> longComparator()
          Instance assessor for the Comparator for Longs.
static int longCompareTo(java.lang.Long firstLong, java.lang.Long secondLong)
          Compares two Long objects numerically.
static java.util.Comparator<java.lang.String> stringComparator()
          Instance assessor for the Comparator for Strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

integerComparator

public static java.util.Comparator<java.lang.Integer> integerComparator()
Instance assessor for the Comparator for Integers. This is necessary because the Integer class did not implement Comparable in Java 1.1.8. This is an instance because the IntComparator has no state, allowing a single instance to be used whenever a Comparator is needed for Integers.

Returns:
the IntComparator instance

inverseIntegerComparator

public static java.util.Comparator<java.lang.Integer> inverseIntegerComparator()

longComparator

public static java.util.Comparator<java.lang.Long> longComparator()
Instance assessor for the Comparator for Longs. This is necessary because the Long class did not implement Comparable in Java 1.1.8. This is an instance because the LongComparator has no state, allowing a single instance to be used whenever a Comparator is needed for Longs.

Returns:
the LongComparator instance

inverseLongComparator

public static java.util.Comparator<java.lang.Long> inverseLongComparator()
Instance assessor for the inverse Comparator for Longs. This is necessary because the Long class did not implement Comparable in Java 1.1.8. This is an instance because the LongComparator has no state, allowing a single instance to be used whenever a Comparator is needed for Longs.

Returns:
the LongComparator instance

inverseDoubleComparator

public static java.util.Comparator<java.lang.Double> inverseDoubleComparator()

stringComparator

public static java.util.Comparator<java.lang.String> stringComparator()
Instance assessor for the Comparator for Strings. This is necessary because the String class did not implement Comparable in Java 1.1.8. This is an instance because the StringComparator has no state, allowing a single instance to be used whenever a Comparator is needed for Strings.

Returns:
the StringComparator instance

fileComparator

public static java.util.Comparator<java.io.File> fileComparator()
Instance assessor for the Comparator for Files. This is necessary because the File class did not implement Comparable in Java 1.1.8. This is an instance because the FileComparator has no state, allowing a single instance to be used whenever a Comparator is needed for Files.

Returns:
the FileComparator instance

caseInsensitiveStringComparator

public static java.util.Comparator<java.lang.String> caseInsensitiveStringComparator()
Instance assessor for the Comparator for case insensitive Strings. This is an instance because the CaseInsensitiveStringComparator has no state, allowing a single instance to be used whenever a Comparator is needed.

Returns:
the CaseInsensitiveStringComparator instance

intCompareTo

public static int intCompareTo(java.lang.Integer thisInt,
                               java.lang.Integer anotherInt)
Compares two Integer objects numerically. This function is identical to the Integer compareTo method. The Integer compareTo method was added in Java 1.2, however, so any app that is 1.1.8 compatible must use this method.


longCompareTo

public static int longCompareTo(java.lang.Long firstLong,
                                java.lang.Long secondLong)
Compares two Long objects numerically. This function is identical to the Long compareTo method. The Long compareTo method was added in Java 1.2, however, so any app that is 1.1.8 compatible must use this method.

Parameters:
firstLong - the first Long to be compared.
secondLong - the second Long to be compared.
Returns:
the value 0 if the first Long argument is equal to the second Long argument; a value less than 0 if the first Long argument is numerically less than the second Long; and a value greater than 0 if the first Long argument is numerically greater than the second Long argument (signed comparison).

LimeWire collection component api

Copyright © 2009 Lime Wire LLC. All Rights Reserved.