@Test public void shouldReturnSameObject() { Generator<Integer> memoizingGenerator = new MemoizingGenerator<>(Generators.positiveInts); Integer first = memoizingGenerator.next(); assertThat(first, notNullValue()); for (int i = 0; i < 20; i++) { assertThat(memoizingGenerator.next(), sameInstance(first)); } }
@Override public T next() { lock.lock(); try { if (!initialized) { value = delegate.next(); initialized = true; } } finally { lock.unlock(); } return value; }
MemoizingGenerator implements Generator<T> { @Override public T next() { lock.lock(); try { if (!initialized) { value = delegate.next(); initialized = true; } } finally { lock.unlock(); } return value; } }
MemoizingGenerator implements Generator<T> { @Override public T next() { lock.lock(); try { if (!initialized) { value = delegate.next(); initialized = true; } } finally { lock.unlock(); } return value; } MemoizingGenerator(@Nonnull Generator<T> delegate); }
MemoizingGenerator implements Generator<T> { @Override public T next() { lock.lock(); try { if (!initialized) { value = delegate.next(); initialized = true; } } finally { lock.unlock(); } return value; } MemoizingGenerator(@Nonnull Generator<T> delegate); @Override T next(); }
MemoizingGenerator implements Generator<T> { @Override public T next() { lock.lock(); try { if (!initialized) { value = delegate.next(); initialized = true; } } finally { lock.unlock(); } return value; } MemoizingGenerator(@Nonnull Generator<T> delegate); @Override T next();  }