View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.logging.log4j.core.util;
18  
19  import java.util.Iterator;
20  import java.util.NoSuchElementException;
21  
22  /**
23   * An {@link java.util.Iterator Iterator} over an array of objects.
24   * <p>
25   * This iterator does not support {@link #remove}, as the object array cannot be
26   * structurally modified.
27   * <p>
28   * The iterator implements a {@link #reset} method, allowing the reset of the iterator
29   * back to the start if required.
30   *
31   * @param <E> the type to iterate over
32   * @since 3.0
33   * @version $Id: ObjectArrayIterator.java 1734648 2016-03-11 23:51:22Z ggregory $
34   */
35  public class ObjectArrayIterator<E> implements /*Resettable*/ Iterator<E> {
36  
37      /** The array */
38      final E[] array;
39      /** The start index to loop from */
40      final int startIndex;
41      /** The end index to loop to */
42      final int endIndex;
43      /** The current iterator index */
44      int index = 0;
45  
46      //-------------------------------------------------------------------------
47      /**
48       * Constructs an ObjectArrayIterator that will iterate over the values in the
49       * specified array.
50       *
51       * @param array the array to iterate over
52       * @throws NullPointerException if <code>array</code> is <code>null</code>
53       */
54      @SafeVarargs
55      public ObjectArrayIterator(final E... array) {
56          this(array, 0, array.length);
57      }
58  
59      /**
60       * Constructs an ObjectArrayIterator that will iterate over the values in the
61       * specified array from a specific start index.
62       *
63       * @param array  the array to iterate over
64       * @param start  the index to start iterating at
65       * @throws NullPointerException if <code>array</code> is <code>null</code>
66       * @throws IndexOutOfBoundsException if the start index is out of bounds
67       */
68      public ObjectArrayIterator(final E array[], final int start) {
69          this(array, start, array.length);
70      }
71  
72      /**
73       * Construct an ObjectArrayIterator that will iterate over a range of values
74       * in the specified array.
75       *
76       * @param array  the array to iterate over
77       * @param start  the index to start iterating at
78       * @param end  the index (exclusive) to finish iterating at
79       * @throws IndexOutOfBoundsException if the start or end index is out of bounds
80       * @throws IllegalArgumentException if end index is before the start
81       * @throws NullPointerException if <code>array</code> is <code>null</code>
82       */
83      public ObjectArrayIterator(final E array[], final int start, final int end) {
84          super();
85          if (start < 0) {
86              throw new ArrayIndexOutOfBoundsException("Start index must not be less than zero");
87          }
88          if (end > array.length) {
89              throw new ArrayIndexOutOfBoundsException("End index must not be greater than the array length");
90          }
91          if (start > array.length) {
92              throw new ArrayIndexOutOfBoundsException("Start index must not be greater than the array length");
93          }
94          if (end < start) {
95              throw new IllegalArgumentException("End index must not be less than start index");
96          }
97          this.array = array;
98          this.startIndex = start;
99          this.endIndex = end;
100         this.index = start;
101     }
102 
103     // Iterator interface
104     //-------------------------------------------------------------------------
105 
106     /**
107      * Returns true if there are more elements to return from the array.
108      *
109      * @return true if there is a next element to return
110      */
111     @Override
112     public boolean hasNext() {
113         return this.index < this.endIndex;
114     }
115 
116     /**
117      * Returns the next element in the array.
118      *
119      * @return the next element in the array
120      * @throws NoSuchElementException if all the elements in the array
121      *    have already been returned
122      */
123     @Override
124     public E next() {
125         if (hasNext() == false) {
126             throw new NoSuchElementException();
127         }
128         return this.array[this.index++];
129     }
130 
131     /**
132      * Throws {@link UnsupportedOperationException}.
133      *
134      * @throws UnsupportedOperationException always
135      */
136     @Override
137     public void remove() {
138         throw new UnsupportedOperationException("remove() method is not supported for an ObjectArrayIterator");
139     }
140 
141     // Properties
142     //-------------------------------------------------------------------------
143 
144     /**
145      * Gets the array that this iterator is iterating over.
146      *
147      * @return the array this iterator iterates over
148      */
149     public E[] getArray() {
150         return this.array;
151     }
152 
153     /**
154      * Gets the start index to loop from.
155      *
156      * @return the start index
157      */
158     public int getStartIndex() {
159         return this.startIndex;
160     }
161 
162     /**
163      * Gets the end index to loop to.
164      *
165      * @return the end index
166      */
167     public int getEndIndex() {
168         return this.endIndex;
169     }
170 
171     /**
172      * Resets the iterator back to the start index.
173      */
174     //@Override
175     public void reset() {
176         this.index = this.startIndex;
177     }
178 
179 }