Skip to content

Instantly share code, notes, and snippets.

@winteryoung
Last active December 17, 2017 05:03
Show Gist options
  • Save winteryoung/17a6e3a7c340a45c935a8af08dc2f322 to your computer and use it in GitHub Desktop.
Save winteryoung/17a6e3a7c340a45c935a8af08dc2f322 to your computer and use it in GitHub Desktop.
package com.github.winteryoung.yanwte2.core.utils;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.function.Supplier;
/**
* @author Winter Young
* @since 2017/12/16
*/
public class Lazy<T> {
private static final Object NULL_VALUE = new Object();
private Object value;
private Supplier<T> supplier;
private Lazy(Supplier<T> supplier) {
this.supplier = checkNotNull(supplier);
}
@SuppressWarnings("unchecked")
public synchronized T get() {
if (value == NULL_VALUE) {
return null;
}
if (value != null) {
return (T) value;
}
value = supplier.get();
if (value == null) {
value = NULL_VALUE;
}
return get();
}
public static <T> Lazy<T> of(Supplier<T> supplier) {
return new Lazy<>(supplier);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment