001: /*
002: * list.java
003: *
004: * Copyright (c) 1997-2007 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 list()
018: * function list(arg)
019: * function list(arg, type)
020: */
021: public class list extends PnutsFunction {
022:
023: public list() {
024: super ("list");
025: }
026:
027: static List getList(Context context, Object arg, String type) {
028: type = type.toUpperCase();
029: Class c;
030: if ("L".equals(type)) {
031: c = LinkedList.class;
032: } else if ("A".equals(type)) {
033: c = ArrayList.class;
034: } else if ("V".equals(type)) {
035: c = Vector.class;
036: } else {
037: throw new IllegalArgumentException();
038: }
039: List l;
040: try {
041: if (arg instanceof List) {
042: l = (List) c.newInstance();
043: for (Iterator it = ((List) arg).iterator(); it
044: .hasNext();) {
045: l.add(it.next());
046: }
047: return l;
048: } else if (arg instanceof Iterator) {
049: l = (List) c.newInstance();
050: for (Iterator it = (Iterator) arg; it.hasNext();) {
051: l.add(it.next());
052: }
053: return l;
054: } else if (arg instanceof Enumeration) {
055: l = (List) c.newInstance();
056: for (Enumeration en = (Enumeration) arg; en
057: .hasMoreElements();) {
058: l.add(en.nextElement());
059: }
060: return l;
061: } else if (Runtime.isArray(arg)) {
062: l = (List) c.newInstance();
063: int len = Runtime.getArrayLength(arg);
064: for (int i = 0; i < len; i++) {
065: l.add(Array.get(arg, i));
066: }
067: return l;
068: } else if (arg instanceof Generator) {
069: l = (List) c.newInstance();
070: final List lst = l;
071: PnutsFunction func = new PnutsFunction() {
072: protected Object exec(Object[] args, Context ctx) {
073: lst.add(args[0]);
074: return null;
075: }
076: };
077: Runtime.applyGenerator((Generator) arg, func, context);
078: return l;
079: } else {
080: Enumeration e = context.getConfiguration()
081: .toEnumeration(arg);
082: if (e != null) {
083: l = (List) c.newInstance();
084: for (Enumeration en = (Enumeration) e; en
085: .hasMoreElements();) {
086: l.add(en.nextElement());
087: }
088: return l;
089: } else {
090: throw new IllegalArgumentException(String
091: .valueOf(arg));
092: }
093: }
094: } catch (InstantiationException e1) {
095: throw new PnutsException(e1, context);
096: } catch (IllegalAccessException e2) {
097: throw new PnutsException(e2, context);
098: }
099: }
100:
101: public boolean defined(int nargs) {
102: return nargs >= 0 && nargs <= 2;
103: }
104:
105: protected Object exec(Object[] args, Context context) {
106: int nargs = args.length;
107: if (nargs == 0) {
108: return new ArrayList();
109: } else if (nargs == 1) {
110: return getList(context, args[0], "A");
111: } else if (nargs == 2) {
112: if (!(args[1] instanceof String)) {
113: throw new IllegalArgumentException();
114: }
115: return getList(context, args[0], (String) args[1]);
116: } else {
117: undefined(args, context);
118: return null;
119: }
120: }
121:
122: public String toString() {
123: return "function list({ arg {, type }})";
124: }
125: }
|