Last active
December 17, 2017 05:03
-
-
Save winteryoung/17a6e3a7c340a45c935a8af08dc2f322 to your computer and use it in GitHub Desktop.
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
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