View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream.util;
17  
18  import java.util.Iterator;
19  import java.util.NoSuchElementException;
20  
21  public final class ArrayIterator implements Iterator {
22  
23    private final Object[] array;
24    private final int maxIndex;
25    private int index;
26  
27    public ArrayIterator(Object[] a) {
28      this(a, 0, a.length);
29    }
30  
31    public ArrayIterator(Object[] a, int off, int len) {
32      if (off < 0) throw new IllegalArgumentException();
33      if (off > a.length) throw new IllegalArgumentException();
34      if (len > a.length - off) throw new IllegalArgumentException();
35      array = a;
36      index = off;
37      maxIndex = len + off;
38    }
39  
40    public boolean hasNext() { return index < maxIndex; }
41  
42    public Object next() {
43      if (index >= maxIndex) throw new NoSuchElementException();
44      return array[index++];
45    }
46  
47    public void remove() { throw new UnsupportedOperationException(); }
48  
49  }
50