PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/collection/CollectionIterator.java

#
Java | 89 lines | 30 code | 11 blank | 48 comment | 3 complexity | 10562d39d2484ad704dd873a1b762549 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. package bsh.collection;
  2. import java.util.Iterator;
  3. import java.util.Collection;
  4. import java.util.Enumeration;
  5. //import java.util.Map;
  6. import java.lang.reflect.Array;
  7. /**
  8. * This is the implementation of:
  9. * BshIterator - a dynamically loaded extension that supports the collections
  10. * API supported by JDK1.2 and greater.
  11. *
  12. * @author Daniel Leuck
  13. * @author Pat Niemeyer
  14. */
  15. public class CollectionIterator implements bsh.BshIterator
  16. {
  17. private Iterator iterator;
  18. /**
  19. * Construct a basic CollectionIterator
  20. *
  21. * @param The object over which we are iterating
  22. *
  23. * @throws java.lang.IllegalArgumentException If the argument is not a
  24. * supported (i.e. iterable) type.
  25. *
  26. * @throws java.lang.NullPointerException If the argument is null
  27. */
  28. public CollectionIterator(Object iterateOverMe) {
  29. iterator = createIterator(iterateOverMe);
  30. }
  31. /**
  32. * Create an iterator over the given object
  33. *
  34. * @param iterateOverMe Object of type Iterator, Collection, or types
  35. * supported by CollectionManager.BasicBshIterator
  36. *
  37. * @return an Iterator
  38. *
  39. * @throws java.lang.IllegalArgumentException If the argument is not a
  40. * supported (i.e. iterable) type.
  41. *
  42. * @throws java.lang.NullPointerException If the argument is null
  43. */
  44. protected Iterator createIterator(Object iterateOverMe)
  45. {
  46. if (iterateOverMe==null)
  47. throw new NullPointerException("Object arguments passed to " +
  48. "the CollectionIterator constructor cannot be null.");
  49. if (iterateOverMe instanceof Iterator)
  50. return (Iterator)iterateOverMe;
  51. if (iterateOverMe instanceof Collection)
  52. return ((Collection)iterateOverMe).iterator();
  53. /*
  54. Should we be able to iterate over maps?
  55. if (iterateOverMe instanceof Map)
  56. return ((Map)iterateOverMe).entrySet().iterator();
  57. */
  58. throw new IllegalArgumentException(
  59. "Cannot enumerate object of type "+iterateOverMe.getClass());
  60. }
  61. /**
  62. * Fetch the next object in the iteration
  63. *
  64. * @return The next object
  65. */
  66. public Object next() {
  67. return iterator.next();
  68. }
  69. /**
  70. * Returns true if and only if there are more objects available
  71. * via the <code>next()</code> method
  72. *
  73. * @return The next object
  74. */
  75. public boolean hasNext() {
  76. return iterator.hasNext();
  77. }
  78. }