1. /*
  2. * Copyright 1999-2001,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.jexl.util;
  17. import java.util.Iterator;
  18. import java.util.NoSuchElementException;
  19. import java.lang.reflect.Array;
  20. /**
  21. * <p>
  22. * An Iterator wrapper for an Object[]. This will
  23. * allow us to deal with all array like structures
  24. * in a consistent manner.
  25. * </p>
  26. * <p>
  27. * WARNING : this class's operations are NOT synchronized.
  28. * It is meant to be used in a single thread, newly created
  29. * for each use in the #foreach() directive.
  30. * If this is used or shared, synchronize in the
  31. * next() method.
  32. * </p>
  33. *
  34. * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
  35. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  36. * @version $Id: ArrayIterator.java,v 1.3 2004/02/28 13:45:21 yoavs Exp $
  37. */
  38. public class ArrayIterator implements Iterator
  39. {
  40. /**
  41. * The objects to iterate.
  42. */
  43. private Object array;
  44. /**
  45. * The current position and size in the array.
  46. */
  47. private int pos;
  48. private int size;
  49. /**
  50. * Creates a new iterator instance for the specified array.
  51. *
  52. * @param array The array for which an iterator is desired.
  53. */
  54. public ArrayIterator(Object array)
  55. {
  56. /*
  57. * if this isn't an array, then throw. Note that this is
  58. * for internal use - so this should never happen - if it does
  59. * we screwed up.
  60. */
  61. if ( !array.getClass().isArray() )
  62. {
  63. throw new IllegalArgumentException(
  64. "Programmer error : internal ArrayIterator invoked w/o array");
  65. }
  66. this.array = array;
  67. pos = 0;
  68. size = Array.getLength( this.array );
  69. }
  70. /**
  71. * Move to next element in the array.
  72. *
  73. * @return The next object in the array.
  74. */
  75. public Object next()
  76. {
  77. if (pos < size )
  78. return Array.get( array, pos++);
  79. /*
  80. * we screwed up...
  81. */
  82. throw new NoSuchElementException("No more elements: " + pos +
  83. " / " + size);
  84. }
  85. /**
  86. * Check to see if there is another element in the array.
  87. *
  88. * @return Whether there is another element.
  89. */
  90. public boolean hasNext()
  91. {
  92. return (pos < size );
  93. }
  94. /**
  95. * No op--merely added to satify the <code>Iterator</code> interface.
  96. */
  97. public void remove()
  98. {
  99. throw new UnsupportedOperationException();
  100. }
  101. }