PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 10ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/CollectionManager.java

#
Java | 199 lines | 90 code | 23 blank | 86 comment | 11 complexity | 34c0fb949387d8a773ae2e77cf126dd7 MD5 | raw file
  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
 34package bsh;
 35
 36import java.util.Enumeration;
 37import java.util.Vector;
 38import java.util.Hashtable;
 39import java.lang.reflect.Array;
 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*/
 50public 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 clas;
 60			try {
 61				clas = Class.forName( "bsh.collection.CollectionManagerImpl" );
 62				manager = (CollectionManager)clas.newInstance();
 63			} catch ( Exception 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 obj ) 
 77	{
 78		// This could be smarter...
 79		try { 
 80			getBshIterator( obj ); 
 81			return true;
 82		} catch( IllegalArgumentException e ) { 
 83			return false;
 84		}
 85	}
 86
 87	public BshIterator getBshIterator( Object obj ) 
 88		throws IllegalArgumentException
 89	{
 90		return new BasicBshIterator( obj ); 
 91	}
 92
 93	public boolean isMap( Object obj ) {
 94		return obj instanceof Hashtable;
 95	}
 96
 97	public Object getFromMap( Object map, Object key ) {
 98		return ((Hashtable)map).get(key);
 99	}
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}