View Javadoc
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.nio.charset.Charset;
18  
19  /**
20   * An abstract hasher, implementing {@link #putBoolean(boolean)}, {@link #putDouble(double)},
21   * {@link #putFloat(float)}, {@link #putUnencodedChars(CharSequence)}, and
22   * {@link #putString(CharSequence, Charset)} as prescribed by {@link Hasher}.
23   *
24   * @author Dimitris Andreou
25   */
26  abstract class AbstractHasher implements Hasher {
27    @Override public final Hasher putBoolean(boolean b) {
28      return putByte(b ? (byte) 1 : (byte) 0);
29    }
30  
31    @Override public final Hasher putDouble(double d) {
32      return putLong(Double.doubleToRawLongBits(d));
33    }
34  
35    @Override public final Hasher putFloat(float f) {
36      return putInt(Float.floatToRawIntBits(f));
37    }
38  
39    @Override public Hasher putUnencodedChars(CharSequence charSequence) {
40      for (int i = 0, len = charSequence.length(); i < len; i++) {
41        putChar(charSequence.charAt(i));
42      }
43      return this;
44    }
45  
46    @Override public Hasher putString(CharSequence charSequence, Charset charset) {
47      return putBytes(charSequence.toString().getBytes(charset));
48    }
49  }