    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
    015    package com.google.common.hash;
    016
    017    import com.google.common.annotations.Beta;
    018
    019    import 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     * @author Kevin Bourrillion
    027     * @since 11.0
    028     */
    029    @Beta
    030    public interface Hasher extends PrimitiveSink {
    031      @Override Hasher putByte(byte b);
    032      @Override Hasher putBytes(byte[] bytes);
    033      @Override Hasher putBytes(byte[] bytes, int off, int len);
    034      @Override Hasher putShort(short s);
    035      @Override Hasher putInt(int i);
    036      @Override Hasher putLong(long l);
    037
    038      /**
    039       * Equivalent to {@code putInt(Float.floatToRawIntBits(f))}.
    040       */
    041      @Override Hasher putFloat(float f);
    042
    043      /**
    044       * Equivalent to {@code putLong(Double.doubleToRawLongBits(d))}.
    045       */
    046      @Override Hasher putDouble(double d);
    047
    048      /**
    049       * Equivalent to {@code putByte(b ? (byte) 1 : (byte) 0)}.
    050       */
    051      @Override Hasher putBoolean(boolean b);
    052      @Override Hasher putChar(char c);
    053
    054      /**
    055       * Equivalent to processing each {@code char} value in the {@code CharSequence}, in order.
    056       * The input must not be updated while this method is in progress.
    057       */
    058      @Override Hasher putString(CharSequence charSequence);
    059
    060      /**
    061       * Equivalent to {@code putBytes(charSequence.toString().getBytes(charset))}.
    062       */
    063      @Override Hasher putString(CharSequence charSequence, Charset charset);
    064
    065      /**
    066       * A simple convenience for {@code funnel.funnel(object, this)}.
    067       */
    068      <T> Hasher putObject(T instance, Funnel<? super T> funnel);
    069
    070      /**
    071       * Computes a hash code based on the data that have been provided to this hasher. The result is
    072       * unspecified if this method is called more than once on the same instance.
    073       */
    074      HashCode hash();
    075    }