01: // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.xquery.util;
05:
06: import gnu.mapping.*;
07:
08: /** Returns a value at a given index in a sequence of values.
09: * Implements XQuery 'item-at'. */
10:
11: public class ItemAt extends Procedure2 {
12: public static final ItemAt itemAt = new ItemAt();
13:
14: static public Object itemAt(Object seq, int index) {
15: if (seq instanceof Values) {
16: Values vals = (Values) seq;
17: if (vals.isEmpty())
18: return Values.empty;
19: return vals.get(index - 1);
20: } else {
21: if (index != 1)
22: throw new IndexOutOfBoundsException();
23: return seq;
24: }
25: }
26:
27: public Object apply2(Object arg1, Object arg2) {
28: return itemAt(arg1, ((Number) arg2).intValue());
29: }
30: }
|