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.commons.collections.iterators;
18  
19  import java.util.NoSuchElementException;
20  
21  /** 
22   * Provides an implementation of an empty iterator.
23   *
24   * @since 3.1
25   * @version $Id: AbstractEmptyIterator.java 1429905 2013-01-07 17:15:14Z ggregory $
26   */
27  abstract class AbstractEmptyIterator<E> {
28   
29      /**
30       * Constructor.
31       */
32      protected AbstractEmptyIterator() {
33          super();
34      }
35  
36      public boolean hasNext() {
37          return false;
38      }
39  
40      public E next() {
41          throw new NoSuchElementException("Iterator contains no elements");
42      }
43  
44      public boolean hasPrevious() {
45          return false;
46      }
47  
48      public E previous() {
49          throw new NoSuchElementException("Iterator contains no elements");
50      }
51  
52      public int nextIndex() {
53          return 0;
54      }
55  
56      public int previousIndex() {
57          return -1;
58      }
59  
60      public void add(final E obj) {
61          throw new UnsupportedOperationException("add() not supported for empty Iterator");
62      }
63  
64      public void set(final E obj) {
65          throw new IllegalStateException("Iterator contains no elements");
66      }
67  
68      public void remove() {
69          throw new IllegalStateException("Iterator contains no elements");
70      }
71  
72      public void reset() {
73          // do nothing
74      }
75  
76  }