Beispiel #1
0
 public static HashCode fromString(final String string) {
   Preconditions.checkArgument(
       string.length() >= 2, "input string (%s) must have at least 2 characters", string);
   Preconditions.checkArgument(
       string.length() % 2 == 0,
       "input string (%s) must have an even number of characters",
       string);
   final byte[] bytes = new byte[string.length() / 2];
   for (int i = 0; i < string.length(); i += 2) {
     final int ch1 = decode(string.charAt(i)) << 4;
     final int ch2 = decode(string.charAt(i + 1));
     bytes[i / 2] = (byte) (ch1 + ch2);
   }
   return fromBytesNoCopy(bytes);
 }
Beispiel #2
0
 @Override
 public long asLong() {
   Preconditions.checkState(
       this.bytes.length >= 8,
       "HashCode#asLong() requires >= 8 bytes (it only has %s bytes).",
       this.bytes.length);
   return this.padToLong();
 }
Beispiel #3
0
 @Override
 public int asInt() {
   Preconditions.checkState(
       this.bytes.length >= 4,
       "HashCode#asInt() requires >= 4 bytes (it only has %s bytes).",
       this.bytes.length);
   return (this.bytes[0] & 0xFF)
       | (this.bytes[1] & 0xFF) << 8
       | (this.bytes[2] & 0xFF) << 16
       | (this.bytes[3] & 0xFF) << 24;
 }
Beispiel #4
0
 public static HashCode fromBytes(final byte[] bytes) {
   Preconditions.checkArgument(
       bytes.length >= 1, (Object) "A HashCode must contain at least 1 byte.");
   return fromBytesNoCopy(bytes.clone());
 }
Beispiel #5
0
 public int writeBytesTo(final byte[] dest, final int offset, int maxLength) {
   maxLength = Ints.min(maxLength, this.bits() / 8);
   Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
   this.writeBytesToImpl(dest, offset, maxLength);
   return maxLength;
 }
Beispiel #6
0
 BytesHashCode(final byte[] bytes) {
   this.bytes = Preconditions.checkNotNull(bytes);
 }