001: /*
002: * AggregateConfiguration.java
003: *
004: * Copyright (c) 2004-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 pnuts.lang.*;
012: import pnuts.lang.Runtime;
013: import pnuts.ext.*;
014: import java.util.*;
015: import java.lang.reflect.Array;
016:
017: /**
018: * Aggregate mode
019: *
020: */
021: public class AggregateConfiguration extends ConfigurationAdapter {
022:
023: public AggregateConfiguration() {
024: }
025:
026: public AggregateConfiguration(Configuration conf) {
027: super (conf);
028: }
029:
030: public Object getElement(Context context, Object target, Object key) {
031: if (target instanceof Collection) {
032: if (key instanceof PnutsFunction) {
033: return filter((Collection) target, (PnutsFunction) key,
034: context);
035: }
036: if (target instanceof List) {
037: if (key instanceof Number) {
038: return ((List) target).get(((Number) key)
039: .intValue());
040: } else {
041: ArrayList lst = new ArrayList();
042: for (Iterator it = ((List) target).iterator(); it
043: .hasNext();) {
044: lst.add(getElement(context, it.next(), key));
045: }
046: return lst;
047: }
048: } else {
049: HashSet set = new HashSet();
050: for (Iterator it = ((Set) target).iterator(); it
051: .hasNext();) {
052: set.add(getElement(context, it.next(), key));
053: }
054: return set;
055: }
056: } else {
057: return super .getElement(context, target, key);
058: }
059: }
060:
061: public void setElement(Context context, Object target, Object key,
062: Object value) {
063: if ((target instanceof Collection)
064: && (key instanceof PnutsFunction)) {
065: PnutsFunction f = (PnutsFunction) key;
066: if (target instanceof List) {
067: List lst = (List) target;
068: int n = lst.size();
069: for (int i = 0; i < n; i++) {
070: Object ret = f.call(new Object[] { lst.get(i) },
071: context);
072: if (Boolean.TRUE.equals(ret)) {
073: lst.set(i, value);
074: }
075: }
076: } else {
077: Collection c = (Collection) target;
078: ArrayList lst = new ArrayList();
079: for (Iterator it = c.iterator(); it.hasNext();) {
080: Object elem = it.next();
081: Object ret = f.call(new Object[] { elem }, context);
082: if (Boolean.TRUE.equals(ret)) {
083: lst.add(elem);
084: }
085: }
086: for (Iterator it = lst.iterator(); it.hasNext();) {
087: if (c.remove(it.next())) {
088: c.add(value);
089: }
090: }
091: }
092: } else {
093: super .setElement(context, target, key, value);
094: }
095: }
096:
097: public Object getField(Context context, Object target, String name) {
098: if (target instanceof Collection) {
099: Collection col = (Collection) target;
100: Collection c;
101: try {
102: c = (Collection) target.getClass().newInstance();
103: } catch (Exception e) {
104: if (target instanceof List) {
105: c = new ArrayList();
106: } else {
107: c = new HashSet();
108: }
109: }
110: for (Iterator it = col.iterator(); it.hasNext();) {
111: Object elem = it.next();
112: c.add(Runtime.getField(context, elem, name));
113: }
114: return c;
115: } else {
116: return super .getField(context, target, name);
117: }
118: }
119:
120: public void putField(Context context, Object target, String name,
121: Object value) {
122: if (target instanceof Collection) {
123: Collection col = (Collection) target;
124: for (Iterator it = col.iterator(); it.hasNext();) {
125: Object elem = it.next();
126: Runtime.putField(context, elem, name, value);
127: }
128: } else {
129: super .putField(context, target, name, value);
130: }
131: }
132:
133: protected Object filter(Collection c, PnutsFunction f,
134: Context context) {
135: Collection col;
136: try {
137: Class cls = c.getClass();
138: col = (Collection) cls.newInstance();
139: } catch (Exception e) {
140: if (c instanceof List) {
141: col = new ArrayList();
142: } else {
143: col = new HashSet();
144: }
145: }
146: for (Iterator it = c.iterator(); it.hasNext();) {
147: Object elem = it.next();
148: Object ret = f.call(new Object[] { elem }, context);
149: if (ret instanceof Boolean) {
150: if (((Boolean) ret).booleanValue()) {
151: col.add(elem);
152: }
153: }
154: }
155: return col;
156: }
157: }
|