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 java.io.UnsupportedEncodingException; 18 import java.nio.charset.Charset; 19 20 /** 21 * An abstract hasher, implementing {@link #putBoolean(boolean)}, {@link #putDouble(double)}, 22 * {@link #putFloat(float)}, {@link #putUnencodedChars(CharSequence)}, and 23 * {@link #putString(CharSequence, Charset)} as prescribed by {@link Hasher}. 24 * 25 * @author Dimitris Andreou 26 */ 27 abstract class AbstractHasher implements Hasher { putBoolean(boolean b)28 @Override public final Hasher putBoolean(boolean b) { 29 return putByte(b ? (byte) 1 : (byte) 0); 30 } 31 putDouble(double d)32 @Override public final Hasher putDouble(double d) { 33 return putLong(Double.doubleToRawLongBits(d)); 34 } 35 putFloat(float f)36 @Override public final Hasher putFloat(float f) { 37 return putInt(Float.floatToRawIntBits(f)); 38 } 39 40 /** 41 * @deprecated Use {@link AbstractHasher#putUnencodedChars} instead. 42 */ 43 @Deprecated putString(CharSequence charSequence)44 @Override public Hasher putString(CharSequence charSequence) { 45 return putUnencodedChars(charSequence); 46 } 47 putUnencodedChars(CharSequence charSequence)48 @Override 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 putString(CharSequence charSequence, Charset charset)55 @Override public Hasher putString(CharSequence charSequence, Charset charset) { 56 try { 57 return putBytes(charSequence.toString().getBytes(charset.name())); 58 } catch (UnsupportedEncodingException e) { 59 throw new AssertionError(e); 60 } 61 } 62 } 63