[New] Added new helper DataUnit
This commit is contained in:
parent
961e93fa35
commit
54d76b1264
|
@ -0,0 +1,59 @@
|
|||
package li.strolch.utils;
|
||||
|
||||
public enum DataUnit {
|
||||
Bytes("bytes", 1),
|
||||
KiloBytes("KB", Bytes.bytesFactor * 1000),
|
||||
MegaBytes("MB", KiloBytes.bytesFactor * 1000),
|
||||
GigaBytes("GB", MegaBytes.bytesFactor * 1000),
|
||||
TeraBytes("TB", GigaBytes.bytesFactor * 1000),
|
||||
PetaBytes("PB", TeraBytes.bytesFactor * 1000);
|
||||
|
||||
private final String uom;
|
||||
private final long bytesFactor;
|
||||
|
||||
DataUnit(String uom, long bytesFactor) {
|
||||
this.uom = uom;
|
||||
this.bytesFactor = bytesFactor;
|
||||
}
|
||||
|
||||
public String getInterpretation() {
|
||||
return "DataUnit";
|
||||
}
|
||||
|
||||
public String getUom() {
|
||||
return this.uom;
|
||||
}
|
||||
|
||||
public long getBytesFactor() {
|
||||
return this.bytesFactor;
|
||||
}
|
||||
|
||||
public double convertToBytes(long value) {
|
||||
return value * (double) this.bytesFactor;
|
||||
}
|
||||
|
||||
public long roundBytesToUnit(long bytes) {
|
||||
return bytes / this.bytesFactor;
|
||||
}
|
||||
|
||||
public String humanizeBytesValue(long value) {
|
||||
double bytes = convertToBytes(value);
|
||||
|
||||
if (bytes < KiloBytes.bytesFactor)
|
||||
return String.format("%f bytes", bytes);
|
||||
|
||||
if (bytes < MegaBytes.bytesFactor)
|
||||
return String.format("%.1f KB", (bytes / (double) KiloBytes.bytesFactor));
|
||||
|
||||
if (bytes < GigaBytes.bytesFactor)
|
||||
return String.format("%.1f MB", (bytes / (double) MegaBytes.bytesFactor));
|
||||
|
||||
if (bytes < TeraBytes.bytesFactor)
|
||||
return String.format("%.1f GB", (bytes / (double) GigaBytes.bytesFactor));
|
||||
|
||||
if (bytes < PetaBytes.bytesFactor)
|
||||
return String.format("%.1f TB", (bytes / (double) GigaBytes.bytesFactor));
|
||||
|
||||
return String.format("%.1f PB", (bytes / (double) PetaBytes.bytesFactor));
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import li.strolch.utils.DataUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -459,8 +460,8 @@ public class FileHelper {
|
|||
*
|
||||
* @return the humanized form of the files size
|
||||
*/
|
||||
public final static String humanizeFileSize(File file) {
|
||||
return FileHelper.humanizeFileSize(file.length());
|
||||
public static String humanizeFileSize(File file) {
|
||||
return humanizeFileSize(file.length());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -473,17 +474,8 @@ public class FileHelper {
|
|||
* @return the humanized form of the files size
|
||||
*/
|
||||
@SuppressWarnings("nls")
|
||||
public final static String humanizeFileSize(long fileSize) {
|
||||
if (fileSize < 1024)
|
||||
return String.format("%d bytes", fileSize);
|
||||
|
||||
if (fileSize < 1048576)
|
||||
return String.format("%.1f KB", (fileSize / 1024.0d));
|
||||
|
||||
if (fileSize < 1073741824)
|
||||
return String.format("%.1f MB", (fileSize / 1048576.0d));
|
||||
|
||||
return String.format("%.1f GB", (fileSize / 1073741824.0d));
|
||||
public static String humanizeFileSize(long fileSize) {
|
||||
return DataUnit.Bytes.humanizeBytesValue(fileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue