Move size formatting code to StringUtil

This commit is contained in:
0xboobface 2018-11-27 14:11:52 +01:00
parent 8826de38b2
commit 55b219d271
2 changed files with 20 additions and 14 deletions

View File

@ -160,20 +160,7 @@ public class RecordingsTab extends Tab implements TabSelectionListener {
if(empty || sizeInByte == null) {
setText(null);
} else {
DecimalFormat df = new DecimalFormat("0.00");
String unit = "Bytes";
double size = sizeInByte.doubleValue();
if(size > 1024.0 * 1024 * 1024) {
size = size / 1024.0 / 1024 / 1024;
unit = "GiB";
} else if(size > 1024.0 * 1024) {
size = size / 1024.0 / 1024;
unit = "MiB";
} else if(size > 1024.0) {
size = size / 1024.0;
unit = "KiB";
}
setText(df.format(size) + ' ' + unit);
setText(StringUtil.formatSize(sizeInByte));
}
}
};

View File

@ -1,5 +1,7 @@
package ctbrec;
import java.text.DecimalFormat;
public class StringUtil {
public static boolean isBlank(String s) {
return s == null || s.trim().isEmpty();
@ -8,4 +10,21 @@ public class StringUtil {
public static boolean isNotBlank(String s) {
return !isBlank(s);
}
public static String formatSize(Number sizeInByte) {
DecimalFormat df = new DecimalFormat("0.00");
String unit = "Bytes";
double size = sizeInByte.doubleValue();
if(size > 1024.0 * 1024 * 1024) {
size = size / 1024.0 / 1024 / 1024;
unit = "GiB";
} else if(size > 1024.0 * 1024) {
size = size / 1024.0 / 1024;
unit = "MiB";
} else if(size > 1024.0) {
size = size / 1024.0;
unit = "KiB";
}
return df.format(size) + ' ' + unit;
}
}