View Javadoc

1   package com.github.sfleiter.cdi_interceptors.impl;
2   
3   import java.lang.reflect.Array;
4   import java.util.Iterator;
5   import java.util.NoSuchElementException;
6   
7   /**
8    * Creates an <code>Iterator</code> from the supplied object that has to be any
9    * kind of array, either <code>Object[]</code>, a custom class array or an array
10   * of a primitive type.
11   * 
12   * @author Stefan Fleiter
13   */
14  public final class ArrayIterator implements Iterator<Object> {
15  
16      private final Object array;
17      private final int length;
18      private int pos;
19  
20      /**
21       * Constructor.
22       * 
23       * @param array
24       *            any kind of array
25       * @throws IllegalArgumentException
26       *             if param is no array
27       */
28      public ArrayIterator(final Object array) throws IllegalArgumentException {
29          this.array = array;
30          length = Array.getLength(array);
31      }
32  
33      /**
34       * {@inheritDoc}
35       */
36      @Override
37      public boolean hasNext() {
38          return pos < length;
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public Object next() {
46          if (pos == length) {
47              throw new NoSuchElementException();
48          }
49          return Array.get(array, pos++);
50      }
51  
52      /**
53       * Not supported for this iterator.
54       * 
55       * @throws UnsupportedOperationException
56       */
57      @Override
58      public void remove() {
59          throw new UnsupportedOperationException();
60      }
61  
62      /**
63       * Returns the full length of the underlying array.
64       * 
65       * @return length of the array
66       */
67      public int getLength() {
68          return length;
69      }
70  
71  }