improve MemoryUtil

This commit is contained in:
Marc Baloup 2020-03-29 21:22:31 +02:00
parent cda196e377
commit b38e0c7b60
2 changed files with 59 additions and 17 deletions

View File

@ -24,7 +24,7 @@ public class MinecraftWebUtil {
if (c == code_prefix && (i<ligne.length()-1)) { if (c == code_prefix && (i<ligne.length()-1)) {
i++; i++;
c = ligne.charAt(i); c = ligne.charAt(i);
if (color_char.contains(new Character(c).toString().toLowerCase())) { if (color_char.contains(new String(new char[] {Character.toLowerCase(c)}))) {
if (bold) { if (bold) {
builder += "</span>"; builder += "</span>";
bold = false; bold = false;

View File

@ -3,30 +3,72 @@ package fr.pandacube.util.measurement;
import java.text.DecimalFormat; import java.text.DecimalFormat;
public class MemoryUtil { public class MemoryUtil {
public enum MemoryUnit {
B(1, 1, null),
KB(1024, 1000, "k"),
MB(1024 * 1024, 1000_000, "M"),
GB(1024 * 1024 * 1024, 1000_000_000, "G");
final long valueTrad;
final long valueSI;
final String unitMultiplier;
public long toUnitRound(long byteCount, boolean si) {
return byteCount / value(si);
}
public double toUnit(long byteCount, boolean si) {
return byteCount / (double)value(si);
}
public long value(boolean si) {
return si ? valueSI : valueTrad;
}
public String unit(boolean si) {
return unitMultiplier == null ? "o" : (unitMultiplier + (si ? "o" : "io"));
}
private MemoryUnit(long vTrad, long vSI, String uMult) {
valueTrad = vTrad;
valueSI = vSI;
unitMultiplier = uMult;
}
}
private static final DecimalFormat format = new DecimalFormat("#####0.00"); private static final DecimalFormat format = new DecimalFormat("#####0.00");
public static String humanReadableSize(long octet, boolean si) { public static String humanReadableSize(long octet, MemoryUnit roundTo, boolean si) {
boolean neg = octet < 0; boolean neg = octet < 0;
double size = Math.abs(octet); long size = Math.abs(octet);
int diveBy = si ? 1000 : 1024; MemoryUnit unit = roundTo;
for (int ui = MemoryUnit.values().length - 1; ui >= 0; ui--) {
if (size < diveBy) return (neg ? "-" : "") + size + "o"; MemoryUnit u = MemoryUnit.values()[ui];
size /= diveBy; if (u == roundTo)
if (size < diveBy) return (neg ? "-" : "") + format.format(size) + (si ? "ko" : "kio"); break;
size /= diveBy; if (size < u.value(si))
if (size < diveBy) return (neg ? "-" : "") + format.format(size) + (si ? "Mo" : "Mio"); continue;
size /= diveBy; unit = u;
if (size < diveBy) return (neg ? "-" : "") + format.format(size) + (si ? "Go" : "Gio"); break;
size /= diveBy; }
return (neg ? "-" : "") + format.format(size) + (si ? "To" : "Tio"); String dispValue;
if (unit == roundTo) {
dispValue = ""+unit.toUnitRound(size, si);
}
else {
dispValue = format.format(unit.toUnit(size, si));
}
return (neg ? "-" : "") + dispValue + unit.unit(si);
} }
public static String humanReadableSize(long octet) { public static String humanReadableSize(long octet) {
return humanReadableSize(octet, false); return humanReadableSize(octet, MemoryUnit.B, false);
} }
} }