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