24
24
25
25
package ewc .utilities .testableio .core ;
26
26
27
- import java .util .Arrays ;
28
- import java .util .Iterator ;
29
27
import java .util .NoSuchElementException ;
30
- import java .util .stream . Stream ;
28
+ import java .util .concurrent . atomic . AtomicInteger ;
31
29
32
30
/**
33
31
* This class provides configured responses for a single I/O opersation stub. The responses could
39
37
*/
40
38
public class SingleQueryResponses {
41
39
/**
42
- * The iterator of configured responses. This iterator is used to provide
43
- * responses sequentially.
40
+ * The counter providing the index of the next response to be returned.
44
41
*/
45
- private final Iterator <? extends GenericResponse <?>> values ;
42
+ private final Counter index ;
43
+
44
+ /**
45
+ * The array of responses to be returned sequentially.
46
+ */
47
+ private final GenericResponse <?>[] values ;
46
48
47
49
/**
48
50
* The short description of this instance. Used primarily for identifying the
@@ -57,7 +59,7 @@ public class SingleQueryResponses {
57
59
* @param value The response to be returned forever.
58
60
*/
59
61
public SingleQueryResponses (final String stub , final GenericResponse <?> value ) {
60
- this (Stream . generate (() -> value ). iterator (), stub );
62
+ this (stub , new GenericResponse <?>[]{ value }, new ConstantIndex () );
61
63
}
62
64
63
65
/**
@@ -68,20 +70,22 @@ public SingleQueryResponses(final String stub, final GenericResponse<?> value) {
68
70
* @param values The array of responses to be returned sequentially.
69
71
*/
70
72
public SingleQueryResponses (final String stub , final GenericResponse <?>... values ) {
71
- this (Arrays . stream ( values ). iterator (), stub );
73
+ this (stub , values , new IncrementalIndex () );
72
74
}
73
75
74
76
/**
75
77
* Primary constructor.
76
78
*
77
- * @param values The iterator that returns configured responses sequentially.
78
- * @param description The short description of this instance.
79
+ * @param stub The short description of this stub instance.
80
+ * @param values The array of responses to be returned sequentially.
81
+ * @param index The counter providing the index of the next response to be returned.
79
82
*/
80
83
private SingleQueryResponses (
81
- final Iterator <? extends GenericResponse <?>> values , final String description
84
+ final String stub , final GenericResponse <?>[] values , final Counter index
82
85
) {
86
+ this .index = index ;
83
87
this .values = values ;
84
- this .description = description ;
88
+ this .description = stub ;
85
89
}
86
90
87
91
/**
@@ -95,15 +99,79 @@ public GenericResponse<?> next() {
95
99
if (this .values == null ) {
96
100
throw new IllegalStateException ("No response to send" );
97
101
}
98
- if (! this .values . hasNext ( )) {
102
+ if (this .index . isTheLast ( this . values . length )) {
99
103
throw new NoSuchElementException (
100
104
String .format ("No more configured responses for %s" , this .description )
101
105
);
102
106
}
103
- final GenericResponse <?> value = this .values . next () ;
107
+ final GenericResponse <?> value = this .values [ this . index . getAndIncrement ()] ;
104
108
if (value .contents () instanceof RuntimeException runtimeException ) {
105
109
throw runtimeException ;
106
110
}
107
111
return value ;
108
112
}
113
+
114
+ /**
115
+ * The value of the counter. This value is used to determine the index of the response to
116
+ * be returned.
117
+ *
118
+ * @since 0.1
119
+ */
120
+ private interface Counter {
121
+ /**
122
+ * The value of the counter. This value is used to determine the index of the response to
123
+ * be returned.
124
+ *
125
+ * @return The current value of the counter.
126
+ */
127
+ int currentValue ();
128
+
129
+ /**
130
+ * Increments the counter by one. This method is used to move to the next response in the
131
+ * array.
132
+ *
133
+ * @return The value of the counter before incrementing.
134
+ */
135
+ int getAndIncrement ();
136
+
137
+ /**
138
+ * Checks if the current value of the counter is the last one in the array.
139
+ *
140
+ * @param size The size of the array of responses.
141
+ * @return True if the current value is the last one, false otherwise.
142
+ * @since 0.1
143
+ */
144
+ default boolean isTheLast (final int size ) {
145
+ return this .currentValue () >= size ;
146
+ }
147
+ }
148
+
149
+ private static final class ConstantIndex implements Counter {
150
+ @ Override
151
+ public int currentValue () {
152
+ return 0 ;
153
+ }
154
+
155
+ @ Override
156
+ public int getAndIncrement () {
157
+ return 0 ;
158
+ }
159
+ }
160
+
161
+ private static final class IncrementalIndex implements Counter {
162
+ /**
163
+ * The thread-safe counter providing the index of the next response to be returned.
164
+ */
165
+ private final AtomicInteger index = new AtomicInteger (0 );
166
+
167
+ @ Override
168
+ public int currentValue () {
169
+ return this .index .intValue ();
170
+ }
171
+
172
+ @ Override
173
+ public int getAndIncrement () {
174
+ return this .index .getAndIncrement ();
175
+ }
176
+ }
109
177
}
0 commit comments