01: // Copyright (c) 2003 Per M.A. Bothner
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.xquery.util;
05:
06: import gnu.lists.*;
07: import gnu.mapping.*;
08:
09: /** Used to implement 'some - satisfies' and 'every - satisfies'.
10: * A 2-argument Procedure (similar to ValuesMap), where the first
11: * argument is a Procedure that maps a value to a boolean, and
12: * the second argument is a sequence of values to pass to the former.
13: */
14:
15: public class ValuesEvery extends MethodProc {
16: public static final ValuesEvery every = new ValuesEvery(true);
17: public static final ValuesEvery some = new ValuesEvery(false);
18:
19: public ValuesEvery(boolean matchAll) {
20: this .matchAll = matchAll;
21: }
22:
23: /** True for every, false for some. */
24: boolean matchAll;
25:
26: public int numArgs() {
27: return 0x2002;
28: }
29:
30: public void apply(CallContext ctx) throws Throwable {
31: Procedure proc = (Procedure) ctx.getNextArg();
32: Consumer out = ctx.consumer;
33: Object val = ctx.getNextArg();
34: boolean ok = matchAll;
35: Procedure.checkArgCount(proc, 1);
36: if (val instanceof Values) {
37: int ipos = 0;
38: Values values = (Values) val;
39: while ((ipos = values.nextPos(ipos)) != 0) {
40: proc.check1(values.getPosPrevious(ipos), ctx);
41: ok = BooleanValue.booleanValue(ctx.runUntilValue());
42: if (ok != matchAll)
43: break;
44: }
45: } else {
46: proc.check1(val, ctx);
47: ok = BooleanValue.booleanValue(ctx.runUntilValue());
48: }
49: ctx.consumer.writeBoolean(ok);
50: }
51:
52: }
|