org.apache.jackrabbit.commons.flat
Class Rank<T>

java.lang.Object
  extended by org.apache.jackrabbit.commons.flat.Rank<T>
Type Parameters:
T - Type of values in this Rank.

public class Rank<T>
extends Object

This class does efficient ranking of values of type T wrt. to a Comparator for T. After creating an instance of Rank, the take(int) method returns the next k smallest values. That is, each of these values is smaller than every value not yet retrieved. The order of the values returned by take is not specified in general. However if the values are in increasing order, the values returned by take will also be in increasing order.

Note: The values may not contain duplicates or the behavior of take is not defined.


Constructor Summary
Rank(Collection<T> values, Class<T> componentType, Comparator<? super T> order)
          Create a new instance of Rank for a given collection of values and a given order.
Rank(Iterator<T> values, Class<T> componentType, int count, Comparator<? super T> order)
          Create a new instance of Rank for the first count values in a a given iterator of values and a given order.
Rank(T[] values, Comparator<? super T> order)
          Create a new instance of Rank for a given array of values and a given order.
 
Method Summary
static
<T extends Comparable<T>>
Comparator<T>
comparableComparator()
          Utility method for creating a Comparator of T from a Comparable of type T.
 Comparator<? super T> getOrder()
           
static
<S extends Comparable<S>>
Rank<S>
rank(Collection<S> values, Class<S> componentType)
          Create a new instance of Rank for a given collection of values.
static
<S extends Comparable<S>>
Rank<S>
rank(Iterator<S> values, Class<S> componentType, int count)
          Create a new instance of Rank for the first count values in a a given iterator of values.
static
<S extends Comparable<S>>
Rank<S>
rank(S[] values)
          Create a new instance of Rank for a given array of values.
 int size()
          Returns the number of remaining items in the Rank.
 Iterator<T> take(int n)
          Returns the n-th smallest values remaining in this Rank.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Rank

public Rank(T[] values,
            Comparator<? super T> order)
Create a new instance of Rank for a given array of values and a given order. The values are manipulated in place, no copying is performed.

Parameters:
values - values for ranking. Duplicates are not allowed.
order - Ordering for ranking

Rank

public Rank(Collection<T> values,
            Class<T> componentType,
            Comparator<? super T> order)
Create a new instance of Rank for a given collection of values and a given order. The values are copied into an internal array before they are manipulated.

Parameters:
values - values for ranking. Duplicates are not allowed.
componentType - type evidence for the values
order - Ordering for ranking

Rank

public Rank(Iterator<T> values,
            Class<T> componentType,
            int count,
            Comparator<? super T> order)
Create a new instance of Rank for the first count values in a a given iterator of values and a given order. The values are copied into an internal array before they are manipulated.

Parameters:
values - values for ranking. Duplicates are not allowed.
componentType - type evidence for the values
count - Number of items to include. -1 for all.
order - Ordering for ranking
Method Detail

rank

public static <S extends Comparable<S>> Rank<S> rank(S[] values)
Create a new instance of Rank for a given array of values. The order is determined by the natural ordering of the values (i.e. through Comparable). The values are manipulated in place, no copying is performed.

Type Parameters:
S - extends Comparable<S>
Parameters:
values - values for ranking. Duplicates are not allowed.
Returns:
A new instance of Rank.

rank

public static <S extends Comparable<S>> Rank<S> rank(Collection<S> values,
                                                     Class<S> componentType)
Create a new instance of Rank for a given collection of values. The order is determined by the natural ordering of the values (i.e. through Comparable). The values are copied into an internal array before they are manipulated.

Type Parameters:
S - extends Comparable<S>
Parameters:
values - values for ranking. Duplicates are not allowed.
componentType - type evidence for the values
Returns:
A new instance of Rank.

rank

public static <S extends Comparable<S>> Rank<S> rank(Iterator<S> values,
                                                     Class<S> componentType,
                                                     int count)
Create a new instance of Rank for the first count values in a a given iterator of values. The order is determined by the natural ordering of the values (i.e. through Comparable). The values are copied into an internal array before they are manipulated.

Type Parameters:
S - extends Comparable<S>
Parameters:
values - values for ranking. Duplicates are not allowed.
componentType - type evidence for the values
count - Number of items to include. -1 for all.
Returns:
A new instance of Rank.

comparableComparator

public static <T extends Comparable<T>> Comparator<T> comparableComparator()
Utility method for creating a Comparator of T from a Comparable of type T.

Type Parameters:
T - extends Comparable<T>
Returns:
Comparator whose order is defined by T.

getOrder

public Comparator<? super T> getOrder()

take

public Iterator<T> take(int n)
Returns the n-th smallest values remaining in this Rank.

Parameters:
n - Number of values to return
Returns:
An iterator containing the next n smallest values.
Throws:
NoSuchElementException - if this Rank has not enough remaining elements or when n is negative.

size

public int size()
Returns the number of remaining items in the Rank.

Returns:
number of remaining items.


Copyright © 2004-2010 The Apache Software Foundation. All Rights Reserved.