@Test public void testMemoize2() { final AtomicInteger count = new AtomicInteger(0); Func2<Integer, Integer, MyObject> memoized = RxMemoization .memoize(new Func2<Integer, Integer, MyObject>() { @Override public MyObject call(Integer integer, Integer integer2) { count.incrementAndGet(); return INSTANCES.get(integer + integer2); } }); Assert.assertEquals(INSTANCES.get(0), memoized.call(0, 0)); Assert.assertEquals(INSTANCES.get(0), memoized.call(0, 0)); Assert.assertEquals(INSTANCES.get(0), memoized.call(0, 0)); Assert.assertEquals(INSTANCES.get(1), memoized.call(0, 1)); Assert.assertEquals(INSTANCES.get(1), memoized.call(0, 1)); Assert.assertEquals(INSTANCES.get(1), memoized.call(0, 1)); Assert.assertEquals(INSTANCES.get(30), memoized.call(15, 15)); Assert.assertEquals(INSTANCES.get(30), memoized.call(15, 15)); Assert.assertEquals(INSTANCES.get(30), memoized.call(15, 15)); Assert.assertEquals(INSTANCES.get(0), memoized.call(0, 0)); Assert.assertEquals(INSTANCES.get(0), memoized.call(0, 0)); Assert.assertEquals(INSTANCES.get(0), memoized.call(0, 0)); Assert.assertEquals(3, count.get()); }
public static <R> Func0<R> memoize(final Func0<R> func0) { return new Func0<R>() { private R value; @Override public R call() { if (null == value) { synchronized (this) { if (null == value) { value = func0.call(); } } } return value; } }; }
RxMemoization { public static <R> Func0<R> memoize(final Func0<R> func0) { return new Func0<R>() { private R value; @Override public R call() { if (null == value) { synchronized (this) { if (null == value) { value = func0.call(); } } } return value; } }; } }
RxMemoization { public static <R> Func0<R> memoize(final Func0<R> func0) { return new Func0<R>() { private R value; @Override public R call() { if (null == value) { synchronized (this) { if (null == value) { value = func0.call(); } } } return value; } }; } private  RxMemoization(); }
RxMemoization { public static <R> Func0<R> memoize(final Func0<R> func0) { return new Func0<R>() { private R value; @Override public R call() { if (null == value) { synchronized (this) { if (null == value) { value = func0.call(); } } } return value; } }; } private  RxMemoization(); static Func0<R> memoize(final Func0<R> func0); static Func1<A, R> memoize(final Func1<A, R> func1); static Func2<A, B, R> memoize(final Func2<A, B, R> func2); static Func3<A, B, C, R> memoize(final Func3<A, B, C, R> func3); static Func4<A, B, C, D, R> memoize(final Func4<A, B, C, D, R> func4); static Func5<A, B, C, D, E, R> memoize(
            final Func5<A, B, C, D, E, R> func5); static Func6<A, B, C, D, E, F, R> memoize(
            final Func6<A, B, C, D, E, F, R> func6); static Func7<A, B, C, D, E, F, G, R> memoize(
            final Func7<A, B, C, D, E, F, G, R> func7); static Func8<A, B, C, D, E, F, G, H, R> memoize(
            final Func8<A, B, C, D, E, F, G, H, R> func8); static Func9<A, B, C, D, E, F, G, H, I, R> memoize(
            final Func9<A, B, C, D, E, F, G, H, I, R> func9); static FuncN<R> memoize(final FuncN<R> funcN); }
RxMemoization { public static <R> Func0<R> memoize(final Func0<R> func0) { return new Func0<R>() { private R value; @Override public R call() { if (null == value) { synchronized (this) { if (null == value) { value = func0.call(); } } } return value; } }; } private  RxMemoization(); static Func0<R> memoize(final Func0<R> func0); static Func1<A, R> memoize(final Func1<A, R> func1); static Func2<A, B, R> memoize(final Func2<A, B, R> func2); static Func3<A, B, C, R> memoize(final Func3<A, B, C, R> func3); static Func4<A, B, C, D, R> memoize(final Func4<A, B, C, D, R> func4); static Func5<A, B, C, D, E, R> memoize(
            final Func5<A, B, C, D, E, R> func5); static Func6<A, B, C, D, E, F, R> memoize(
            final Func6<A, B, C, D, E, F, R> func6); static Func7<A, B, C, D, E, F, G, R> memoize(
            final Func7<A, B, C, D, E, F, G, R> func7); static Func8<A, B, C, D, E, F, G, H, R> memoize(
            final Func8<A, B, C, D, E, F, G, H, R> func8); static Func9<A, B, C, D, E, F, G, H, I, R> memoize(
            final Func9<A, B, C, D, E, F, G, H, I, R> func9); static FuncN<R> memoize(final FuncN<R> funcN);  }