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
021import javax.annotation.CheckReturnValue;
022
023/**
024 * A {@link PrimitiveSink} that can compute a hash code after reading the input. Each hasher should
025 * translate all multibyte values ({@link #putInt(int)}, {@link #putLong(long)}, etc) to bytes
026 * in little-endian order.
027 *
028 * <p><b>Warning:</b> The result of calling any methods after calling {@link #hash} is undefined.
029 *
030 * <p><b>Warning:</b> Using a specific character encoding when hashing a {@link CharSequence} with
031 * {@link #putString(CharSequence, Charset)} is generally only useful for cross-language
032 * compatibility (otherwise prefer {@link #putUnencodedChars}). However, the character encodings
033 * must be identical across languages. Also beware that {@link Charset} definitions may occasionally
034 * change between Java releases.
035 *
036 * <p><b>Warning:</b> Chunks of data that are put into the {@link Hasher} are not delimited.
037 * The resulting {@link HashCode} is dependent only on the bytes inserted, and the order in which
038 * they were inserted, not how those bytes were chunked into discrete put() operations. For example,
039 * the following three expressions all generate colliding hash codes: <pre>   {@code
040 *
041 *   newHasher().putByte(b1).putByte(b2).putByte(b3).hash()
042 *   newHasher().putByte(b1).putBytes(new byte[] { b2, b3 }).hash()
043 *   newHasher().putBytes(new byte[] { b1, b2, b3 }).hash()}</pre>
044 *
045 * <p>If you wish to avoid this, you should either prepend or append the size of each chunk. Keep in
046 * mind that when dealing with char sequences, the encoded form of two concatenated char sequences
047 * is not equivalent to the concatenation of their encoded form. Therefore,
048 * {@link #putString(CharSequence, Charset)} should only be used consistently with <i>complete</i>
049 * sequences and not broken into chunks.
050 *
051 * @author Kevin Bourrillion
052 * @since 11.0
053 */
054@Beta
055public interface Hasher extends PrimitiveSink {
056  @Override
057  Hasher putByte(byte b);
058
059  @Override
060  Hasher putBytes(byte[] bytes);
061
062  @Override
063  Hasher putBytes(byte[] bytes, int off, int len);
064
065  @Override
066  Hasher putShort(short s);
067
068  @Override
069  Hasher putInt(int i);
070
071  @Override
072  Hasher putLong(long l);
073
074  /**
075   * Equivalent to {@code putInt(Float.floatToRawIntBits(f))}.
076   */
077  @Override
078  Hasher putFloat(float f);
079
080  /**
081   * Equivalent to {@code putLong(Double.doubleToRawLongBits(d))}.
082   */
083  @Override
084  Hasher putDouble(double d);
085
086  /**
087   * Equivalent to {@code putByte(b ? (byte) 1 : (byte) 0)}.
088   */
089  @Override
090  Hasher putBoolean(boolean b);
091
092  @Override
093  Hasher putChar(char c);
094
095  /**
096   * Equivalent to processing each {@code char} value in the {@code CharSequence}, in order.
097   * The input must not be updated while this method is in progress.
098   *
099   * @since 15.0 (since 11.0 as putString(CharSequence)).
100   */
101  @Override
102  Hasher putUnencodedChars(CharSequence charSequence);
103
104  /**
105   * Equivalent to {@code putBytes(charSequence.toString().getBytes(charset))}.
106   */
107  @Override
108  Hasher putString(CharSequence charSequence, Charset charset);
109
110  /**
111   * A simple convenience for {@code funnel.funnel(object, this)}.
112   */
113  <T> Hasher putObject(T instance, Funnel<? super T> funnel);
114
115  /**
116   * Computes a hash code based on the data that have been provided to this hasher. The result is
117   * unspecified if this method is called more than once on the same instance.
118   */
119  @CheckReturnValue
120  HashCode hash();
121
122  /**
123   * {@inheritDoc}
124   *
125   * @deprecated This returns {@link Object#hashCode()}; you almost certainly mean to call
126   *     {@code hash().asInt()}.
127   */
128  @Override
129  @Deprecated
130  int hashCode();
131}