FileSponge/src/main/java/org/warp/filesponge/DataBlock.java

97 lines
2.5 KiB
Java
Raw Normal View History

2020-09-23 23:59:11 +02:00
/*
* FileSponge
2021-03-07 15:50:11 +01:00
* Copyright (C) 2021 Andrea Cavalli
2020-09-23 23:59:11 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2021-03-07 15:50:11 +01:00
package org.warp.filesponge;
2020-09-23 23:59:11 +02:00
2022-06-22 12:04:38 +02:00
import static java.lang.Math.toIntExact;
2023-03-06 12:20:40 +01:00
import it.cavallium.buffer.Buf;
2023-02-28 23:11:07 +01:00
import java.nio.Buffer;
2021-05-05 17:31:50 +02:00
import java.util.Objects;
2022-01-04 12:59:21 +01:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2021-03-07 15:50:11 +01:00
2023-02-28 23:11:07 +01:00
public final class DataBlock {
2021-03-07 15:50:11 +01:00
2023-02-28 23:11:07 +01:00
public static DataBlock EMPTY = new DataBlock(-1, -1, null);
2021-10-01 19:17:46 +02:00
2023-02-28 23:11:07 +01:00
public static DataBlock of(long offset, int length, Buf data) {
2021-10-01 19:17:46 +02:00
return new DataBlock(offset, length, data);
2021-09-03 02:23:19 +02:00
}
2023-02-28 23:11:07 +01:00
private DataBlock(long offset, int length, Buf data) {
this.offset = offset;
this.length = length;
this.data = data;
2021-05-03 00:33:04 +02:00
}
2022-06-22 12:04:38 +02:00
private final long offset;
2021-05-05 17:31:50 +02:00
private final int length;
2023-02-28 23:11:07 +01:00
private final Buf data;
2021-09-03 02:23:19 +02:00
2023-02-28 23:11:07 +01:00
public Buf getData() {
2021-09-03 02:23:19 +02:00
return data;
2021-05-03 00:33:04 +02:00
}
2021-03-07 15:50:11 +01:00
public int getId() {
2022-06-22 12:04:38 +02:00
return toIntExact(offset / FileSponge.BLOCK_SIZE);
2021-03-07 15:50:11 +01:00
}
2021-05-05 17:31:50 +02:00
2022-06-22 12:04:38 +02:00
public long getOffset() {
2021-05-05 17:31:50 +02:00
return this.offset;
}
public int getLength() {
return this.length;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
}
2023-02-28 23:11:07 +01:00
if (!(o instanceof final DataBlock other)) {
2021-05-05 17:31:50 +02:00
return false;
}
if (this.getOffset() != other.getOffset()) {
return false;
}
if (this.getLength() != other.getLength()) {
return false;
}
2023-02-28 23:11:07 +01:00
final Object this$data = this.getData();
final Object other$data = other.getData();
return Objects.equals(this$data, other$data);
2021-05-05 17:31:50 +02:00
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
2022-06-22 12:04:38 +02:00
long offset = this.getOffset();
result = result * PRIME + (int) (offset ^ (offset >>> 32));
2021-05-05 17:31:50 +02:00
result = result * PRIME + this.getLength();
2023-02-28 23:11:07 +01:00
final Object $data = this.getData();
2021-05-05 17:31:50 +02:00
result = result * PRIME + ($data == null ? 43 : $data.hashCode());
return result;
}
public String toString() {
2023-02-28 23:11:07 +01:00
return "DataBlock(offset=" + this.getOffset() + ", length=" + this.getLength() + ", data=" + this.getData() + ")";
2021-05-05 17:31:50 +02:00
}
2020-09-23 23:59:11 +02:00
}