001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.terminal.region;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023/**
024 * One read from a {@link TerminalSource}.
025 *
026 * <p>
027 * Offsets are absolute byte positions since the stream started; they never reset.  {@code bytes} starts at
028 * {@code next - bytes.length}, which is the requested offset unless {@code truncated} is set.
029 *
030 * <p>
031 * The record holds the {@code bytes} array itself, not a copy, and equality on it is by identity; treat it as read-only.
032 *
033 * @param bytes The bytes read, never <jk>null</jk>; empty at the end of the stream.
034 * @param next The offset to read from next.
035 * @param end The source's current end offset.
036 * @param done Whether the producer will write no more and {@code next == end}.
037 * @param cols The producer's terminal width.
038 * @param rows The producer's terminal height.
039 * @param truncated Whether the requested offset was no longer held, so the bytes start at the oldest offset held.
040 * @param gone Whether the backing store disappeared; implies {@code done}.  A gone chunk is empty, and its {@code next}
041 *    and {@code end} are the requested offset.
042 */
043@SuppressWarnings({
044   "java:S6218" // Equality on bytes is by identity, as documented; chunks are never compared, hashed or printed
045})
046public record TerminalChunk(byte[] bytes, long next, long end, boolean done, int cols, int rows, boolean truncated, boolean gone) {
047
048   /**
049    * Checks the members.
050    *
051    * @throws IllegalArgumentException If a member is out of range.
052    */
053   public TerminalChunk {
054      Objects.requireNonNull(bytes, "bytes");
055      if (next < bytes.length || next > end)
056         throw iaex("TerminalChunk next must be between %s and end %s; got '%s'.", bytes.length, end, next);
057      if (cols < 1 || rows < 1)
058         throw iaex("TerminalChunk size must be at least 1x1; got '%sx%s'.", cols, rows);
059      if (gone && ! done)
060         throw iaex("TerminalChunk gone requires done.");
061   }
062}