diff options
author | 2016-04-20 12:04:21 +0000 | |
---|---|---|
committer | 2016-04-20 14:23:48 +0000 | |
commit | 8feea7eb117d4c0ca3b8bfd3fa6b8e3b40a2977f (patch) | |
tree | 32d2fb72817a03200ce2be2d03bad30d712e5d66 /src/main/java/com/google/devtools/build/lib | |
parent | e173fc55d22dd1b74ce4a1602c6e018f0572215f (diff) |
Add nullable variants to Fingerprint.
--
MOS_MIGRATED_REVID=120325869
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/util/Fingerprint.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/util/Fingerprint.java b/src/main/java/com/google/devtools/build/lib/util/Fingerprint.java index 443b8a02ea..eb053f9be6 100644 --- a/src/main/java/com/google/devtools/build/lib/util/Fingerprint.java +++ b/src/main/java/com/google/devtools/build/lib/util/Fingerprint.java @@ -25,6 +25,8 @@ import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.UUID; +import javax.annotation.Nullable; + /** * Simplified wrapper for MD5 message digests. See also * com.google.math.crypto.MD5HMAC for a similar interface. @@ -139,6 +141,14 @@ public final class Fingerprint { } /** + * Updates the digest with a boolean value, correctly handling null. + */ + public Fingerprint addNullableBoolean(Boolean input) { + addInt(input == null ? -1 : (input.booleanValue() ? 1 : 0)); + return this; + } + + /** * Updates the digest with the little-endian bytes of a given int value. * * @param input the integer with which to update the digest @@ -175,6 +185,22 @@ public final class Fingerprint { } /** + * Updates the digest with the little-endian bytes of a given int value, correctly distinguishing + * between null and non-null values. + * + * @param input the integer with which to update the digest + */ + public Fingerprint addNullableInt(@Nullable Integer input) { + if (input == null) { + addInt(0); + } else { + addInt(1); + addInt(input); + } + return this; + } + + /** * Updates the digest with a UUID. * * @param uuid the UUID with which to update the digest. Must not be null. @@ -199,6 +225,22 @@ public final class Fingerprint { } /** + * Updates the digest with a String using its length plus its UTF8 encoded bytes; if the string + * is null, then it uses -1 as the length. + * + * @param input the String with which to update the digest + * @see java.security.MessageDigest#update(byte[]) + */ + public Fingerprint addNullableString(@Nullable String input) { + if (input == null) { + addInt(-1); + } else { + addString(input); + } + return this; + } + + /** * Updates the digest with a String using its length and content. * * @param input the String with which to update the digest |