aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/zajc/gogarchiver/util/LazyValue.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/zajc/gogarchiver/util/LazyValue.java')
-rw-r--r--src/main/java/zajc/gogarchiver/util/LazyValue.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/java/zajc/gogarchiver/util/LazyValue.java b/src/main/java/zajc/gogarchiver/util/LazyValue.java
new file mode 100644
index 0000000..118193f
--- /dev/null
+++ b/src/main/java/zajc/gogarchiver/util/LazyValue.java
@@ -0,0 +1,42 @@
1//SPDX-License-Identifier: GPL-3.0
2/*
3 * gogarchiver-ng, an archival tool for GOG.com
4 * Copyright (C) 2024 Marko Zajc
5 *
6 * This program is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this
15 * program. If not, see <https://www.gnu.org/licenses/>.
16 */
17package zajc.gogarchiver.util;
18
19import java.util.function.Supplier;
20
21import javax.annotation.*;
22
23public class LazyValue<T> {
24
25 @Nullable private volatile T value;
26
27 public T get(@Nonnull Supplier<T> generator) {
28 if (this.value != null)
29 return this.value;
30 synchronized (this) {
31 if (this.value != null) // double checked locking
32 return this.value;
33 else
34 return this.value = generator.get();
35 }
36 }
37
38 public synchronized void unset() {
39 this.value = null;
40 }
41
42}