aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Ulf Adams <ulfjack@google.com>2016-04-20 12:04:21 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-04-20 14:23:48 +0000
commit8feea7eb117d4c0ca3b8bfd3fa6b8e3b40a2977f (patch)
tree32d2fb72817a03200ce2be2d03bad30d712e5d66
parente173fc55d22dd1b74ce4a1602c6e018f0572215f (diff)
Add nullable variants to Fingerprint.
-- MOS_MIGRATED_REVID=120325869
-rw-r--r--src/main/java/com/google/devtools/build/lib/util/Fingerprint.java42
-rw-r--r--src/test/java/com/google/devtools/build/lib/util/FingerprintTest.java23
2 files changed, 65 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
diff --git a/src/test/java/com/google/devtools/build/lib/util/FingerprintTest.java b/src/test/java/com/google/devtools/build/lib/util/FingerprintTest.java
index 5aeb9685fe..42fc24e943 100644
--- a/src/test/java/com/google/devtools/build/lib/util/FingerprintTest.java
+++ b/src/test/java/com/google/devtools/build/lib/util/FingerprintTest.java
@@ -134,4 +134,27 @@ public class FingerprintTest {
assertThat("01cc3eeea3a2f58e447e824f9f62d3d1").isEqualTo(
new Fingerprint().addPath(p).hexDigestAndReset());
}
+
+ @Test
+ public void addNullableBoolean() throws Exception {
+ String f1 = new Fingerprint().addNullableBoolean(null).hexDigestAndReset();
+ assertThat(f1).isEqualTo(new Fingerprint().addNullableBoolean(null).hexDigestAndReset());
+ assertThat(f1).isNotEqualTo(new Fingerprint().addNullableBoolean(false).hexDigestAndReset());
+ assertThat(f1).isNotEqualTo(new Fingerprint().addNullableBoolean(true).hexDigestAndReset());
+ }
+
+ @Test
+ public void addNullableInteger() throws Exception {
+ String f1 = new Fingerprint().addNullableInt(null).hexDigestAndReset();
+ assertThat(f1).isEqualTo(new Fingerprint().addNullableInt(null).hexDigestAndReset());
+ assertThat(f1).isNotEqualTo(new Fingerprint().addNullableInt(0).hexDigestAndReset());
+ assertThat(f1).isNotEqualTo(new Fingerprint().addNullableInt(1).hexDigestAndReset());
+ }
+
+ @Test
+ public void addNullableString() throws Exception {
+ String f1 = new Fingerprint().addNullableString(null).hexDigestAndReset();
+ assertThat(f1).isEqualTo(new Fingerprint().addNullableString(null).hexDigestAndReset());
+ assertThat(f1).isNotEqualTo(new Fingerprint().addNullableString("").hexDigestAndReset());
+ }
}