From 02f4a60f0a6c79fb3e3fdbcf325dd9d958083bde Mon Sep 17 00:00:00 2001 From: Mark Perry Date: Thu, 28 May 2015 21:16:56 +1000 Subject: [PATCH 1/2] Attempt to fix #108 --- core/src/main/java/fj/P1.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/fj/P1.java b/core/src/main/java/fj/P1.java index 5ae82a97..844e4482 100644 --- a/core/src/main/java/fj/P1.java +++ b/core/src/main/java/fj/P1.java @@ -9,7 +9,7 @@ import fj.data.Option; import fj.data.Validation; //import fj.data.*; -import fj.function.Try0; + public abstract class P1 implements F0 { @@ -216,22 +216,22 @@ public P1 memo() { final P1 self = this; return new P1() { private final Object latch = new Object(); - @SuppressWarnings({"InstanceVariableMayNotBeInitialized"}) - private volatile SoftReference v; + private volatile SoftReference> v = null; public A _1() { - A a = v != null ? v.get() : null; - if (a == null) + Option o = v != null ? v.get() : null; + if (o == null) { synchronized (latch) { - if (v == null || v.get() == null) { - a = self._1(); - v = new SoftReference(a); - } else { - a = v.get(); + o = v != null ? v.get() : null; + if (o == null) { + o = Option.some(self._1()); + v = new SoftReference<>(o); } } - return a; + } + return o.some(); } + }; } From b07073d3e013ba347bda51ace61fba0504f9fc54 Mon Sep 17 00:00:00 2001 From: Mark Perry Date: Thu, 28 May 2015 22:14:32 +1000 Subject: [PATCH 2/2] Override memo for an already memoised P1 --- core/src/main/java/fj/P1.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/main/java/fj/P1.java b/core/src/main/java/fj/P1.java index 844e4482..ddb0d04d 100644 --- a/core/src/main/java/fj/P1.java +++ b/core/src/main/java/fj/P1.java @@ -218,6 +218,7 @@ public P1 memo() { private final Object latch = new Object(); private volatile SoftReference> v = null; + @Override public A _1() { Option o = v != null ? v.get() : null; if (o == null) { @@ -232,6 +233,11 @@ public A _1() { return o.some(); } + @Override + public P1 memo() { + return this; + } + }; }