Skip to content

Instantly share code, notes, and snippets.

@eaorak
Created January 16, 2014 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eaorak/8454679 to your computer and use it in GitHub Desktop.
Save eaorak/8454679 to your computer and use it in GitHub Desktop.
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