Skip to content

Instantly share code, notes, and snippets.

@VinceBurn
Last active March 7, 2018 13:39
Show Gist options
  • Save VinceBurn/c63bbdecdb5626af2a9c4a3dfa33cc6d to your computer and use it in GitHub Desktop.
Save VinceBurn/c63bbdecdb5626af2a9c4a3dfa33cc6d to your computer and use it in GitHub Desktop.
Generic equality of potentially null values
/**
* Compare 2 nullable for equality. If both are null they are considered to be equal.
*
* @param lhs Left Hand Side value
* @param rhs Right Hand Side value
* @param <T> A Type on which `equals()` will be called
* @return true if the 2 values are equals taking `null` into account.
*/
public static <T> Boolean isEqual(@Nullable T lhs, @Nullable T rhs) {
Boolean result;
if (lhs == null && rhs == null) {
result = true;
} else if (lhs != null && rhs != null) {
result = lhs.equals(rhs);
} else {
result = false;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment