001: /*
002: * @(#)mapFunction.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import java.lang.reflect.Array;
012: import java.util.*;
013: import pnuts.lang.*;
014: import pnuts.lang.Runtime;
015: import pnuts.lang.Package;
016:
017: public class mapFunction extends PnutsFunction {
018:
019: public mapFunction() {
020: super ("mapFunction");
021: }
022:
023: public boolean defined(int nargs) {
024: return (nargs == 2 || nargs == 3);
025: }
026:
027: protected Object exec(Object args[], final Context context) {
028: int nargs = args.length;
029: if (nargs != 2 && nargs != 3) {
030: undefined(args, context);
031: }
032: final PnutsFunction func = (PnutsFunction) args[0];
033: Object array = args[1];
034:
035: int len = 0;
036: int type1 = 0; // 1==array, 2==Collection, 3==Iterator, 4==Enumeration, 5==Map
037: if (Runtime.isArray(array)) {
038: len = Runtime.getArrayLength(array);
039: type1 = 1;
040: } else if (array instanceof Collection) {
041: len = ((Collection) array).size();
042: type1 = 2;
043: } else if (array instanceof Iterator) {
044: type1 = 3;
045: } else if (array instanceof Enumeration) {
046: type1 = 4;
047: } else if (array instanceof Map) {
048: type1 = 5;
049: } else if (array instanceof Package) {
050: type1 = 6;
051: } else if (array instanceof Generator) {
052: type1 = 7;
053: } else {
054: throw new IllegalArgumentException();
055: }
056:
057: Collection output = null;
058: if (nargs == 3) {
059: if (args[2] instanceof Collection) {
060: output = (Collection) args[2];
061: } else {
062: throw new IllegalArgumentException();
063: }
064: }
065:
066: if (type1 == 1) { // array
067: if (output == null) {
068: for (int i = 0; i < len; ++i) {
069: func.call(new Object[] { Array.get(array, i) },
070: context);
071: }
072: } else {
073: for (int i = 0; i < len; ++i) {
074: output.add(func.call(new Object[] { Array.get(
075: array, i) }, context));
076: }
077: }
078: } else if (type1 == 2) { // Collection
079: if (output == null) {
080: for (Iterator it = ((Collection) array).iterator(); it
081: .hasNext();) {
082: func.call(new Object[] { it.next() }, context);
083: }
084: } else {
085: for (Iterator it = ((Collection) array).iterator(); it
086: .hasNext();) {
087: output.add(func.call(new Object[] { it.next() },
088: context));
089: }
090: }
091: } else if (type1 == 3) { // Iterator
092: if (output == null) {
093: for (Iterator it = (Iterator) array; it.hasNext();) {
094: func.call(new Object[] { it.next() }, context);
095: }
096: } else {
097: for (Iterator it = (Iterator) array; it.hasNext();) {
098: output.add(func.call(new Object[] { it.next() },
099: context));
100: }
101: }
102: } else if (type1 == 4) { // Enumeration
103: if (output == null) {
104: for (Enumeration e = (Enumeration) array; e
105: .hasMoreElements();) {
106: func
107: .call(new Object[] { e.nextElement() },
108: context);
109: }
110: } else {
111: for (Enumeration e = (Enumeration) array; e
112: .hasMoreElements();) {
113: output.add(func.call(
114: new Object[] { e.nextElement() }, context));
115: }
116: }
117: } else if (type1 == 5) { // Map
118: if (output == null) {
119: for (Iterator it = ((Map) array).entrySet().iterator(); it
120: .hasNext();) {
121: Map.Entry entry = (Map.Entry) it.next();
122: func.call(new Object[] { entry.getKey(),
123: entry.getValue() }, context);
124: }
125: } else {
126: for (Iterator it = ((Map) array).entrySet().iterator(); it
127: .hasNext();) {
128: Map.Entry entry = (Map.Entry) it.next();
129: output.add(func.call(new Object[] { entry.getKey(),
130: entry.getValue() }, context));
131: }
132: }
133: } else if (type1 == 6) { // Package
134: if (output == null) {
135: for (Enumeration e = ((Package) array).bindings(); e
136: .hasMoreElements();) {
137: NamedValue value = (NamedValue) e.nextElement();
138: func.call(new Object[] { value.getName(),
139: value.get() }, context);
140: }
141: } else {
142: for (Enumeration e = ((Package) array).bindings(); e
143: .hasMoreElements();) {
144: NamedValue value = (NamedValue) e.nextElement();
145: output.add(func.call(new Object[] {
146: value.getName(), value.get() }, context));
147: }
148: }
149: } else if (type1 == 7) {
150: Generator g = (Generator) array;
151: if (output == null) {
152: return Runtime.applyGenerator(g, new PnutsFunction() {
153: protected Object exec(Object[] args, Context ctx) {
154: func.call(args, ctx);
155: return null;
156: }
157: }, context);
158: } else {
159: final Collection output2 = output;
160: return Runtime.applyGenerator(g, new PnutsFunction() {
161: protected Object exec(Object[] args, Context ctx) {
162: output2.add(func.call(args, ctx));
163: return null;
164: }
165: }, context);
166: }
167: }
168: return null;
169: }
170:
171: public String toString() {
172: return "function mapFunction(func, (array|Collection|Iterator|Enumeration|Map|Package|Generator ) {, Collection } )";
173: }
174: }
|