@Test public void get_shouldDelegateToFactory() { Object instance = new Object(); Lazy<Object> lazy = new Lazy<>(mockProvider); when(mockProvider.get()).thenReturn(instance); assertThat(lazy.get()).isSameInstanceAs(instance); verify(mockProvider, times(1)).get(); }
@Override public T get() { Object result = instance; if (result == UNINITIALIZED) { synchronized (this) { result = instance; if (result == UNINITIALIZED) { result = provider.get(); instance = result; provider = null; } } } @SuppressWarnings("unchecked") T tResult = (T) result; return tResult; }
Lazy implements Provider<T> { @Override public T get() { Object result = instance; if (result == UNINITIALIZED) { synchronized (this) { result = instance; if (result == UNINITIALIZED) { result = provider.get(); instance = result; provider = null; } } } @SuppressWarnings("unchecked") T tResult = (T) result; return tResult; } }
Lazy implements Provider<T> { @Override public T get() { Object result = instance; if (result == UNINITIALIZED) { synchronized (this) { result = instance; if (result == UNINITIALIZED) { result = provider.get(); instance = result; provider = null; } } } @SuppressWarnings("unchecked") T tResult = (T) result; return tResult; } Lazy(T instance);  Lazy(Provider<T> provider); }
Lazy implements Provider<T> { @Override public T get() { Object result = instance; if (result == UNINITIALIZED) { synchronized (this) { result = instance; if (result == UNINITIALIZED) { result = provider.get(); instance = result; provider = null; } } } @SuppressWarnings("unchecked") T tResult = (T) result; return tResult; } Lazy(T instance);  Lazy(Provider<T> provider); @Override T get(); }
Lazy implements Provider<T> { @Override public T get() { Object result = instance; if (result == UNINITIALIZED) { synchronized (this) { result = instance; if (result == UNINITIALIZED) { result = provider.get(); instance = result; provider = null; } } } @SuppressWarnings("unchecked") T tResult = (T) result; return tResult; } Lazy(T instance);  Lazy(Provider<T> provider); @Override T get();  }