Skip to content

Commit 0ca475b

Browse files
committed
Lazy::take, Lazy::forget
1 parent 0c6c36f commit 0ca475b

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

  • src/main/java/de/variantsync/evolution/util/functional

src/main/java/de/variantsync/evolution/util/functional/Lazy.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ public A run() {
8686
return val;
8787
}
8888

89+
/**
90+
* Run the lazy computation, obtain the result, and immediately forget it.
91+
* This method first calls {@link #run()} and then {@link #forget()}.
92+
* @return The result of this lazy computation.
93+
*/
94+
public A take() {
95+
final A result = run();
96+
forget();
97+
return result;
98+
}
99+
100+
/**
101+
* Clears the cached value, such that it has to be recomputed next time it is queried.
102+
* @return True if a value was cleared. False if there was no cached value to forget.
103+
*/
104+
public boolean forget() {
105+
final boolean aValueWasForgotten = val != null;
106+
val = null;
107+
return aValueWasForgotten;
108+
}
109+
89110
/**
90111
* Lazy is a functor.
91112
* @param f Function to apply to the result of this Lazy when it is computed.

0 commit comments

Comments
 (0)