KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > CollectionManager


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34 package bsh;
35
36 import java.util.Enumeration JavaDoc;
37 import java.util.Vector JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.lang.reflect.Array JavaDoc;
40
41 /**
42     The default CollectionManager (which remains Java 1.1 compatible)
43     supports iteration over objects of type:
44     Enumeration, Vector, String, StringBuffer and array.
45     The dynamically loaded CollectionManagerImpl supports additional types when
46     it is present.
47
48     @see BshIterable.java
49 */

50 public class CollectionManager
51 {
52     private static CollectionManager manager;
53
54     public synchronized static CollectionManager getCollectionManager()
55     {
56         if ( manager == null
57             && Capabilities.classExists("java.util.Collection") )
58         {
59             Class JavaDoc clas;
60             try {
61                 clas = Class.forName( "bsh.collection.CollectionManagerImpl" );
62                 manager = (CollectionManager)clas.newInstance();
63             } catch ( Exception JavaDoc e ) {
64                 Interpreter.debug("unable to load CollectionManagerImpl: "+e);
65             }
66         }
67
68         if ( manager == null )
69             manager = new CollectionManager(); // default impl
70

71         return manager;
72     }
73
74     /**
75     */

76     public boolean isBshIterable( Object JavaDoc obj )
77     {
78         // This could be smarter...
79
try {
80             getBshIterator( obj );
81             return true;
82         } catch( IllegalArgumentException JavaDoc e ) {
83             return false;
84         }
85     }
86
87     public BshIterator getBshIterator( Object JavaDoc obj )
88         throws IllegalArgumentException JavaDoc
89     {
90         return new BasicBshIterator( obj );
91     }
92
93     public boolean isMap( Object JavaDoc obj ) {
94         return obj instanceof Hashtable JavaDoc;
95     }
96
97     public Object JavaDoc getFromMap( Object JavaDoc map, Object JavaDoc key ) {
98         return ((Hashtable JavaDoc)map).get(key);
99     }
100
101     public Object JavaDoc putInMap( Object JavaDoc map, Object JavaDoc key, Object JavaDoc value )
102     {
103         return ((Hashtable JavaDoc)map).put(key, value);
104     }
105
106     /**
107         Determine dynamically if the target is an iterator by the presence of a
108         pair of next() and hasNext() methods.
109     public static boolean isIterator() { }
110     */

111
112     /**
113      * An implementation that works with JDK 1.1
114      */

115     public static class BasicBshIterator implements BshIterator
116     {
117         Enumeration JavaDoc enumeration;
118         
119         /**
120          * Construct a basic BasicBshIterator
121          *
122          * @param The object over which we are iterating
123          *
124          * @throws java.lang.IllegalArgumentException If the argument is not a
125          * supported (i.e. iterable) type.
126          *
127          * @throws java.lang.NullPointerException If the argument is null
128          */

129         public BasicBshIterator(Object JavaDoc iterateOverMe) {
130             enumeration = createEnumeration(iterateOverMe);
131         }
132         
133         /**
134          * Create an enumeration over the given object
135          *
136          * @param iterateOverMe Object of type Enumeration, Vector, String,
137          * StringBuffer or an array
138          *
139          * @return an enumeration
140          *
141          * @throws java.lang.IllegalArgumentException If the argument is not a
142          * supported (i.e. iterable) type.
143          *
144          * @throws java.lang.NullPointerException If the argument is null
145          */

146         protected Enumeration JavaDoc createEnumeration( Object JavaDoc iterateOverMe )
147         {
148             if(iterateOverMe==null)
149                 throw new NullPointerException JavaDoc("Object arguments passed to " +
150                     "the BasicBshIterator constructor cannot be null.");
151
152             if (iterateOverMe instanceof Enumeration JavaDoc)
153                 return (Enumeration JavaDoc)iterateOverMe;
154
155             if (iterateOverMe instanceof Vector JavaDoc)
156                 return ((Vector JavaDoc)iterateOverMe).elements();
157
158             if (iterateOverMe.getClass().isArray()) {
159                 final Object JavaDoc array = iterateOverMe;
160                 return new Enumeration JavaDoc() {
161                     int index = 0, length = Array.getLength(array);
162                     public Object JavaDoc nextElement() {
163                         return Array.get(array, index++);
164                     }
165                     public boolean hasMoreElements() { return index<length; }
166                 };
167             }
168             
169             if (iterateOverMe instanceof String JavaDoc)
170                 return createEnumeration(((String JavaDoc)iterateOverMe).toCharArray());
171             
172             if (iterateOverMe instanceof StringBuffer JavaDoc)
173                 return createEnumeration(
174                     iterateOverMe.toString().toCharArray());
175
176             throw new IllegalArgumentException JavaDoc(
177                 "Cannot enumerate object of type "+iterateOverMe.getClass());
178         }
179         
180         /**
181          * Fetch the next object in the iteration
182          *
183          * @return The next object
184          */

185         public Object JavaDoc next() {
186             return enumeration.nextElement();
187         }
188         
189         /**
190          * Returns true if and only if there are more objects available
191          * via the <code>next()</code> method
192          *
193          * @return The next object
194          */

195         public boolean hasNext() {
196             return enumeration.hasMoreElements();
197         }
198     }
199 }
200
Popular Tags