/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.uniffle.storage.common;

import java.util.Objects;

public class FileBasedShuffleSegment extends ShuffleSegment implements Comparable<FileBasedShuffleSegment> {

  public static final int SEGMENT_SIZE = 4 * Long.BYTES + 2 * Integer.BYTES;
  private long offset;
  private int length;
  private int uncompressLength;
  private long crc;
  private long blockId;
  private long taskAttemptId;

  public FileBasedShuffleSegment(
      long blockId,
      long offset,
      int length,
      int uncompressLength,
      long crc,
      long taskAttemptId) {
    this.offset = offset;
    this.length = length;
    this.uncompressLength = uncompressLength;
    this.crc = crc;
    this.blockId = blockId;
    this.taskAttemptId = taskAttemptId;
  }

  public long getOffset() {
    return offset;
  }

  public void setOffset(long offset) {
    this.offset = offset;
  }

  public int getLength() {
    return length;
  }

  public void setLength(int length) {
    this.length = length;
  }

  public long getCrc() {
    return crc;
  }

  public long getBlockId() {
    return blockId;
  }

  public void setBlockId(long blockId) {
    this.blockId = blockId;
  }

  public int getUncompressLength() {
    return uncompressLength;
  }

  public long getTaskAttemptId() {
    return taskAttemptId;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    FileBasedShuffleSegment that = (FileBasedShuffleSegment) o;
    return offset == that.offset
        && length == that.length
        && crc == that.crc
        && blockId == that.blockId
        && uncompressLength == that.uncompressLength
        && taskAttemptId == that.taskAttemptId;
  }

  @Override
  public int compareTo(FileBasedShuffleSegment fss) {
    if (this.offset > fss.getOffset()) {
      return 1;
    } else if (this.offset < fss.getOffset()) {
      return -1;
    }
    return 0;
  }

  @Override
  public int hashCode() {
    return Objects.hash(offset, length, uncompressLength, crc, blockId);
  }

  @Override
  public String toString() {
    return "FileBasedShuffleSegment{" + "offset[" + offset + "], length[" + length
        + "], uncompressLength[" + uncompressLength + "], crc[" + crc
        + "], blockId[" + blockId + "], taskAttemptId[" + taskAttemptId + "]}";
  }
}
