From ac439cca91ae6ca8d14c6e42110011756f62b222 Mon Sep 17 00:00:00 2001 From: Jinghai Ren Date: Sun, 13 Sep 2020 16:23:41 -0700 Subject: [PATCH] Simplify the implementation of memoizeSupplier. --- .../main/java/cyclops/function/Memoize.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/cyclops/src/main/java/cyclops/function/Memoize.java b/cyclops/src/main/java/cyclops/function/Memoize.java index f302e68df0..7516952187 100644 --- a/cyclops/src/main/java/cyclops/function/Memoize.java +++ b/cyclops/src/main/java/cyclops/function/Memoize.java @@ -23,7 +23,7 @@ import lombok.val; public class Memoize { - private final static Object UNSET = new Object(); + /** * Convert a Supplier into one that caches it's result * @@ -31,20 +31,17 @@ public class Memoize { * @return Memoised Supplier */ public static Function0 memoizeSupplier(final Supplier s) { - AtomicReference value = new AtomicReference<>( - UNSET); - return ()->{ - Object val = value.get(); - if (val == UNSET) { - synchronized (value){ - if(value.get()==UNSET) { - val = s.get(); - value.set(val); + final AtomicReference atomic = new AtomicReference<>(null); + return () -> { + if (atomic.get() == null) { + synchronized (atomic) { + if (atomic.get() == null) { + T val = s.get(); + atomic.set(val); + } } } - } - - return (T)val; + return atomic.get(); }; }