001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.hash;
016
017import com.google.common.annotations.Beta;
018
019import java.nio.charset.Charset;
020
021/**
022 * A {@link PrimitiveSink} that can compute a hash code after reading the input. Each hasher should
023 * translate all multibyte values ({@link #putInt(int)}, {@link #putLong(long)}, etc) to bytes
024 * in little-endian order.
025 *
026 * <p>The result of calling any methods after calling {@link #hash} is undefined.
027 *
028 * <p><b>Warning:</b> Chunks of data that are put into the {@link Hasher} are not delimited.
029 * The resulting {@link HashCode} is dependent only on the bytes inserted, and the order in which
030 * they were inserted, not how those bytes were chunked into discrete put() operations. For example,
031 * the following three expressions all generate colliding hash codes: <pre>   {@code
032 *
033 *   newHasher().putString("ab").putString("c").hash()
034 *   newHasher().putString("a").putString("bc").hash()
035 *   newHasher().putChar('a').putChar('b').putChar('c').hash()}</pre>
036 *
037 * If you wish to avoid this, you should either prepend or append the size of each chunk.
038 * For example:
039 * <pre>   {@code
040 *   newHasher().putInt(s1.length()).putString(s1).putInt(s2.length()).putString(s2).hash()}</pre>
041 *
042 * @author Kevin Bourrillion
043 * @since 11.0
044 */
045@Beta
046public interface Hasher extends PrimitiveSink {
047  @Override Hasher putByte(byte b);
048  @Override Hasher putBytes(byte[] bytes);
049  @Override Hasher putBytes(byte[] bytes, int off, int len);
050  @Override Hasher putShort(short s);
051  @Override Hasher putInt(int i);
052  @Override Hasher putLong(long l);
053
054  /**
055   * Equivalent to {@code putInt(Float.floatToRawIntBits(f))}.
056   */
057  @Override Hasher putFloat(float f);
058
059  /**
060   * Equivalent to {@code putLong(Double.doubleToRawLongBits(d))}.
061   */
062  @Override Hasher putDouble(double d);
063
064  /**
065   * Equivalent to {@code putByte(b ? (byte) 1 : (byte) 0)}.
066   */
067  @Override Hasher putBoolean(boolean b);
068  @Override Hasher putChar(char c);
069
070  /**
071   * Equivalent to processing each {@code char} value in the {@code CharSequence}, in order.
072   * The input must not be updated while this method is in progress.
073   */
074  @Override Hasher putString(CharSequence charSequence);
075
076  /**
077   * Equivalent to {@code putBytes(charSequence.toString().getBytes(charset))}.
078   */
079  @Override Hasher putString(CharSequence charSequence, Charset charset);
080
081  /**
082   * A simple convenience for {@code funnel.funnel(object, this)}.
083   */
084  <T> Hasher putObject(T instance, Funnel<? super T> funnel);
085
086  /**
087   * Computes a hash code based on the data that have been provided to this hasher. The result is
088   * unspecified if this method is called more than once on the same instance.
089   */
090  HashCode hash();
091}