net.sf.saxon.sort

Class GroupByIterator

public class GroupByIterator extends Object implements GroupIterator, LastPositionFinder, LookaheadIterator

A GroupByIterator iterates over a sequence of groups defined by xsl:for-each-group group-by="x". The groups are returned in order of first appearance. Note that an item can appear in several groups; indeed, an item may be the leading item of more than one group, which means that knowing the leading item is not enough to know the current group.

The GroupByIterator acts as a SequenceIterator, where successive calls of next() return the leading item of each group in turn. The current item of the iterator is therefore the leading item of the current group. To get access to all the members of the current group, the method iterateCurrentGroup() is used; this underpins the current-group() function in XSLT. The grouping key for the current group is available via the getCurrentGroupingKey() method.

Field Summary
protected AtomicComparercomparer
protected List<AtomicValue>groupKeys
protected List<List<Item>>groups
protected ExpressionkeyExpression
Constructor Summary
GroupByIterator(SequenceIterator population, Expression keyExpression, XPathContext keyContext, StringCollator collator)
Create a GroupByIterator
Method Summary
voidclose()
Itemcurrent()
SequenceIteratorgetAnother()
ListgetCurrentGroup()
Get the contents of the current group as a java List
AtomicValuegetCurrentGroupingKey()
Get the value of the grouping key for the current group
intgetLastPosition()
Get the last position (that is, the number of groups)
intgetProperties()
Get properties of this iterator, as a bit-significant integer.
booleanhasNext()
SequenceIteratoriterateCurrentGroup()
Get an iterator over the items in the current group
Itemnext()
intposition()
protected voidprocessItem(HashMap<ComparisonKey,List<Item>> index, Item item, XPathContext c2)
Process one item in the population

Field Detail

comparer

protected AtomicComparer comparer

groupKeys

protected List<AtomicValue> groupKeys

groups

protected List<List<Item>> groups

keyExpression

protected Expression keyExpression

Constructor Detail

GroupByIterator

public GroupByIterator(SequenceIterator population, Expression keyExpression, XPathContext keyContext, StringCollator collator)
Create a GroupByIterator

Parameters: population iterator over the population to be grouped keyExpression the expression used to calculate the grouping key keyContext dynamic context for calculating the grouping key collator Collation to be used for comparing grouping keys

Throws: XPathException

Method Detail

close

public void close()

current

public Item current()

getAnother

public SequenceIterator getAnother()

getCurrentGroup

public List getCurrentGroup()
Get the contents of the current group as a java List

Returns: the contents of the current group

getCurrentGroupingKey

public AtomicValue getCurrentGroupingKey()
Get the value of the grouping key for the current group

Returns: the grouping key, or null if the grouping key is an empty sequence

getLastPosition

public int getLastPosition()
Get the last position (that is, the number of groups)

getProperties

public int getProperties()
Get properties of this iterator, as a bit-significant integer.

Returns: the properties of this iterator. This will be some combination of properties such as GroupByIterator, GroupByIterator, and GroupByIterator. 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()

iterateCurrentGroup

public SequenceIterator iterateCurrentGroup()
Get an iterator over the items in the current group

Returns: the iterator

next

public Item next()

position

public int position()

processItem

protected void processItem(HashMap<ComparisonKey,List<Item>> index, Item item, XPathContext c2)
Process one item in the population

Parameters: index the index of items item the item from the population to be processed c2 the XPath evaluation context

Throws: XPathException