com.amazon.carbonado.cursor
Class SortedCursor<S>

java.lang.Object
  extended by com.amazon.carbonado.cursor.AbstractCursor<S>
      extended by com.amazon.carbonado.cursor.SortedCursor<S>
All Implemented Interfaces:
Cursor<S>

public class SortedCursor<S>
extends AbstractCursor<S>

Wraps another Cursor and ensures the results are sorted. If the elements in the source cursor are already partially sorted, a handled comparator can be passed in which specifies the partial ordering. Elements are then processed in smaller chunks rather than sorting the entire set. The handled comparator can represent ascending or descending order of source elements.

Author:
Brian S O'Neill

Constructor Summary
SortedCursor(Cursor<S> cursor, SortBuffer<S> buffer, Class<S> type, String... orderProperties)
           
SortedCursor(Cursor<S> cursor, SortBuffer<S> buffer, Comparator<S> handled, Comparator<S> finisher)
           
 
Method Summary
 void close()
          Call close to release any resources being held by this cursor.
 Comparator<S> comparator()
          Returns a comparator representing the effective sort order of this cursor.
static
<S> Comparator<S>
createComparator(Class<S> type, String... orderProperties)
          Convenience method to create a comparator which orders storables by the given order-by properties.
static
<S extends Storable>
Comparator<S>
createComparator(List<OrderedProperty<S>> properties)
          Convenience method to create a comparator which orders storables by the given properties.
static
<S extends Storable>
Comparator<S>
createComparator(OrderedProperty<S>... properties)
          Convenience method to create a comparator which orders storables by the given properties.
 boolean hasNext()
          Returns true if this cursor has more elements.
 S next()
          Returns the next element from this cursor.
 int skipNext(int amount)
          Skips forward by the specified amount of elements, returning the actual amount skipped.
 
Methods inherited from class com.amazon.carbonado.cursor.AbstractCursor
copyInto, copyInto, toList, toList
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SortedCursor

public SortedCursor(Cursor<S> cursor,
                    SortBuffer<S> buffer,
                    Comparator<S> handled,
                    Comparator<S> finisher)
Parameters:
cursor - cursor to wrap
buffer - required buffer to hold results
handled - optional comparator which represents how the results are already sorted
finisher - required comparator which finishes the sort

SortedCursor

public SortedCursor(Cursor<S> cursor,
                    SortBuffer<S> buffer,
                    Class<S> type,
                    String... orderProperties)
Parameters:
cursor - cursor to wrap
buffer - required buffer to hold results
type - type of storable to create cursor for
orderProperties - list of properties to order by
Throws:
IllegalArgumentException - if any property is null or not a member of storable type
Method Detail

createComparator

public static <S> Comparator<S> createComparator(Class<S> type,
                                                 String... orderProperties)
Convenience method to create a comparator which orders storables by the given order-by properties. The property names may be prefixed with '+' or '-' to indicate ascending or descending order. If the prefix is omitted, ascending order is assumed.

Parameters:
type - type of storable to create comparator for
orderProperties - list of properties to order by
Throws:
IllegalArgumentException - if any property is null or not a member of storable type

createComparator

public static <S extends Storable> Comparator<S> createComparator(OrderedProperty<S>... properties)
Convenience method to create a comparator which orders storables by the given properties.

Parameters:
properties - list of properties to order by
Throws:
IllegalArgumentException - if no properties or if any property is null

createComparator

public static <S extends Storable> Comparator<S> createComparator(List<OrderedProperty<S>> properties)
Convenience method to create a comparator which orders storables by the given properties.

Parameters:
properties - list of properties to order by
Throws:
IllegalArgumentException - if no properties or if any property is null

comparator

public Comparator<S> comparator()
Returns a comparator representing the effective sort order of this cursor.


close

public void close()
           throws FetchException
Description copied from interface: Cursor
Call close to release any resources being held by this cursor. Further operations on this cursor will behave as if there are no results.

Throws:
FetchException

hasNext

public boolean hasNext()
                throws FetchException
Description copied from interface: Cursor
Returns true if this cursor has more elements. In other words, returns true if next would return an element rather than throwing an exception.

Throws:
FetchException - if storage layer throws an exception

next

public S next()
       throws FetchException
Description copied from interface: Cursor
Returns the next element from this cursor. This method may be called repeatedly to iterate through the results.

Throws:
FetchException - if storage layer throws an exception

skipNext

public int skipNext(int amount)
             throws FetchException
Description copied from interface: Cursor
Skips forward by the specified amount of elements, returning the actual amount skipped. The actual amount is less than the requested amount only if the end of the results was reached.

Specified by:
skipNext in interface Cursor<S>
Overrides:
skipNext in class AbstractCursor<S>
Parameters:
amount - maximum amount of elements to skip
Returns:
actual amount skipped
Throws:
FetchException - if storage layer throws an exception


Copyright © 2006-2009 Amazon Technologies, Inc.. All Rights Reserved.