Last active
March 7, 2018 13:39
-
-
Save VinceBurn/c63bbdecdb5626af2a9c4a3dfa33cc6d to your computer and use it in GitHub Desktop.
Generic equality of potentially null values
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
/** | |
* 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