01: /*
02: * @(#)aggregateMode.java 1.2 04/12/06
03: *
04: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import pnuts.lang.*;
12: import pnuts.ext.*;
13:
14: public class aggregateMode extends PnutsFunction {
15:
16: private static final String AGGREGATE_MODE = "pnuts.lib.aggregateMode"
17: .intern();
18:
19: public aggregateMode() {
20: super ("aggregateMode");
21: }
22:
23: public boolean defined(int nargs) {
24: return nargs == 0 || nargs == 1;
25: }
26:
27: static boolean getAggregateMode(Context context) {
28: Object mode = context.get(AGGREGATE_MODE);
29: return (mode instanceof Configuration)
30: && !(mode instanceof AggregateConfiguration);
31: }
32:
33: static void setAggregateMode(Context context, boolean mode) {
34: Configuration conf = context.getConfiguration();
35: if (mode) {
36: context.set(AGGREGATE_MODE, conf);
37: context.setConfiguration(new AggregateConfiguration(conf));
38: } else {
39: if (conf instanceof Configuration) {
40: context.set(AGGREGATE_MODE, null);
41: context.setConfiguration(conf);
42: }
43: }
44: }
45:
46: protected Object exec(Object[] args, Context context) {
47: int nargs = args.length;
48: if (nargs == 0) {
49: return Boolean.valueOf(getAggregateMode(context));
50: } else if (nargs == 1) {
51: boolean mode = ((Boolean) args[0]).booleanValue();
52: setAggregateMode(context, mode);
53: return null;
54: } else {
55: undefined(args, context);
56: return null;
57: }
58: }
59:
60: public String toString() {
61: return "function aggregateMode({ mode })";
62: }
63: }
|