1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class ArrayBooleanListIterator implements BooleanListIterator {
39
40
41
42 protected final boolean[] array;
43
44 protected int cursor = 0;
45
46 protected int last = -1;
47
48
49
50
51
52
53
54
55
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
66
67
68
69
70
71
72
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 }