Coverage Summary for Class: ChecksumHashFunction (com.google.common.hash)
| Class | Method, % | Line, % |
|---|---|---|
| ChecksumHashFunction | 0% (0/5) | 0% (0/9) |
| ChecksumHashFunction$ChecksumHasher | 0% (0/5) | 0% (0/9) |
| Total | 0% (0/10) | 0% (0/18) |
1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.hash; 16 17 import static com.google.common.base.Preconditions.checkArgument; 18 import static com.google.common.base.Preconditions.checkNotNull; 19 20 import com.google.errorprone.annotations.Immutable; 21 import java.io.Serializable; 22 import java.util.zip.Checksum; 23 24 /** 25 * {@link HashFunction} adapter for {@link Checksum} instances. 26 * 27 * @author Colin Decker 28 */ 29 @Immutable 30 @ElementTypesAreNonnullByDefault 31 final class ChecksumHashFunction extends AbstractHashFunction implements Serializable { 32 private final ImmutableSupplier<? extends Checksum> checksumSupplier; 33 private final int bits; 34 private final String toString; 35 36 ChecksumHashFunction( 37 ImmutableSupplier<? extends Checksum> checksumSupplier, int bits, String toString) { 38 this.checksumSupplier = checkNotNull(checksumSupplier); 39 checkArgument(bits == 32 || bits == 64, "bits (%s) must be either 32 or 64", bits); 40 this.bits = bits; 41 this.toString = checkNotNull(toString); 42 } 43 44 @Override 45 public int bits() { 46 return bits; 47 } 48 49 @Override 50 public Hasher newHasher() { 51 return new ChecksumHasher(checksumSupplier.get()); 52 } 53 54 @Override 55 public String toString() { 56 return toString; 57 } 58 59 /** Hasher that updates a checksum. */ 60 private final class ChecksumHasher extends AbstractByteHasher { 61 private final Checksum checksum; 62 63 private ChecksumHasher(Checksum checksum) { 64 this.checksum = checkNotNull(checksum); 65 } 66 67 @Override 68 protected void update(byte b) { 69 checksum.update(b); 70 } 71 72 @Override 73 protected void update(byte[] bytes, int off, int len) { 74 checksum.update(bytes, off, len); 75 } 76 77 @Override 78 public HashCode hash() { 79 long value = checksum.getValue(); 80 if (bits == 32) { 81 /* 82 * The long returned from a 32-bit Checksum will have all 0s for its second word, so the 83 * cast won't lose any information and is necessary to return a HashCode of the correct 84 * size. 85 */ 86 return HashCode.fromInt((int) value); 87 } else { 88 return HashCode.fromLong(value); 89 } 90 } 91 } 92 93 private static final long serialVersionUID = 0L; 94 }