import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.Supplier;
class Ideone
{
@FunctionalInterface
public interface Lazy<T> extends Supplier<T> {
abstract class Cache {
private volatile static Map
<Integer, Object
> instances
= new HashMap
<>();
private static synchronized Object getInstance
(int instanceId, Supplier
<Object
> create
) {
Object instance
= instances.
get(instanceId
); if (instance == null) {
synchronized (Cache.class) {
instance = instances.get(instanceId);
if (instance == null) {
instance = create.get();
instances.put(instanceId, instance);
}
}
}
return instance;
}
}
@Override
default T get() {
return (T) Cache.getInstance(this.hashCode(), () -> init());
}
T init();
}
static Lazy<String> name1 = () -> {
System.
out.
println("lazy init 1"); return "name 1";
};
static Lazy<String> name2 = () -> {
System.
out.
println("lazy init 2"); return "name 2";
};
{
System.
out.
println(name1.
get()); System.
out.
println(name1.
get()); System.
out.
println(name2.
get()); System.
out.
println(name2.
get()); }
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgppbXBvcnQgamF2YS51dGlsLmZ1bmN0aW9uLlN1cHBsaWVyOwoKY2xhc3MgSWRlb25lCnsKCQkKCQpARnVuY3Rpb25hbEludGVyZmFjZQpwdWJsaWMgaW50ZXJmYWNlIExhenk8VD4gZXh0ZW5kcyBTdXBwbGllcjxUPiB7CiAgICBhYnN0cmFjdCBjbGFzcyBDYWNoZSB7CiAgICAgICAgcHJpdmF0ZSB2b2xhdGlsZSBzdGF0aWMgTWFwPEludGVnZXIsIE9iamVjdD4gaW5zdGFuY2VzID0gbmV3IEhhc2hNYXA8PigpOwoKICAgICAgICBwcml2YXRlIHN0YXRpYyBzeW5jaHJvbml6ZWQgT2JqZWN0IGdldEluc3RhbmNlKGludCBpbnN0YW5jZUlkLCBTdXBwbGllcjxPYmplY3Q+IGNyZWF0ZSkgewoKICAgICAgICAgICAgT2JqZWN0IGluc3RhbmNlID0gaW5zdGFuY2VzLmdldChpbnN0YW5jZUlkKTsKICAgICAgICAgICAgaWYgKGluc3RhbmNlID09IG51bGwpIHsKICAgICAgICAgICAgICAgIHN5bmNocm9uaXplZCAoQ2FjaGUuY2xhc3MpIHsKICAgICAgICAgICAgICAgICAgICBpbnN0YW5jZSA9IGluc3RhbmNlcy5nZXQoaW5zdGFuY2VJZCk7CiAgICAgICAgICAgICAgICAgICAgaWYgKGluc3RhbmNlID09IG51bGwpIHsKICAgICAgICAgICAgICAgICAgICAgICAgaW5zdGFuY2UgPSBjcmVhdGUuZ2V0KCk7CiAgICAgICAgICAgICAgICAgICAgICAgIGluc3RhbmNlcy5wdXQoaW5zdGFuY2VJZCwgaW5zdGFuY2UpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm4gaW5zdGFuY2U7CiAgICAgICAgfQogICAgfQoKICAgIEBPdmVycmlkZQogICAgZGVmYXVsdCBUIGdldCgpIHsKICAgICAgICByZXR1cm4gKFQpIENhY2hlLmdldEluc3RhbmNlKHRoaXMuaGFzaENvZGUoKSwgKCkgLT4gaW5pdCgpKTsKICAgIH0KCiAgICBUIGluaXQoKTsKfQoKCglzdGF0aWMgTGF6eTxTdHJpbmc+IG5hbWUxID0gKCkgLT4geyAKCQlTeXN0ZW0ub3V0LnByaW50bG4oImxhenkgaW5pdCAxIik7IAoJCXJldHVybiAibmFtZSAxIjsKCX07CgkJCglzdGF0aWMgTGF6eTxTdHJpbmc+IG5hbWUyID0gKCkgLT4geyAKCQlTeXN0ZW0ub3V0LnByaW50bG4oImxhenkgaW5pdCAyIik7IAoJCXJldHVybiAibmFtZSAyIjsKCX07CgoKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCVN5c3RlbS5vdXQucHJpbnRsbigic3RhcnQiKTsgCgkJU3lzdGVtLm91dC5wcmludGxuKG5hbWUxLmdldCgpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4obmFtZTEuZ2V0KCkpOwoJCVN5c3RlbS5vdXQucHJpbnRsbihuYW1lMi5nZXQoKSk7CgkJU3lzdGVtLm91dC5wcmludGxuKG5hbWUyLmdldCgpKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4oImVuZCIpOyAKCX0KCn0=