AbstractProducer implements Producer<T> { @Override public final ListenableFuture<T> get() { ListenableFuture<T> result = instance; if (result == null) { synchronized (this) { result = instance; if (result == null) { instance = result = compute(); if (result == null) { throw new NullPointerException("compute returned null"); } } } } return result; }  @Override final ListenableFuture<T> get();  }
@Test public void get_nullPointerException() { Producer<Object> producer = new AbstractProducer<Object>() { @Override public ListenableFuture<Object> compute() { return null; } }; try { producer.get(); fail(); } catch (NullPointerException expected) { } }
@Test public void get() throws Exception { Producer<Integer> producer = new AbstractProducer<Integer>() { int i = 0; @Override public ListenableFuture<Integer> compute() { return Futures.immediateFuture(i++); } }; assertThat(producer.get().get()).isEqualTo(0); assertThat(producer.get().get()).isEqualTo(0); assertThat(producer.get().get()).isEqualTo(0); }