net.sf.saxon.sort

Class SortedIterator

Implemented Interfaces:
LastPositionFinder, LookaheadIterator, SequenceIterator, Sortable
Known Direct Subclasses:
SortedGroupIterator, SortedTupleIterator

public class SortedIterator
extends java.lang.Object
implements SequenceIterator, LastPositionFinder, LookaheadIterator, Sortable

Class to do a sorted iteration

Field Summary

protected SequenceIterator
base
protected AtomicComparer[]
comparators
protected XPathContext
context
protected int
count
protected Object[]
nodeKeys
protected int
position
protected int
recordSize
protected SortKeyEvaluator
sortKeyEvaluator

Fields inherited from interface net.sf.saxon.om.SequenceIterator

GROUNDED, LAST_POSITION_FINDER, LOOKAHEAD

Constructor Summary

SortedIterator(XPathContext context, SequenceIterator base, SortKeyEvaluator sortKeyEvaluator, AtomicComparer[] comparators)
Create a sorted iterator

Method Summary

protected void
buildArray()
Create an array holding the items to be sorted and the values of their sort keys
int
compare(int a, int b)
Compare two items in sorted sequence (needed to implement the Sortable interface)
Item
current()
SequenceIterator
getAnother()
int
getLastPosition()
Get the last position (that is, the number of items in the sequence).
int
getProperties()
Get properties of this iterator, as a bit-significant integer.
boolean
hasNext()
Determine whether there are more items to come.
Item
next()
Get the next item, in sorted order
int
position()
void
setHostLanguage(int language)
Set the host language
void
swap(int a, int b)
Swap two items (needed to implement the Sortable interface)

Field Details

base

protected SequenceIterator base

comparators

protected AtomicComparer[] comparators

context

protected XPathContext context

count

protected int count

nodeKeys

protected Object[] nodeKeys

position

protected int position

recordSize

protected int recordSize

sortKeyEvaluator

protected SortKeyEvaluator sortKeyEvaluator

Constructor Details

SortedIterator

public SortedIterator(XPathContext context,
                      SequenceIterator base,
                      SortKeyEvaluator sortKeyEvaluator,
                      AtomicComparer[] comparators)
Create a sorted iterator
Parameters:
context - the dynamic XPath evaluation context
base - an iterator over the sequence to be sorted
sortKeyEvaluator - an object that allows the n'th sort key for a given item to be evaluated
comparators - an array of AtomicComparers, one for each sort key, for comparing sort key values

Method Details

buildArray

protected void buildArray()
            throws XPathException
Create an array holding the items to be sorted and the values of their sort keys
Throws:
XPathException -

compare

public int compare(int a,
                   int b)
Compare two items in sorted sequence (needed to implement the Sortable interface)
Specified by:
compare in interface Sortable
Returns:
<0 if obj[a]0 if obj[a]>obj[b]

current

public Item current()
Specified by:
current in interface SequenceIterator

getAnother

public SequenceIterator getAnother()
            throws XPathException
Specified by:
getAnother in interface SequenceIterator

getLastPosition

public int getLastPosition()
            throws XPathException
Get the last position (that is, the number of items in the sequence). This method is non-destructive: it does not change the state of the iterator. The result is undefined if the next() method of the iterator has already returned null. This method must not be called unless the result of getProperties() on the iterator includes the bit setting SequenceIterator.LAST_POSITION_FINDER
Specified by:
getLastPosition in interface LastPositionFinder

getProperties

public int getProperties()
Get properties of this iterator, as a bit-significant integer.
Specified by:
getProperties in interface SequenceIterator
Returns:
the properties of this iterator. This will be some combination of properties such as SequenceIterator.GROUNDED, SequenceIterator.LAST_POSITION_FINDER, and SequenceIterator.LOOKAHEAD. It is always acceptable to return the value zero, indicating that there are no known special properties. It is acceptable for the properties of the iterator to change depending on its state.

hasNext

public boolean hasNext()
Specified by:
hasNext in interface LookaheadIterator
Returns:
true if there are more items in the sequence

next

public Item next()
            throws XPathException
Get the next item, in sorted order
Specified by:
next in interface SequenceIterator

position

public int position()
Specified by:
position in interface SequenceIterator

setHostLanguage

public void setHostLanguage(int language)
Set the host language

swap

public void swap(int a,
                 int b)
Swap two items (needed to implement the Sortable interface)
Specified by:
swap in interface Sortable