Skip to content

Instantly share code, notes, and snippets.

@eaorak
Created Jan 16, 2014
Embed
What would you like to do?
Useful enum for file size calculations.
/**
* 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