001: /*
002: * map.java
003: *
004: * Copyright (c) 1997-2006 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.util.*;
012: import java.lang.reflect.*;
013: import pnuts.lang.*;
014: import pnuts.lang.Runtime;
015: import pnuts.lang.Package;
016:
017: /*
018: * function map()
019: * function map(arg)
020: * function map(arg, type)
021: * function map(arg, func)
022: * function map(arg, func, intValue)
023: */
024: public class map extends PnutsFunction {
025:
026: private final static Integer ZERO = new Integer(0);
027: private final static Integer ONE = new Integer(1);
028:
029: public map() {
030: super ("map");
031: }
032:
033: static Map createMap(String type, Context context) {
034: try {
035: type = type.toUpperCase();
036: Class c;
037: if ("H".equals(type)) {
038: c = HashMap.class;
039: } else if ("HT".equals(type)) {
040: c = Hashtable.class;
041: } else if ("LH".equals(type)) {
042: c = LinkedHashMap.class;
043: } else if ("WH".equals(type)) {
044: c = WeakHashMap.class;
045: } else if ("T".equals(type)) {
046: c = TreeMap.class;
047: } else if ("P".equals(type)) {
048: c = Properties.class;
049: } else if ("IH".equals(type)) {
050: c = IdentityHashMap.class;
051: } else {
052: throw new IllegalArgumentException();
053: }
054: return (Map) c.newInstance();
055: } catch (InstantiationException e1) {
056: throw new PnutsException(e1, context);
057: } catch (IllegalAccessException e2) {
058: throw new PnutsException(e2, context);
059: }
060: }
061:
062: static Map getMap(Context context, Object arg, String type) {
063: Map m = createMap(type, context);
064: populate(context, arg, m);
065: return m;
066: }
067:
068: static Map populate(Context context, Object arg, Map map) {
069: final Configuration conf = context.getConfiguration();
070: if (arg == null) {
071: return map;
072: } else if (arg instanceof Map) {
073: map.putAll((Map) arg);
074: return map;
075: } else if (arg instanceof Iterator) {
076: for (Iterator it = (Iterator) arg; it.hasNext();) {
077: Object elem = it.next();
078: map.put(conf.getElement(context, elem, ZERO), conf
079: .getElement(context, elem, ONE));
080: }
081: return map;
082: } else if (arg instanceof Enumeration) {
083: for (Enumeration en = (Enumeration) arg; en
084: .hasMoreElements();) {
085: Object elem = en.nextElement();
086: map.put(conf.getElement(context, elem, ZERO), conf
087: .getElement(context, elem, ONE));
088: }
089: return map;
090: } else if (Runtime.isArray(arg)) {
091: int len = Runtime.getArrayLength(arg);
092: for (int i = 0; i < len; i++) {
093: Object elem = Array.get(arg, i);
094: map.put(conf.getElement(context, elem, ZERO), conf
095: .getElement(context, elem, ONE));
096: }
097: return map;
098: } else if (arg instanceof List) {
099: List lst = (List) arg;
100: int len = lst.size();
101: for (int i = 0; i < len; i++) {
102: Object elem = lst.get(i);
103: map.put(conf.getElement(context, elem, ZERO), conf
104: .getElement(context, elem, ONE));
105: }
106: return map;
107: } else if (arg instanceof Generator) {
108: final Map m = map;
109: Runtime.applyGenerator((Generator) arg,
110: new PnutsFunction() {
111: protected Object exec(Object[] args,
112: Context context) {
113: Object elem = args[0];
114: m
115: .put(conf.getElement(context, elem,
116: ZERO), conf.getElement(
117: context, elem, ONE));
118: return null;
119: }
120: }, context);
121: return m;
122: } else if (arg instanceof String) {
123: return (Map) new MapPackage.Function().call(new Object[] {
124: map, arg }, context);
125: } else if (arg instanceof Package) {
126: return ((Package) arg).asMap();
127: } else {
128: throw new IllegalArgumentException(String.valueOf(arg));
129: }
130: }
131:
132: public boolean defined(int nargs) {
133: return nargs >= 0 && nargs <= 3;
134: }
135:
136: protected Object exec(Object[] args, Context context) {
137: int nargs = args.length;
138: if (nargs == 0) {
139: return new HashMap();
140: } else if (nargs == 1) {
141: return getMap(context, args[0], "H");
142: } else if (nargs == 2) {
143: Object arg1 = args[1];
144: if (arg1 instanceof PnutsFunction) {
145: Map m = new TreeMap(new FunctionComparator(
146: (PnutsFunction) arg1, context));
147: m = populate(context, args[0], m);
148: return m;
149: } else if (arg1 instanceof String) {
150: return getMap(context, args[0], (String) args[1]);
151: } else {
152: throw new IllegalArgumentException();
153: }
154: } else if (nargs == 3) {
155: Object arg1 = args[1];
156: if (arg1 instanceof PnutsFunction) { // TreeSet
157: Object arg2 = args[2];
158: if (arg2 instanceof Number) {
159: Map m = new TreeMap(new FunctionComparator(
160: (PnutsFunction) arg1, context,
161: ((Number) arg2).intValue()));
162: populate(context, args[0], m);
163: return m;
164: }
165: }
166: throw new IllegalArgumentException();
167: } else {
168: undefined(args, context);
169: return null;
170: }
171: }
172:
173: public String toString() {
174: return "function map({ arg {, type }}) or (arg, func {, num})";
175: }
176: }
|