|
37 | 37 | import static org.junit.Assert.assertSame;
|
38 | 38 | import static org.junit.Assert.assertTrue;
|
39 | 39 |
|
| 40 | +import java.math.BigInteger; |
40 | 41 | import java.util.ArrayList;
|
41 | 42 | import java.util.Arrays;
|
42 | 43 | import java.util.Collection;
|
@@ -243,4 +244,98 @@ public void testToString() {
|
243 | 244 | assertArrayEquals(expected, actual);
|
244 | 245 | }
|
245 | 246 |
|
| 247 | + @Test |
| 248 | + public void testAddLater() { |
| 249 | + final ObjectIndex<Object> objectIndex = |
| 250 | + new ObjectIndex<Object>(Object.class); |
| 251 | + objectIndex.add(new Integer(5)); |
| 252 | + objectIndex.add(new Float(2.5f)); |
| 253 | + objectIndex.add(new Integer(3)); |
| 254 | + |
| 255 | + final LazyThings<Integer> lazyIntegers = new LazyThings<Integer>(9, -7); |
| 256 | + objectIndex.addLater(lazyIntegers); |
| 257 | + |
| 258 | + final LazyThings<Float> lazyFloats = |
| 259 | + new LazyThings<Float>(6.6f, -3.3f, -5.1f, 12.3f); |
| 260 | + objectIndex.addLater(lazyFloats); |
| 261 | + |
| 262 | + final LazyThings<BigInteger> lazyBigIntegers = |
| 263 | + new LazyThings<BigInteger>(BigInteger.ONE, BigInteger.TEN); |
| 264 | + objectIndex.addLater(lazyBigIntegers); |
| 265 | + |
| 266 | + // verify that no pending objects have been resolved yet |
| 267 | + assertFalse(lazyIntegers.wasAccessed()); |
| 268 | + assertFalse(lazyFloats.wasAccessed()); |
| 269 | + assertFalse(lazyBigIntegers.wasAccessed()); |
| 270 | + |
| 271 | + // verify list of Integers; this will resolve the pending ones |
| 272 | + final List<Object> integerObjects = objectIndex.get(Integer.class); |
| 273 | + assertEquals(4, integerObjects.size()); |
| 274 | + assertEquals(5, integerObjects.get(0)); |
| 275 | + assertEquals(3, integerObjects.get(1)); |
| 276 | + assertEquals(9, integerObjects.get(2)); |
| 277 | + assertEquals(-7, integerObjects.get(3)); |
| 278 | + |
| 279 | + // verify that pending Integers have now been resolved |
| 280 | + assertTrue(lazyIntegers.wasAccessed()); |
| 281 | + |
| 282 | + // verify that the other pending objects have still not been resolved |
| 283 | + assertFalse(lazyFloats.wasAccessed()); |
| 284 | + assertFalse(lazyBigIntegers.wasAccessed()); |
| 285 | + |
| 286 | + // verify list of Floats; this will resolve the pending ones |
| 287 | + final List<Object> floatObjects = objectIndex.get(Float.class); |
| 288 | + assertEquals(5, floatObjects.size()); |
| 289 | + assertEquals(2.5f, floatObjects.get(0)); |
| 290 | + assertEquals(6.6f, floatObjects.get(1)); |
| 291 | + assertEquals(-3.3f, floatObjects.get(2)); |
| 292 | + assertEquals(-5.1f, floatObjects.get(3)); |
| 293 | + assertEquals(12.3f, floatObjects.get(4)); |
| 294 | + |
| 295 | + // verify that pending Floats have now been resolved |
| 296 | + assertTrue(lazyFloats.wasAccessed()); |
| 297 | + |
| 298 | + // verify that pending BigIntegers have still not been resolved |
| 299 | + assertFalse(lazyBigIntegers.wasAccessed()); |
| 300 | + |
| 301 | + // verify list of BigIntegers; this will resolve the pending ones |
| 302 | + final List<Object> bigIntegerObjects = objectIndex.get(BigInteger.class); |
| 303 | + assertEquals(2, bigIntegerObjects.size()); |
| 304 | + assertEquals(BigInteger.ONE, bigIntegerObjects.get(0)); |
| 305 | + assertEquals(BigInteger.TEN, bigIntegerObjects.get(1)); |
| 306 | + |
| 307 | + // verify that pending BigIntegers have finally been resolved |
| 308 | + assertTrue(lazyBigIntegers.wasAccessed()); |
| 309 | + } |
| 310 | + |
| 311 | + // -- Helper classes -- |
| 312 | + |
| 313 | + public static class LazyThings<T> implements LazyObjects<T> { |
| 314 | + |
| 315 | + private Collection<T> objects; |
| 316 | + private Class<?> type; |
| 317 | + private boolean accessed; |
| 318 | + |
| 319 | + public LazyThings(T... objects) { |
| 320 | + this.objects = Arrays.asList(objects); |
| 321 | + this.type = objects[0].getClass(); |
| 322 | + } |
| 323 | + |
| 324 | + @Override |
| 325 | + public Collection<T> get() { |
| 326 | + accessed = true; |
| 327 | + return objects; |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public Class<?> getType() { |
| 332 | + return type; |
| 333 | + } |
| 334 | + |
| 335 | + public boolean wasAccessed() { |
| 336 | + return accessed; |
| 337 | + } |
| 338 | + |
| 339 | + } |
| 340 | + |
246 | 341 | }
|
0 commit comments