001: /*
002: * set.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:
016: /*
017: * function set()
018: * function set(arg)
019: * function set(arg, type)
020: * function set(arg, func)
021: * function set(arg, func, intValue)
022: */
023: public class set extends PnutsFunction {
024:
025: public set() {
026: super ("set");
027: }
028:
029: static Set createSet(String type, Context context) {
030: try {
031: type = type.toUpperCase();
032: Class c;
033: if ("H".equals(type)) {
034: c = HashSet.class;
035: } else if ("LH".equals(type)) {
036: c = LinkedHashSet.class;
037: } else if ("T".equals(type)) {
038: c = TreeSet.class;
039: } else {
040: throw new IllegalArgumentException();
041: }
042: return (Set) c.newInstance();
043: } catch (InstantiationException e1) {
044: throw new PnutsException(e1, context);
045: } catch (IllegalAccessException e2) {
046: throw new PnutsException(e2, context);
047: }
048: }
049:
050: static Set getSet(Context context, Object arg, String type) {
051: Set set = createSet(type, context);
052: populate(context, arg, set);
053: return set;
054: }
055:
056: static void populate(Context context, Object arg, Set set) {
057: if (arg == null) {
058: return;
059: } else if (arg instanceof Collection) {
060: set.addAll((Collection) arg);
061: } else if (arg instanceof Iterator) {
062: for (Iterator it = (Iterator) arg; it.hasNext();) {
063: set.add(it.next());
064: }
065: } else if (arg instanceof Enumeration) {
066: for (Enumeration en = (Enumeration) arg; en
067: .hasMoreElements();) {
068: set.add(en.nextElement());
069: }
070: } else if (Runtime.isArray(arg)) {
071: int len = Runtime.getArrayLength(arg);
072: for (int i = 0; i < len; i++) {
073: set.add(Array.get(arg, i));
074: }
075: } else if (arg instanceof Generator) {
076: final Set s = set;
077: PnutsFunction func = new PnutsFunction() {
078: protected Object exec(Object[] args, Context ctx) {
079: s.add(args[0]);
080: return null;
081: }
082: };
083: Runtime.applyGenerator((Generator) arg, func, context);
084: } else {
085: Enumeration e = context.getConfiguration().toEnumeration(
086: arg);
087: for (Enumeration en = (Enumeration) arg; en
088: .hasMoreElements();) {
089: set.add(en.nextElement());
090: }
091: }
092:
093: }
094:
095: public boolean defined(int nargs) {
096: return nargs >= 0 && nargs <= 3;
097: }
098:
099: protected Object exec(Object[] args, Context context) {
100: int nargs = args.length;
101: if (nargs == 0) {
102: return new HashSet();
103: } else if (nargs == 1) {
104: return getSet(context, args[0], "H");
105: } else if (nargs == 2) {
106: Object arg1 = args[1];
107: if (arg1 instanceof PnutsFunction) { // TreeSet
108: Set s = new TreeSet(new FunctionComparator(
109: (PnutsFunction) arg1, context));
110: populate(context, args[0], s);
111: return s;
112: } else if (arg1 instanceof String) {
113: return getSet(context, args[0], (String) args[1]);
114: } else {
115: throw new IllegalArgumentException();
116: }
117: } else if (nargs == 3) {
118: Object arg1 = args[1];
119: if (arg1 instanceof PnutsFunction) { // TreeSet
120: Object arg2 = args[2];
121: if (arg2 instanceof Number) {
122: Set s = new TreeSet(new FunctionComparator(
123: (PnutsFunction) arg1, context,
124: ((Number) arg2).intValue()));
125: populate(context, args[0], s);
126: return s;
127: }
128: }
129: throw new IllegalArgumentException();
130: } else {
131: undefined(args, context);
132: return null;
133: }
134: }
135:
136: public String toString() {
137: return "function set({ arg {, type }}) or (arg, func {, num})";
138: }
139: }
|