PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/CollectionManager.java

#
Java | 199 lines | 90 code | 23 blank | 86 comment | 11 complexity | 34c0fb949387d8a773ae2e77cf126dd7 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package bsh;
  34. import java.util.Enumeration;
  35. import java.util.Vector;
  36. import java.util.Hashtable;
  37. import java.lang.reflect.Array;
  38. /**
  39. The default CollectionManager (which remains Java 1.1 compatible)
  40. supports iteration over objects of type:
  41. Enumeration, Vector, String, StringBuffer and array.
  42. The dynamically loaded CollectionManagerImpl supports additional types when
  43. it is present.
  44. @see BshIterable.java
  45. */
  46. public class CollectionManager
  47. {
  48. private static CollectionManager manager;
  49. public synchronized static CollectionManager getCollectionManager()
  50. {
  51. if ( manager == null
  52. && Capabilities.classExists("java.util.Collection") )
  53. {
  54. Class clas;
  55. try {
  56. clas = Class.forName( "bsh.collection.CollectionManagerImpl" );
  57. manager = (CollectionManager)clas.newInstance();
  58. } catch ( Exception e ) {
  59. Interpreter.debug("unable to load CollectionManagerImpl: "+e);
  60. }
  61. }
  62. if ( manager == null )
  63. manager = new CollectionManager(); // default impl
  64. return manager;
  65. }
  66. /**
  67. */
  68. public boolean isBshIterable( Object obj )
  69. {
  70. // This could be smarter...
  71. try {
  72. getBshIterator( obj );
  73. return true;
  74. } catch( IllegalArgumentException e ) {
  75. return false;
  76. }
  77. }
  78. public BshIterator getBshIterator( Object obj )
  79. throws IllegalArgumentException
  80. {
  81. return new BasicBshIterator( obj );
  82. }
  83. public boolean isMap( Object obj ) {
  84. return obj instanceof Hashtable;
  85. }
  86. public Object getFromMap( Object map, Object key ) {
  87. return ((Hashtable)map).get(key);
  88. }
  89. public Object putInMap( Object map, Object key, Object value )
  90. {
  91. return ((Hashtable)map).put(key, value);
  92. }
  93. /**
  94. Determine dynamically if the target is an iterator by the presence of a
  95. pair of next() and hasNext() methods.
  96. public static boolean isIterator() { }
  97. */
  98. /**
  99. * An implementation that works with JDK 1.1
  100. */
  101. public static class BasicBshIterator implements BshIterator
  102. {
  103. Enumeration enumeration;
  104. /**
  105. * Construct a basic BasicBshIterator
  106. *
  107. * @param The object over which we are iterating
  108. *
  109. * @throws java.lang.IllegalArgumentException If the argument is not a
  110. * supported (i.e. iterable) type.
  111. *
  112. * @throws java.lang.NullPointerException If the argument is null
  113. */
  114. public BasicBshIterator(Object iterateOverMe) {
  115. enumeration = createEnumeration(iterateOverMe);
  116. }
  117. /**
  118. * Create an enumeration over the given object
  119. *
  120. * @param iterateOverMe Object of type Enumeration, Vector, String,
  121. * StringBuffer or an array
  122. *
  123. * @return an enumeration
  124. *
  125. * @throws java.lang.IllegalArgumentException If the argument is not a
  126. * supported (i.e. iterable) type.
  127. *
  128. * @throws java.lang.NullPointerException If the argument is null
  129. */
  130. protected Enumeration createEnumeration( Object iterateOverMe )
  131. {
  132. if(iterateOverMe==null)
  133. throw new NullPointerException("Object arguments passed to " +
  134. "the BasicBshIterator constructor cannot be null.");
  135. if (iterateOverMe instanceof Enumeration)
  136. return (Enumeration)iterateOverMe;
  137. if (iterateOverMe instanceof Vector)
  138. return ((Vector)iterateOverMe).elements();
  139. if (iterateOverMe.getClass().isArray()) {
  140. final Object array = iterateOverMe;
  141. return new Enumeration() {
  142. int index = 0, length = Array.getLength(array);
  143. public Object nextElement() {
  144. return Array.get(array, index++);
  145. }
  146. public boolean hasMoreElements() { return index<length; }
  147. };
  148. }
  149. if (iterateOverMe instanceof String)
  150. return createEnumeration(((String)iterateOverMe).toCharArray());
  151. if (iterateOverMe instanceof StringBuffer)
  152. return createEnumeration(
  153. iterateOverMe.toString().toCharArray());
  154. throw new IllegalArgumentException(
  155. "Cannot enumerate object of type "+iterateOverMe.getClass());
  156. }
  157. /**
  158. * Fetch the next object in the iteration
  159. *
  160. * @return The next object
  161. */
  162. public Object next() {
  163. return enumeration.nextElement();
  164. }
  165. /**
  166. * Returns true if and only if there are more objects available
  167. * via the <code>next()</code> method
  168. *
  169. * @return The next object
  170. */
  171. public boolean hasNext() {
  172. return enumeration.hasMoreElements();
  173. }
  174. }
  175. }