KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xquery > util > ItemAt


1 // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.xquery.util;
5 import gnu.mapping.*;
6
7 /** Returns a value at a given index in a sequence of values.
8  * Implements XQuery 'item-at'. */

9
10 public class ItemAt extends Procedure2
11 {
12   public static final ItemAt itemAt = new ItemAt();
13
14   static public Object JavaDoc itemAt (Object JavaDoc seq, int index)
15   {
16     if (seq instanceof Values)
17       {
18     Values vals = (Values) seq;
19     if (vals.isEmpty())
20       return Values.empty;
21     return vals.get(index - 1);
22       }
23     else
24       {
25     if (index != 1)
26       throw new IndexOutOfBoundsException JavaDoc();
27     return seq;
28       }
29   }
30
31   public Object JavaDoc apply2(Object JavaDoc arg1, Object JavaDoc arg2)
32   {
33     return itemAt(arg1, ((Number JavaDoc) arg2).intValue());
34   }
35 }
36
Popular Tags