Coverage Summary for Class: AbstractHasher (com.google.common.hash)

Class Class, % Method, % Line, %
AbstractHasher 0% (0/1) 0% (0/14) 0% (0/35)


1 /* 2  * Copyright (C) 2011 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 com.google.common.base.Preconditions; 18 import com.google.errorprone.annotations.CanIgnoreReturnValue; 19 import java.nio.ByteBuffer; 20 import java.nio.charset.Charset; 21 import org.checkerframework.checker.nullness.qual.Nullable; 22  23 /** 24  * An abstract implementation of {@link Hasher}, which only requires subtypes to implement {@link 25  * #putByte}. Subtypes may provide more efficient implementations, however. 26  * 27  * @author Dimitris Andreou 28  */ 29 @CanIgnoreReturnValue 30 @ElementTypesAreNonnullByDefault 31 abstract class AbstractHasher implements Hasher { 32  @Override 33  public final Hasher putBoolean(boolean b) { 34  return putByte(b ? (byte) 1 : (byte) 0); 35  } 36  37  @Override 38  public final Hasher putDouble(double d) { 39  return putLong(Double.doubleToRawLongBits(d)); 40  } 41  42  @Override 43  public final Hasher putFloat(float f) { 44  return putInt(Float.floatToRawIntBits(f)); 45  } 46  47  @Override 48  public Hasher putUnencodedChars(CharSequence charSequence) { 49  for (int i = 0, len = charSequence.length(); i < len; i++) { 50  putChar(charSequence.charAt(i)); 51  } 52  return this; 53  } 54  55  @Override 56  public Hasher putString(CharSequence charSequence, Charset charset) { 57  return putBytes(charSequence.toString().getBytes(charset)); 58  } 59  60  @Override 61  public Hasher putBytes(byte[] bytes) { 62  return putBytes(bytes, 0, bytes.length); 63  } 64  65  @Override 66  public Hasher putBytes(byte[] bytes, int off, int len) { 67  Preconditions.checkPositionIndexes(off, off + len, bytes.length); 68  for (int i = 0; i < len; i++) { 69  putByte(bytes[off + i]); 70  } 71  return this; 72  } 73  74  @Override 75  public Hasher putBytes(ByteBuffer b) { 76  if (b.hasArray()) { 77  putBytes(b.array(), b.arrayOffset() + b.position(), b.remaining()); 78  Java8Compatibility.position(b, b.limit()); 79  } else { 80  for (int remaining = b.remaining(); remaining > 0; remaining--) { 81  putByte(b.get()); 82  } 83  } 84  return this; 85  } 86  87  @Override 88  public Hasher putShort(short s) { 89  putByte((byte) s); 90  putByte((byte) (s >>> 8)); 91  return this; 92  } 93  94  @Override 95  public Hasher putInt(int i) { 96  putByte((byte) i); 97  putByte((byte) (i >>> 8)); 98  putByte((byte) (i >>> 16)); 99  putByte((byte) (i >>> 24)); 100  return this; 101  } 102  103  @Override 104  public Hasher putLong(long l) { 105  for (int i = 0; i < 64; i += 8) { 106  putByte((byte) (l >>> i)); 107  } 108  return this; 109  } 110  111  @Override 112  public Hasher putChar(char c) { 113  putByte((byte) c); 114  putByte((byte) (c >>> 8)); 115  return this; 116  } 117  118  @Override 119  public <T extends @Nullable Object> Hasher putObject( 120  @ParametricNullness T instance, Funnel<? super T> funnel) { 121  funnel.funnel(instance, this); 122  return this; 123  } 124 }