• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Charsets;
18 
19 import java.nio.charset.Charset;
20 
21 /**
22  * An abstract hasher, implementing {@link #putBoolean(boolean)}, {@link #putDouble(double)},
23  * {@link #putFloat(float)}, {@link #putString(CharSequence)}, and
24  * {@link #putString(CharSequence, Charset)} as prescribed by {@link Hasher}.
25  *
26  * @author andreou@google.com (Dimitris Andreou)
27  */
28 abstract class AbstractHasher implements Hasher {
putBoolean(boolean b)29   @Override public final Hasher putBoolean(boolean b) {
30     return putByte(b ? (byte) 1 : (byte) 0);
31   }
32 
putDouble(double d)33   @Override public final Hasher putDouble(double d) {
34     return putLong(Double.doubleToRawLongBits(d));
35   }
36 
putFloat(float f)37   @Override public final Hasher putFloat(float f) {
38     return putInt(Float.floatToRawIntBits(f));
39   }
40 
putString(CharSequence charSequence)41   @Override public Hasher putString(CharSequence charSequence) {
42     // TODO(user): Should we instead loop over the CharSequence and call #putChar?
43     return putString(charSequence, Charsets.UTF_16LE);
44   }
45 
putString(CharSequence charSequence, Charset charset)46   @Override public Hasher putString(CharSequence charSequence, Charset charset) {
47     try {
48       return putBytes(charSequence.toString().getBytes(charset.name()));
49     } catch (java.io.UnsupportedEncodingException impossible) {
50       throw new AssertionError(impossible);
51     }
52   }
53 }
54