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.CharUtils;
21 import org.joda.primitives.listiterator.CharListIterator;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class ArrayCharListIterator implements CharListIterator {
39
40
41
42 protected final char[] 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 ArrayCharListIterator copyOf(char[] array) {
58 if (array == null) {
59 throw new IllegalArgumentException("Array must not be null");
60 }
61 return new ArrayCharListIterator(array.clone());
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public ArrayCharListIterator(char[] 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 char nextChar() {
101 if (hasNext() == false) {
102 throw new NoSuchElementException("No more elements available");
103 }
104 last = cursor;
105 return array[cursor++];
106 }
107
108 public Character next() {
109 return CharUtils.toObject(nextChar());
110 }
111
112 public boolean hasPrevious() {
113 return (cursor > 0);
114 }
115
116 public int previousIndex() {
117 return cursor - 1;
118 }
119
120 public char previousChar() {
121 if (hasPrevious() == false) {
122 throw new NoSuchElementException("No more elements available");
123 }
124 last = --cursor;
125 return array[cursor];
126 }
127
128 public Character previous() {
129 return CharUtils.toObject(previousChar());
130 }
131
132 public void add(char value) {
133 throw new UnsupportedOperationException("ArrayCharListIterator does not support add");
134 }
135
136 public void add(Character value) {
137 throw new UnsupportedOperationException("ArrayCharListIterator does not support add");
138 }
139
140 public void remove() {
141 throw new UnsupportedOperationException("ArrayCharListIterator does not support remove");
142 }
143
144 public void set(char value) {
145 if (last < 0) {
146 throw new IllegalStateException("ArrayCharListIterator cannot be set until next is called");
147 }
148 array[last] = value;
149 }
150
151 public void set(Character value) {
152 set(CharUtils.toPrimitive(value));
153 }
154
155 public void reset() {
156 cursor = 0;
157 last = -1;
158 }
159
160 }