@Test public void testMemoize() { final AtomicInteger count = new AtomicInteger(0); final Func0<MyObject> memoized = RxMemoization.memoize(new Func0<MyObject>() { @Override public MyObject call() { count.incrementAndGet(); return INSTANCE; } }); final int threadCount = 10000; final int maxDelay = 100; Iterable<Observable<Long>> observableList = Observable.range(0, threadCount) .map(new Func1<Integer, Observable<Long>>() { @Override public Observable<Long> call(final Integer integer) { return Observable .timer((long)(Math.random() * maxDelay), TimeUnit.MILLISECONDS) .doOnNext(new Action1<Long>() { @Override public void call(Long aLong) { memoized.call(); memoized.call(); memoized.call(); memoized.call(); memoized.call(); } }).doOnCompleted(new Action0() { @Override public void call() { System.out.println("Test-" + integer); } }).subscribeOn(Schedulers.newThread()); } }).toList().toBlocking().first(); Observable.merge(observableList).toBlocking().forEach(new Action1<Long>() { @Override public void call(Long o) { } }); Assert.assertEquals(1, 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);  }