View Javadoc

    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
    17  package org.apache.commons.jexl.util;
    18
    19
    20
    21  import java.util.Iterator;
    22  import java.util.NoSuchElementException;
    23  import java.lang.reflect.Array;
    24
    25
    26  /***
    27   *  <p>
    28   *  An Iterator wrapper for an Object[]. This will
    29   *  allow us to deal with all array like structures
    30   *  in a consistent manner.
    31   *  </p>
    32   *  <p>
    33   *  WARNING : this class's operations are NOT synchronized.
    34   *  It is meant to be used in a single thread, newly created
    35   *  for each use in the #foreach() directive.
    36   *  If this is used or shared, synchronize in the
    37   *  next() method.
    38   *  </p>
    39   *
    40   * @since 1.0
    41   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    42   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    43   * @version $Id: ArrayIterator.java 398329 2006-04-30 12:51:43Z dion $
    44   */
    45  public class ArrayIterator implements Iterator {
    46      /***
    47       * The objects to iterate over.
    48       */
    49      private final Object array;
    50
    51      /***
    52       * The current position and size in the array.
    53       */
    54      private int pos;
    55
    56      /***
    57       * The size of the array.
    58       */
    59      private final int size;
    60
    61      /***
    62       * Creates a new iterator instance for the specified array.
    63       *
    64       * @param arr The array for which an iterator is desired.
    65       */
    66      public ArrayIterator(Object arr) {
    67          /*
    68           * if this isn't an array, then throw.  Note that this is
    69           * for internal use - so this should never happen - if it does
    70           *  we screwed up.
    71           */
    72
    73          if (!arr.getClass().isArray()) {
    74              throw new IllegalArgumentException("Programmer error :"
    75                        + " internal ArrayIterator invoked w/o array");
    76          }
    77
    78          array = arr;
    79          pos = 0;
    80          size = Array.getLength(array);
    81      }
    82
    83      /***
    84       * Move to next element in the array.
    85       *
    86       * @return The next object in the array.
    87       */
    88      public Object next() {
    89          if (pos < size) {
    90              return Array.get(array, pos++);
    91          }
    92
    93          /*
    94           *  we screwed up...
    95           */
    96
    97          throw new NoSuchElementException("No more elements: " + pos
    98                                           + " / " + size);
    99      }
    100
    101     /***
    102      * Check to see if there is another element in the array.
    103      *
    104      * @return Whether there is another element.
    105      */
    106     public boolean hasNext() {
    107         return (pos < size);
    108     }
    109
    110     /***
    111      * No op--merely added to satify the <code>Iterator</code> interface.
    112      */
    113     public void remove() {
    114         throw new UnsupportedOperationException();
    115     }
    116 }