1 /*
2 * Copyright 2001-2010 Stephen Colebourne
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.joda.primitives.listiterator.impl;
17
18 import java.util.NoSuchElementException;
19
20 import org.joda.primitives.BooleanUtils;
21 import org.joda.primitives.listiterator.BooleanListIterator;
22
23 /**
24 * An iterator over an array of <code>boolean</code> values.
25 * <p>
26 * This class implements {@link java.util.ListIterator ListIterator} allowing
27 * seamless integration with other APIs.
28 * <p>
29 * The iterator can be reset to the start if required.
30 * <code>add()</code> and <code>remove()</code> are unsupported, but
31 * <code>set()</code> is supported.
32 *
33 * @author Stephen Colebourne
34 * @author Jason Tiscione
35 * @version CODE GENERATED
36 * @since 1.0
37 */
38 public class ArrayBooleanListIterator implements BooleanListIterator {
39 // This file is CODE GENERATED. Do not change manually.
40
41 /** The array to iterate over */
42 protected final boolean[] array;
43 /** Cursor position */
44 protected int cursor = 0;
45 /** Last returned position */
46 protected int last = -1;
47
48 /**
49 * Creates an iterator over a copy of an array of <code>boolean</code> values.
50 * <p>
51 * The specified array is copied, ensuring the original data is unaltered.
52 * Note that the class is not immutable due to the {@code set} methods.
53 *
54 * @param array the array to iterate over, must not be null
55 * @throws IllegalArgumentException if the array is null
56 */
57 public static ArrayBooleanListIterator copyOf(boolean[] array) {
58 if (array == null) {
59 throw new IllegalArgumentException("Array must not be null");
60 }
61 return new ArrayBooleanListIterator(array.clone());
62 }
63
64 /**
65 * Constructs an iterator over an array of <code>boolean</code> values.
66 * <p>
67 * The array is assigned internally, thus the caller holds a reference to
68 * the internal state of the returned iterator. It is not recommended to
69 * modify the state of the array after construction.
70 *
71 * @param array the array to iterate over, must not be null
72 * @throws IllegalArgumentException if the array is null
73 */
74 public ArrayBooleanListIterator(boolean[] array) {
75 super();
76 if (array == null) {
77 throw new IllegalArgumentException("Array must not be null");
78 }
79 this.array = array;
80 }
81
82 //-----------------------------------------------------------------------
83 public boolean isModifiable() {
84 return true;
85 }
86
87 public boolean isResettable() {
88 return true;
89 }
90
91 //-----------------------------------------------------------------------
92 public boolean hasNext() {
93 return (cursor < array.length);
94 }
95
96 public int nextIndex() {
97 return cursor;
98 }
99
100 public boolean nextBoolean() {
101 if (hasNext() == false) {
102 throw new NoSuchElementException("No more elements available");
103 }
104 last = cursor;
105 return array[cursor++];
106 }
107
108 public Boolean next() {
109 return BooleanUtils.toObject(nextBoolean());
110 }
111
112 public boolean hasPrevious() {
113 return (cursor > 0);
114 }
115
116 public int previousIndex() {
117 return cursor - 1;
118 }
119
120 public boolean previousBoolean() {
121 if (hasPrevious() == false) {
122 throw new NoSuchElementException("No more elements available");
123 }
124 last = --cursor;
125 return array[cursor];
126 }
127
128 public Boolean previous() {
129 return BooleanUtils.toObject(previousBoolean());
130 }
131
132 public void add(boolean value) {
133 throw new UnsupportedOperationException("ArrayBooleanListIterator does not support add");
134 }
135
136 public void add(Boolean value) {
137 throw new UnsupportedOperationException("ArrayBooleanListIterator does not support add");
138 }
139
140 public void remove() {
141 throw new UnsupportedOperationException("ArrayBooleanListIterator does not support remove");
142 }
143
144 public void set(boolean value) {
145 if (last < 0) {
146 throw new IllegalStateException("ArrayBooleanListIterator cannot be set until next is called");
147 }
148 array[last] = value;
149 }
150
151 public void set(Boolean value) {
152 set(BooleanUtils.toPrimitive(value));
153 }
154
155 public void reset() {
156 cursor = 0;
157 last = -1;
158 }
159
160 }