Skip to content

Instantly share code, notes, and snippets.

@Daio-io
Created September 2, 2016 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daio-io/1430c97dd34c1e60654a6fe17a84cade to your computer and use it in GitHub Desktop.
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
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