Commit 3f8662d8 authored by Michael Ritter's avatar Michael Ritter
Browse files

Add formatter to handle file sizes from ingest summary queries

parent 1504d7dc
package edu.umiacs.ace.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* Similar to the {@link FileSizeHandler}, but for KiB/MiB/etc when
* we may not have access to the tlds
*
* Created by shake on 4/4/17.
*/
public class FileSizeFormatter {
private Unit unit;
public FileSizeFormatter(String unit) {
this.unit = Unit.fromString(unit);
}
public String format(BigDecimal decimal) {
BigDecimal result = decimal.divide(unit.divisor, 5, RoundingMode.HALF_UP);
String displayUnit = unit == Unit.B ? "" : unit.name();
return result.stripTrailingZeros().toPlainString() + " " + displayUnit;
}
public enum Unit {
B(0), KiB(1), MiB(2), GiB(3), TiB(4);
private BigDecimal divisor;
Unit(int pow) {
this.divisor = new BigDecimal(Math.pow(1024, pow));
}
private static Unit fromString(String unit) {
if (unit == null) {
return B;
}
if (unit.equalsIgnoreCase("kib")) {
return KiB;
} else if (unit.equalsIgnoreCase("mib")) {
return MiB;
} else if (unit.equalsIgnoreCase("gib")) {
return GiB;
} else if (unit.equalsIgnoreCase("tib")) {
return TiB;
}
return B;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment