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