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;
018import com.google.errorprone.annotations.CanIgnoreReturnValue;
019import java.nio.charset.Charset;
020
021/**
022 * An object which can receive a stream of primitive values.
023 *
024 * @author Kevin Bourrillion
025 * @since 12.0 (in 11.0 as {@code Sink})
026 */
027@Beta
028@CanIgnoreReturnValue
029public interface PrimitiveSink {
030  /**
031   * Puts a byte into this sink.
032   *
033   * @param b a byte
034   * @return this instance
035   */
036  PrimitiveSink putByte(byte b);
037
038  /**
039   * Puts an array of bytes into this sink.
040   *
041   * @param bytes a byte array
042   * @return this instance
043   */
044  PrimitiveSink putBytes(byte[] bytes);
045
046  /**
047   * Puts a chunk of an array of bytes into this sink. {@code bytes[off]} is the first byte written,
048   * {@code bytes[off + len - 1]} is the last.
049   *
050   * @param bytes a byte array
051   * @param off the start offset in the array
052   * @param len the number of bytes to write
053   * @return this instance
054   * @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > bytes.length} or
055   *     {@code len < 0}
056   */
057  PrimitiveSink putBytes(byte[] bytes, int off, int len);
058
059  /**
060   * Puts a short into this sink.
061   */
062  PrimitiveSink putShort(short s);
063
064  /**
065   * Puts an int into this sink.
066   */
067  PrimitiveSink putInt(int i);
068
069  /**
070   * Puts a long into this sink.
071   */
072  PrimitiveSink putLong(long l);
073
074  /**
075   * Puts a float into this sink.
076   */
077  PrimitiveSink putFloat(float f);
078
079  /**
080   * Puts a double into this sink.
081   */
082  PrimitiveSink putDouble(double d);
083
084  /**
085   * Puts a boolean into this sink.
086   */
087  PrimitiveSink putBoolean(boolean b);
088
089  /**
090   * Puts a character into this sink.
091   */
092  PrimitiveSink putChar(char c);
093
094  /**
095   * Puts each 16-bit code unit from the {@link CharSequence} into this sink.
096   *
097   * <p><b>Warning:</b> This method will produce different output than most other languages do when
098   * running on the equivalent input. For cross-language compatibility, use {@link #putString},
099   * usually with a charset of UTF-8. For other use cases, use {@code putUnencodedChars}.
100   *
101   * @since 15.0 (since 11.0 as putString(CharSequence))
102   */
103  PrimitiveSink putUnencodedChars(CharSequence charSequence);
104
105  /**
106   * Puts a string into this sink using the given charset.
107   *
108   * <p><b>Warning:</b> This method, which reencodes the input before processing it, is useful only
109   * for cross-language compatibility. For other use cases, prefer {@link #putUnencodedChars}, which
110   * is faster, produces the same output across Java releases, and processes every {@code char} in
111   * the input, even if some are invalid.
112   */
113  PrimitiveSink putString(CharSequence charSequence, Charset charset);
114}