Skip to content

Commit

Permalink
deferred value: Memoize value to ensure at-most-once supplier call
Browse files Browse the repository at this point in the history
Since we are using deferred values to defer expensive operations, we
don't want concurrent threads all performing the expensive operation.
So we memoize the result of the operation to ensure at-most-once.

Signed-off-by: BJ Hargrave <bj@hargrave.dev>
  • Loading branch information
bjhargrave committed Sep 26, 2022
1 parent 6cd8742 commit 82717fa
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/resource/DeferredValue.java
Expand Up @@ -4,25 +4,22 @@

import java.util.function.Supplier;

import aQute.bnd.memoize.Memoize;

class DeferredValue<T> implements Supplier<T> {
private final Class<T> type;
private final Supplier<? extends T> supplier;
private final int hashCode;
private T value;

DeferredValue(Class<T> type, Supplier<? extends T> supplier, int hashCode) {
this.type = requireNonNull(type);
this.supplier = requireNonNull(supplier);
this.supplier = Memoize.supplier(supplier);
this.hashCode = hashCode;
}

@Override
public T get() {
T v = value;
if (v == null) {
return value = supplier.get();
}
return v;
return supplier.get();
}

Class<T> type() {
Expand Down

0 comments on commit 82717fa

Please sign in to comment.