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