-
-
Save eaorak/8454679 to your computer and use it in GitHub Desktop.
Useful enum for file size calculations.
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
/** | |
* Useful for disk file size calculations. [eaorak] | |
*/ | |
public enum Size { | |
BY, // Bytes | |
KB, // Kilo | |
MB, // Mega | |
GB, // Giga | |
TB, // Tera | |
PB; // Peta | |
private long size; | |
private Size() { | |
this.size = (long) Math.pow(1024, this.ordinal()); | |
} | |
public long size() { | |
return this.size; | |
} | |
public long asBytes(int amount) { | |
return this.as(Size.KB, amount); | |
} | |
public long asMb(int amount) { | |
return this.as(Size.MB, amount); | |
} | |
public long asGb(int amount) { | |
return this.as(Size.GB, amount); | |
} | |
public long asTb(int amount) { | |
return this.as(Size.TB, amount); | |
} | |
private long as(Size size, int amount) { | |
return (this.size * amount) / size.size; | |
} | |
public static void main(String[] args) { | |
for (Size s : Size.values()) { | |
System.out.println(s.name() + ":" + s.size); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment