aboutsummaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/hash/HashCodes.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava/src/com/google/common/hash/HashCodes.java')
-rw-r--r--guava/src/com/google/common/hash/HashCodes.java92
1 files changed, 19 insertions, 73 deletions
diff --git a/guava/src/com/google/common/hash/HashCodes.java b/guava/src/com/google/common/hash/HashCodes.java
index b911c28..b6101a5 100644
--- a/guava/src/com/google/common/hash/HashCodes.java
+++ b/guava/src/com/google/common/hash/HashCodes.java
@@ -14,35 +14,23 @@
package com.google.common.hash;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-
-import com.google.common.annotations.Beta;
-import com.google.common.primitives.UnsignedInts;
-
-import java.io.Serializable;
-
/**
- * Static factories for creating {@link HashCode} instances; most users should never have to use
- * this. All returned instances are {@link Serializable}.
- *
- * @author Dimitris Andreou
- * @since 12.0
+ * Static factories for {@link HashCode} instances.
+ *
+ * @author andreou@google.com (Dimitris Andreou)
*/
-@Beta
-public final class HashCodes {
- private HashCodes() {}
-
+final class HashCodes {
+ private HashCodes() { }
+
/**
* Creates a 32-bit {@code HashCode}, of which the bytes will form the passed int, interpreted
* in little endian order.
*/
- public static HashCode fromInt(int hash) {
+ static HashCode fromInt(int hash) {
return new IntHashCode(hash);
}
- private static final class IntHashCode extends HashCode implements Serializable {
+ private static class IntHashCode extends HashCode {
final int hash;
IntHashCode(int hash) {
@@ -68,24 +56,17 @@ public final class HashCodes {
@Override public long asLong() {
throw new IllegalStateException("this HashCode only has 32 bits; cannot create a long");
}
-
- @Override
- public long padToLong() {
- return UnsignedInts.toLong(hash);
- }
-
- private static final long serialVersionUID = 0;
}
/**
* Creates a 64-bit {@code HashCode}, of which the bytes will form the passed long, interpreted
* in little endian order.
*/
- public static HashCode fromLong(long hash) {
+ static HashCode fromLong(long hash) {
return new LongHashCode(hash);
}
- private static final class LongHashCode extends HashCode implements Serializable {
+ private static class LongHashCode extends HashCode {
final long hash;
LongHashCode(long hash) {
@@ -115,37 +96,22 @@ public final class HashCodes {
@Override public long asLong() {
return hash;
}
-
- @Override
- public long padToLong() {
- return hash;
- }
-
- private static final long serialVersionUID = 0;
}
-
- /**
- * Creates a {@code HashCode} from a byte array. The array is defensively copied to preserve
- * the immutability contract of {@code HashCode}. The array cannot be empty.
- */
- public static HashCode fromBytes(byte[] bytes) {
- checkArgument(bytes.length >= 1, "A HashCode must contain at least 1 byte.");
- return fromBytesNoCopy(bytes.clone());
- }
-
+
/**
* Creates a {@code HashCode} from a byte array. The array is <i>not</i> copied defensively,
* so it must be handed-off so as to preserve the immutability contract of {@code HashCode}.
+ * The array must be at least of length 4 (not checked).
*/
- static HashCode fromBytesNoCopy(byte[] bytes) {
+ static HashCode fromBytes(byte[] bytes) {
return new BytesHashCode(bytes);
}
- private static final class BytesHashCode extends HashCode implements Serializable {
+ private static class BytesHashCode extends HashCode {
final byte[] bytes;
BytesHashCode(byte[] bytes) {
- this.bytes = checkNotNull(bytes);
+ this.bytes = bytes;
}
@Override public int bits() {
@@ -157,8 +123,6 @@ public final class HashCodes {
}
@Override public int asInt() {
- checkState(bytes.length >= 4,
- "HashCode#asInt() requires >= 4 bytes (it only has %s bytes).", bytes.length);
return (bytes[0] & 0xFF)
| ((bytes[1] & 0xFF) << 8)
| ((bytes[2] & 0xFF) << 16)
@@ -166,8 +130,10 @@ public final class HashCodes {
}
@Override public long asLong() {
- checkState(bytes.length >= 8,
- "HashCode#asLong() requires >= 8 bytes (it only has %s bytes).", bytes.length);
+ if (bytes.length < 8) {
+ // Checking this to throw the correct type of exception
+ throw new IllegalStateException("Not enough bytes");
+ }
return (bytes[0] & 0xFFL)
| ((bytes[1] & 0xFFL) << 8)
| ((bytes[2] & 0xFFL) << 16)
@@ -177,25 +143,5 @@ public final class HashCodes {
| ((bytes[6] & 0xFFL) << 48)
| ((bytes[7] & 0xFFL) << 56);
}
-
- @Override
- public long padToLong() {
- return (bytes.length < 8) ? UnsignedInts.toLong(asInt()) : asLong();
- }
-
- @Override
- public int hashCode() {
- if (bytes.length >= 4) {
- return asInt();
- } else {
- int val = (bytes[0] & 0xFF);
- for (int i = 1; i < bytes.length; i++) {
- val |= ((bytes[i] & 0xFF) << (i * 8));
- }
- return val;
- }
- }
-
- private static final long serialVersionUID = 0;
}
}