001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.archivers.tar;
020
021import java.util.Objects;
022
023/**
024 * A {@code struct sparse} in a <a href="https://www.gnu.org/software/tar/manual/html_node/Standard.html">Tar archive</a>.
025 * <p>
026 * Whereas, "struct sparse" is:
027 * </p>
028 * <pre>
029 * struct sparse {
030 * char offset[12];   // offset 0
031 * char numbytes[12]; // offset 12
032 * };
033 * </pre>
034 *
035 * @since 1.20
036 */
037public final class TarArchiveStructSparse {
038    private final long offset;
039    private final long numbytes;
040
041    /**
042     * Constructs a new instance.
043     *
044     * @param offset An offset greater or equal to zero.
045     * @param numBytes An count greater or equal to zero.
046     */
047    public TarArchiveStructSparse(final long offset, final long numBytes) {
048        if (offset < 0) {
049            throw new IllegalArgumentException("offset must not be negative");
050        }
051        if (numBytes < 0) {
052            throw new IllegalArgumentException("numbytes must not be negative");
053        }
054        this.offset = offset;
055        this.numbytes = numBytes;
056    }
057
058    @Override
059    public boolean equals(final Object o) {
060        if (this == o) {
061            return true;
062        }
063        if (o == null || getClass() != o.getClass()) {
064            return false;
065        }
066        final TarArchiveStructSparse that = (TarArchiveStructSparse) o;
067        return offset == that.offset && numbytes == that.numbytes;
068    }
069
070    /**
071     * Gets the byte count.
072     *
073     * @return the byte count.
074     */
075    public long getNumbytes() {
076        return numbytes;
077    }
078
079    /**
080     * Gets the offset.
081     *
082     * @return the offset.
083     */
084    public long getOffset() {
085        return offset;
086    }
087
088    @Override
089    public int hashCode() {
090        return Objects.hash(offset, numbytes);
091    }
092
093    @Override
094    public String toString() {
095        return "TarArchiveStructSparse{offset=" + offset + ", numbytes=" + numbytes + '}';
096    }
097}