01: /*
02: * @(#)generator.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.lang.Runtime;
13: import java.util.Enumeration;
14:
15: public class generator extends PnutsFunction {
16:
17: public generator() {
18: super ("generator");
19: }
20:
21: public boolean defined(int narg) {
22: return (narg == 1);
23: }
24:
25: protected Object exec(Object[] args, final Context context) {
26: if (args.length != 1) {
27: undefined(args, context);
28: return null;
29: } else {
30: Object a0 = args[0];
31: if (a0 instanceof Generator) {
32: return a0;
33: } else {
34: final Enumeration en = Runtime.toEnumeration(a0,
35: context);
36: if (en != null) {
37: return new Generator() {
38: public Object apply(PnutsFunction closure,
39: Context c) {
40: Object[] a = new Object[1];
41: while (en.hasMoreElements()) {
42: a[0] = en.nextElement();
43: closure.call(a, context);
44: }
45: return null;
46: }
47: };
48: } else {
49: throw new IllegalArgumentException(String
50: .valueOf(a0));
51: }
52: }
53: }
54: }
55:
56: public String toString() {
57: return "function generator(elements)";
58: }
59: }
|