-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] adjusting for backport (#36117)
- Loading branch information
Showing
2 changed files
with
32 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/CachedSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.elasticsearch.xpack.core.ml.utils; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A {@link Supplier} that caches its return value. This may be useful to make | ||
* a {@link Supplier} idempotent or for performance reasons if always returning | ||
* the same instance is acceptable. | ||
*/ | ||
public final class CachedSupplier<T> implements Supplier<T> { | ||
|
||
private Supplier<T> supplier; | ||
private T result; | ||
private boolean resultSet; | ||
|
||
public CachedSupplier(Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
@Override | ||
public synchronized T get() { | ||
if (resultSet == false) { | ||
result = supplier.get(); | ||
resultSet = true; | ||
} | ||
return result; | ||
} | ||
|
||
} |