001 /*****************************************************************************
002 * *
003 * This file is part of the BeanShell Java Scripting distribution. *
004 * Documentation and updates may be found at http://www.beanshell.org/ *
005 * *
006 * Sun Public License Notice: *
007 * *
008 * The contents of this file are subject to the Sun Public License Version *
009 * 1.0 (the "License"); you may not use this file except in compliance with *
010 * the License. A copy of the License is available at http://www.sun.com *
011 * *
012 * The Original Code is BeanShell. The Initial Developer of the Original *
013 * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014 * (C) 2000. All Rights Reserved. *
015 * *
016 * GNU Public License Notice: *
017 * *
018 * Alternatively, the contents of this file may be used under the terms of *
019 * the GNU Lesser General Public License (the "LGPL"), in which case the *
020 * provisions of LGPL are applicable instead of those above. If you wish to *
021 * allow use of your version of this file only under the terms of the LGPL *
022 * and not to allow others to use your version of this file under the SPL, *
023 * indicate your decision by deleting the provisions above and replace *
024 * them with the notice and other provisions required by the LGPL. If you *
025 * do not delete the provisions above, a recipient may use your version of *
026 * this file under either the SPL or the LGPL. *
027 * *
028 * Patrick Niemeyer (pat@pat.net) *
029 * Author of Learning Java, O'Reilly & Associates *
030 * http://www.pat.net/~pat/ *
031 * *
032 *****************************************************************************/
033
034 package bsh;
035
036 import java.util.Enumeration;
037 import java.util.Vector;
038 import java.util.Hashtable;
039 import java.lang.reflect.Array;
040
041 /**
042 The default CollectionManager (which remains Java 1.1 compatible)
043 supports iteration over objects of type:
044 Enumeration, Vector, String, StringBuffer and array.
045 The dynamically loaded CollectionManagerImpl supports additional types when
046 it is present.
047
048 @see BshIterable.java
049 */
050 public class CollectionManager
051 {
052 private static CollectionManager manager;
053
054 public synchronized static CollectionManager getCollectionManager()
055 {
056 if ( manager == null
057 && Capabilities.classExists("java.util.Collection") )
058 {
059 Class clas;
060 try {
061 clas = Class.forName( "bsh.collection.CollectionManagerImpl" );
062 manager = (CollectionManager)clas.newInstance();
063 } catch ( Exception e ) {
064 Interpreter.debug("unable to load CollectionManagerImpl: "+e);
065 }
066 }
067
068 if ( manager == null )
069 manager = new CollectionManager(); // default impl
070
071 return manager;
072 }
073
074 /**
075 */
076 public boolean isBshIterable( Object obj )
077 {
078 // This could be smarter...
079 try {
080 getBshIterator( obj );
081 return true;
082 } catch( IllegalArgumentException e ) {
083 return false;
084 }
085 }
086
087 public BshIterator getBshIterator( Object obj )
088 throws IllegalArgumentException
089 {
090 return new BasicBshIterator( obj );
091 }
092
093 public boolean isMap( Object obj ) {
094 return obj instanceof Hashtable;
095 }
096
097 public Object getFromMap( Object map, Object key ) {
098 return ((Hashtable)map).get(key);
099 }
100
101 public Object putInMap( Object map, Object key, Object value )
102 {
103 return ((Hashtable)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 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 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 createEnumeration( Object iterateOverMe )
147 {
148 if(iterateOverMe==null)
149 throw new NullPointerException("Object arguments passed to " +
150 "the BasicBshIterator constructor cannot be null.");
151
152 if (iterateOverMe instanceof Enumeration)
153 return (Enumeration)iterateOverMe;
154
155 if (iterateOverMe instanceof Vector)
156 return ((Vector)iterateOverMe).elements();
157
158 if (iterateOverMe.getClass().isArray()) {
159 final Object array = iterateOverMe;
160 return new Enumeration() {
161 int index = 0, length = Array.getLength(array);
162 public Object nextElement() {
163 return Array.get(array, index++);
164 }
165 public boolean hasMoreElements() { return index<length; }
166 };
167 }
168
169 if (iterateOverMe instanceof String)
170 return createEnumeration(((String)iterateOverMe).toCharArray());
171
172 if (iterateOverMe instanceof StringBuffer)
173 return createEnumeration(
174 iterateOverMe.toString().toCharArray());
175
176 throw new IllegalArgumentException(
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 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 }