Skip to content

Commit

Permalink
Added the DataUnit class, allows to easily convert DataType (kb, mb, …
Browse files Browse the repository at this point in the history
…gb, ects..).
  • Loading branch information
Raft08 committed May 31, 2024
1 parent f69b894 commit d224a65
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
37 changes: 37 additions & 0 deletions common/src/main/java/fr/atlasworld/common/file/DataUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fr.atlasworld.common.file;

public final class DataUnit {
public static final DataUnit BYTES = new DataUnit("Byte", "b", 1);
public static final DataUnit KB = new DataUnit("Kilobyte", "kb", BYTES.multiplier * 1024);
public static final DataUnit MB = new DataUnit("Megabyte", "mb", KB.multiplier * 1024);
public static final DataUnit GB = new DataUnit("Gigabyte", "gb", MB.multiplier * 1024);
public static final DataUnit TB = new DataUnit("Terabyte", "tb", GB.multiplier * 1024);
public static final DataUnit PB = new DataUnit("Petabyte", "pb", TB.multiplier * 1024);

private final String displayName;
private final String shortName;
private final long multiplier;

private DataUnit(String displayName, String shortName, long multiplier) {
this.displayName = displayName;
this.shortName = shortName;
this.multiplier = multiplier;
}

public String displayName() {
return displayName;
}

public String shortUnit() {
return shortName;
}

public long multiplier() {
return multiplier;
}

public long convert(long value, DataUnit sourceUnit) {
long byteSize = value / sourceUnit.multiplier;
return byteSize * this.multiplier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.hash.Hashing;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import fr.atlasworld.common.file.DataUnit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -111,6 +112,16 @@ public String checksum(HashFunction func) throws IOException {
return fileBytes.hash(func).toString();
}

/**
* Retrieve the size of the file in the specified data unit.
*
* @param unit data unit in which the file size should be returned.
* @return file size in the specified {@link DataUnit}
*/
public long size(DataUnit unit) {
return unit.convert(this.file.length(), DataUnit.BYTES);
}

/**
* Retrieve the file this reader is handling.
*
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
exports fr.atlasworld.common.compound;
exports fr.atlasworld.common.compound.json;
exports fr.atlasworld.common.exception;
exports fr.atlasworld.common.file;
exports fr.atlasworld.common.file.reader;
exports fr.atlasworld.common.logging;
exports fr.atlasworld.common.logging.stream;
Expand Down

0 comments on commit d224a65

Please sign in to comment.