KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package gnu.xquery.util;
5 import gnu.lists.*;
6 import gnu.mapping.*;
7
8 /** Used to implement 'some - satisfies' and 'every - satisfies'.
9  * A 2-argument Procedure (similar to ValuesMap), where the first
10  * argument is a Procedure that maps a value to a boolean, and
11  * the second argument is a sequence of values to pass to the former.
12  */

13
14 public class ValuesEvery extends MethodProc
15 {
16   public static final ValuesEvery every = new ValuesEvery(true);
17   public static final ValuesEvery some = new ValuesEvery(false);
18
19   public ValuesEvery(boolean matchAll) {this.matchAll = matchAll; }
20
21   /** True for every, false for some. */
22   boolean matchAll;
23
24   public int numArgs() { return 0x2002; }
25
26   public void apply (CallContext ctx) throws Throwable JavaDoc
27   {
28     Procedure proc = (Procedure) ctx.getNextArg();
29     Consumer out = ctx.consumer;
30     Object JavaDoc val = ctx.getNextArg();
31     boolean ok = matchAll;
32     Procedure.checkArgCount(proc, 1);
33     if (val instanceof Values)
34       {
35     int ipos = 0;
36     Values values = (Values) val;
37     while ((ipos = values.nextPos(ipos)) != 0)
38       {
39         proc.check1(values.getPosPrevious(ipos), ctx);
40         ok = BooleanValue.booleanValue(ctx.runUntilValue());
41         if (ok != matchAll)
42           break;
43       }
44       }
45     else
46       {
47     proc.check1(val, ctx);
48     ok = BooleanValue.booleanValue(ctx.runUntilValue());
49       }
50     ctx.consumer.writeBoolean(ok);
51   }
52
53 }
54
Popular Tags