KickJava   Java API By Example, From Geeks To Geeks.

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


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 import gnu.lists.*;
7
8 /** Extracts a sub-range from a value sequence.
9  * Implements XQuery 'sublist'. */

10
11 public class SubList extends MethodProc
12 {
13   public static final SubList subList = new SubList();
14
15   public int numArgs() { return 0x3002; }
16
17   public static void subList(Object JavaDoc seq, double start, double end,
18                              Consumer out)
19   {
20     if (seq instanceof Values)
21       {
22     Values vals = (Values) seq;
23         int n = 0;
24     int i = 0;
25     while (++n < start)
26       {
27         i = vals.nextDataIndex(i);
28         if (i < 0)
29           return;
30       }
31     int startPosition = i;
32     int endPosition = i;
33     while (n++ < end)
34       {
35         i = vals.nextDataIndex(i);
36         if (i < 0)
37           break;
38         endPosition = i;
39       }
40     vals.consumeIRange(startPosition, endPosition, out);
41       }
42     else
43       {
44     if (start <= 1 && end >= 2)
45       out.writeObject(seq);
46       }
47   }
48
49   public void apply (CallContext ctx)
50   {
51     Consumer consumer = ctx.consumer;
52     Object JavaDoc seq = ctx.getNextArg();
53     double d1 = Math.round(StringUtils.asDouble(ctx.getNextArg()));
54     Object JavaDoc eof = Sequence.eofValue;
55     Object JavaDoc arg2 = ctx.getNextArg(eof);
56     ctx.lastArg();
57     double d2 = arg2 != eof ? Math.round(StringUtils.asDouble(arg2))
58       : Double.POSITIVE_INFINITY;
59     subList(seq, d1, d1 + d2, consumer);
60   }
61 }
62
Popular Tags