final int len = specs.length;
final Item[] groupKeys = new Item[len];
final Sequence[] rawKeys = new Sequence[len];
for(int i = 0; i < len; i++) {
GroupingSpec spec = specs[i];
Sequence result = spec.getKeyExpr().eval(contextSeq, dynEnv);
Sequence atomized = result.atomize(dynEnv);
IFocus atomizedItor = atomized.iterator();
final Item groupKey;
if(atomizedItor.hasNext()) {
AtomicValue atom = (AtomicValue) atomizedItor.next();
if(atomizedItor.hasNext()) {
atomizedItor.closeQuietly();
throw new DynamicError("err:XQDY0095", "Illegal resulting value for a grouping variable: "
+ atomized);
}
groupKey = atom.asGroupingValue();
} else {
groupKey = ValueSequence.EMPTY_SEQUENCE;
}
atomizedItor.closeQuietly();
groupKeys[i] = groupKey;
rawKeys[i] = result;
}
final GroupKeys k = new GroupKeys(specs, groupKeys, rawKeys, dynEnv);
final GroupEntry v = new GroupEntry(it, dynEnv);
final GroupEntry prevEntry = map.put(k, v);
if(prevEntry != null) {
v.setNonGroupingVaribales(prevEntry.getNonGroupingVaribales());
final Sequence prevValue = prevEntry.getValue();
if(prevValue instanceof ValueSequence) {
((ValueSequence) prevValue).addItem(it);
v.setValue(prevValue);
} else {
final List<Item> list = new ArrayList<Item>(4);