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.collections4.iterators;
18
19 import java.util.NoSuchElementException;
20
21 import org.apache.commons.collections4.ResettableIterator;
22
23 /**
24 * Provides an abstract implementation of an empty iterator.
25 *
26 * @since 3.1
27 */
28 abstract class AbstractEmptyIterator<E> implements ResettableIterator<E> {
29
30 /**
31 * Constructs a new instance.
32 */
33 protected AbstractEmptyIterator() {
34 }
35
36 /**
37 * Always throws UnsupportedOperationException.
38 *
39 * @param ignored ignore.
40 * @throws UnsupportedOperationException Always thrown.
41 * @deprecated Will be removed in 5.0 without replacement.
42 */
43 @Deprecated
44 public void add(final E ignored) {
45 throw new UnsupportedOperationException("add() not supported for empty Iterator");
46 }
47
48 /**
49 * Always returns false, this iterator contains no elements.
50 *
51 * @return Always false.
52 */
53 @Override
54 public boolean hasNext() {
55 return false;
56 }
57
58 /**
59 * Always returns false, this iterator contains no elements.
60 *
61 * @return Always false.
62 */
63 public boolean hasPrevious() {
64 return false;
65 }
66
67 /**
68 * Always throws IllegalStateException, this iterator contains no elements.
69 *
70 * @return Always throws IllegalStateException.
71 * @throws IllegalStateException Always thrown.
72 */
73 @Override
74 public E next() {
75 throw new NoSuchElementException("Iterator contains no elements");
76 }
77
78 /**
79 * Always returns 0, this iterator contains no elements.
80 *
81 * @return Always returns 0.
82 */
83 public int nextIndex() {
84 return 0;
85 }
86
87 /**
88 * Always throws IllegalStateException, this iterator contains no elements.
89 *
90 * @return Always throws IllegalStateException.
91 * @throws IllegalStateException Always thrown.
92 */
93 public E previous() {
94 throw new NoSuchElementException("Iterator contains no elements");
95 }
96
97 /**
98 * Always returns -1, this iterator contains no elements.
99 *
100 * @return Always returns -1.
101 */
102 public int previousIndex() {
103 return -1;
104 }
105
106 /**
107 * Always throws IllegalStateException, this iterator contains no elements.
108 *
109 * @throws IllegalStateException Always thrown.
110 */
111 @Override
112 public void remove() {
113 throw new IllegalStateException("Iterator contains no elements");
114 }
115
116 @Override
117 public void reset() {
118 // do nothing
119 }
120
121 /**
122 * Always throws IllegalStateException, this iterator contains no elements.
123 *
124 * @param ignored ignored.
125 * @throws IllegalStateException Always thrown.
126 */
127 public void set(final E ignored) {
128 throw new IllegalStateException("Iterator contains no elements");
129 }
130
131 }