Source Code Cross Referenced for AggregateConfiguration.java in  » Scripting » Pnuts » org » pnuts » lib » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » Pnuts » org.pnuts.lib 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.