Browse Source

add ZillaRuntime.lazyGet

tags/2.0.1
Jonathan Cobb 4 years ago
parent
commit
a39467dbcd
1 changed files with 19 additions and 0 deletions
  1. +19
    -0
      src/main/java/org/cobbzilla/util/daemon/ZillaRuntime.java

+ 19
- 0
src/main/java/org/cobbzilla/util/daemon/ZillaRuntime.java View File

@@ -457,4 +457,23 @@ public class ZillaRuntime {
}
}
}

public interface LazyGet<T> { T init();}

public static <T> T lazyGet(AtomicReference<T> ref, LazyGet<T> init, LazyGet<T> error) {
if (ref.get() == null) {
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (ref) {
if (ref.get() == null) {
try {
ref.set(init.init());
} catch (Exception e) {
ref.set(error.init());
}
}
}
}
return ref.get();
}

}

Loading…
Cancel
Save