Created
September 2, 2016 18:01
-
-
Save Daio-io/1430c97dd34c1e60654a6fe17a84cade to your computer and use it in GitHub Desktop.
Safely unwrap a WeakReference<T> without having to do chained if null check
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
public final class WeakRefUtil { | |
/** | |
* Safely unwrap a WeakReference<T> | |
* | |
* Returns reference if unwrapped or null | |
* @param reference - WeakReference<T> | |
* @return T | |
*/ | |
@Nullable | |
public static <T> T unwrap(WeakReference<T> reference) { | |
return reference != null ? reference.get() : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment