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