Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions internal-api/src/main/java/datadog/trace/util/HashingUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package datadog.trace.util;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* This class is intended to be a drop-in replacement for the hashing portions of java.util.Objects.
* This class provides more convenience methods for hashing primitives and includes overrides for
Expand All @@ -8,7 +11,7 @@
public final class HashingUtils {
private HashingUtils() {}

public static final int hashCode(Object obj) {
public static final int hashCode(@Nullable Object obj) {
return obj != null ? obj.hashCode() : 0;
}

Expand Down Expand Up @@ -102,7 +105,7 @@ public static final int addToHash(int hash, int value) {
return 31 * hash + value;
}

public static final int addToHash(int hash, Object obj) {
public static final int addToHash(int hash, @Nullable Object obj) {
return addToHash(hash, hashCode(obj));
}

Expand Down Expand Up @@ -134,6 +137,18 @@ public static final int addToHash(int hash, double value) {
return addToHash(hash, Double.hashCode(value));
}

/** Folds {@code arr[0..len)}; the array must be non-null, but its elements may be null. */
public static final int addToHash(int hash, @Nonnull Object[] arr, int len) {
for (int i = 0; i < len; i++) {
hash = addToHash(hash, arr[i]);
}
return hash;
}

public static final int addToHash(int hash, @Nonnull Object[] arr) {
return addToHash(hash, arr, arr.length);
Comment thread
dougqh marked this conversation as resolved.
}

public static final int hash(Iterable<?> objs) {
int result = 0;
for (Object obj : objs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package datadog.trace.util;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* This class is intended to be a drop-in replacement for the hashing portions of java.util.Objects.
* This class provides more convenience methods for hashing primitives and includes overrides for
Expand Down Expand Up @@ -52,7 +55,7 @@ static final long hash(int hash0, int hash1) {
return 31L * hash0 + hash1;
}

private static final int intHash(Object obj) {
private static final int intHash(@Nullable Object obj) {
return obj == null ? 0 : obj.hashCode();
}

Expand Down Expand Up @@ -106,7 +109,7 @@ public static final long addToHash(long hash, int value) {
return 31L * hash + value;
}

public static final long addToHash(long hash, Object obj) {
public static final long addToHash(long hash, @Nullable Object obj) {
return addToHash(hash, intHash(obj));
}

Expand Down Expand Up @@ -138,14 +141,15 @@ public static final long addToHash(long hash, double value) {
return addToHash(hash, Double.hashCode(value));
}

public static final long addToHash(long hash, Object[] arr, int len) {
/** Folds {@code arr[0..len)}; the array must be non-null, but its elements may be null. */
public static final long addToHash(long hash, @Nonnull Object[] arr, int len) {
for (int i = 0; i < len; i++) {
hash = addToHash(hash, arr[i]);
}
return hash;
}

public static final long addToHash(long hash, Object[] arr) {
public static final long addToHash(long hash, @Nonnull Object[] arr) {
return addToHash(hash, arr, arr.length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ public void hashArrayAndIterable() {
assertEquals(hashArray, hashIterable);
}

@Test
public void addToHashArrayFoldsFromSeedLikeChainedAddToHash() {
Object[] array = new Object[] {"foo", "bar", "quux"};

// Full-array overload folds every element onto the seed; from zero it matches hash(Object[]).
assertEquals(HashingUtils.hash(array), HashingUtils.addToHash(0, array));

// A non-zero seed carries through, so the result differs from the zero-seed fold.
assertNotEquals(HashingUtils.addToHash(0, array), HashingUtils.addToHash(1, array));
}

@Test
public void addToHashArrayRespectsLen() {
Object[] array = new Object[] {"foo", "bar", "quux"};

// The len override folds only the first len elements.
assertEquals(
HashingUtils.hash(new Object[] {"foo", "bar"}), HashingUtils.addToHash(0, array, 2));
assertNotEquals(HashingUtils.addToHash(0, array), HashingUtils.addToHash(0, array, 2));

// len==0 never enters the loop and returns the seed unchanged.
assertEquals(42, HashingUtils.addToHash(42, array, 0));
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
public void booleans(boolean value) {
Expand Down
Loading