Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73ad318822c81c93d250d1f7aa1fc3d551fa605c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2009, 2011, 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.collection;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * An object that iterates over the elements of an array
 *
 * @author Eike Stepper
 * @since 3.0
 */
public class ArrayIterator<T> implements Iterator<T>
{
  private T[] elements;

  private int index;

  private int lastElement;

  public ArrayIterator(T[] elements)
  {
    this(elements, 0, elements.length - 1);
  }

  public ArrayIterator(T[] elements, int firstElement)
  {
    this(elements, firstElement, elements.length - 1);
  }

  public ArrayIterator(T[] elements, int firstElement, int lastElement)
  {
    this.elements = elements;
    index = firstElement;
    this.lastElement = lastElement;
  }

  public boolean hasNext()
  {
    return elements != null && index <= lastElement;
  }

  public T next() throws NoSuchElementException
  {
    if (!hasNext())
    {
      throw new NoSuchElementException();
    }

    return elements[index++];
  }

  /**
   * Unsupported.
   *
   * @throws UnsupportedOperationException
   *           always
   */
  public void remove()
  {
    throw new UnsupportedOperationException();
  }
}

Back to the top