PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/collection/CollectionIterator.java

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