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