-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the DataUnit class, allows to easily convert DataType (kb, mb, …
…gb, ects..).
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
common/src/main/java/fr/atlasworld/common/file/DataUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters