Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/main/java/org/scijava/util/UnitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

package org.scijava.util;

import java.util.Locale;

/**
* Utility methods for working with units.
Expand All @@ -41,15 +40,19 @@
*/
public final class UnitUtils {

private static final String[] BYTE_UNITS = { "B", "KiB", "MiB", "GiB",
"TiB", "PiB", "EiB", "ZiB", "YiB" };
private static final String[] BYTE_UNITS = { "B", "KiB", "MiB", "GiB", "TiB",
"PiB", "EiB", "ZiB", "YiB" };

private static final double LOG1024 = Math.log(1024);

private UnitUtils() {
// prevent instantiation of utility class
}

/**
* @return A properly formatted String representation of the given bytes, in
* the default {@link java.util.Locale} for this JVM.
*/
public static String getAbbreviatedByteLabel(final double totBytes) {
if (totBytes < 0) {
throw new IllegalArgumentException("Bytes must be non-negative");
Expand All @@ -63,9 +66,14 @@ public static String getAbbreviatedByteLabel(final double totBytes) {
// compute value from unit
final double value = totBytes / Math.pow(1024.0, pow);

// format result with 0 decimal places for bytes, or 1 for larger values
final String format = pow == 0 ? "%.0f%s" : "%.1f%s";
return String.format(Locale.US, format, value, BYTE_UNITS[pow]);
final String format = format(pow);
return String.format(format, value, BYTE_UNITS[pow]);
}

/**
* @return Format result with 0 decimal places for bytes, or 1 for larger values
*/
public static String format(final double power) {
return power == 0 ? "%.0f%s" : "%.1f%s";
}
}
75 changes: 41 additions & 34 deletions src/test/java/org/scijava/util/UnitUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.scijava.util.UnitUtils;

/**
* Tests {@link UnitUtils}.
Expand All @@ -45,40 +44,48 @@ public class UnitUtilsTest {

@Test
public void testGetAbbreviatedByteLabel() {
assertEquals("0B", UnitUtils.getAbbreviatedByteLabel(0));
assertEquals("1B", UnitUtils.getAbbreviatedByteLabel(1));
assertEquals("123B", UnitUtils.getAbbreviatedByteLabel(123));
assertEquals(local(0, "B"), UnitUtils.getAbbreviatedByteLabel(0));
assertEquals(local(1, "B"), UnitUtils.getAbbreviatedByteLabel(1));
assertEquals(local(123, "B"), UnitUtils.getAbbreviatedByteLabel(123));

assertEquals("10B", UnitUtils.getAbbreviatedByteLabel(1e1));
assertEquals("100B", UnitUtils.getAbbreviatedByteLabel(1e2));
assertEquals("1000B", UnitUtils.getAbbreviatedByteLabel(1e3));
assertEquals("9.8KiB", UnitUtils.getAbbreviatedByteLabel(1e4));
assertEquals("97.7KiB", UnitUtils.getAbbreviatedByteLabel(1e5));
assertEquals("976.6KiB", UnitUtils.getAbbreviatedByteLabel(1e6));
assertEquals("9.5MiB", UnitUtils.getAbbreviatedByteLabel(1e7));
assertEquals("95.4MiB", UnitUtils.getAbbreviatedByteLabel(1e8));
assertEquals("953.7MiB", UnitUtils.getAbbreviatedByteLabel(1e9));
assertEquals("9.3GiB", UnitUtils.getAbbreviatedByteLabel(1e10));
assertEquals("93.1GiB", UnitUtils.getAbbreviatedByteLabel(1e11));
assertEquals("931.3GiB", UnitUtils.getAbbreviatedByteLabel(1e12));
assertEquals("9.1TiB", UnitUtils.getAbbreviatedByteLabel(1e13));
assertEquals("90.9TiB", UnitUtils.getAbbreviatedByteLabel(1e14));
assertEquals("909.5TiB", UnitUtils.getAbbreviatedByteLabel(1e15));
assertEquals("8.9PiB", UnitUtils.getAbbreviatedByteLabel(1e16));
assertEquals("88.8PiB", UnitUtils.getAbbreviatedByteLabel(1e17));
assertEquals("888.2PiB", UnitUtils.getAbbreviatedByteLabel(1e18));
assertEquals("8.7EiB", UnitUtils.getAbbreviatedByteLabel(1e19));
assertEquals("86.7EiB", UnitUtils.getAbbreviatedByteLabel(1e20));
assertEquals("867.4EiB", UnitUtils.getAbbreviatedByteLabel(1e21));
assertEquals("8.5ZiB", UnitUtils.getAbbreviatedByteLabel(1e22));
assertEquals("84.7ZiB", UnitUtils.getAbbreviatedByteLabel(1e23));
assertEquals("847.0ZiB", UnitUtils.getAbbreviatedByteLabel(1e24));
assertEquals("8.3YiB", UnitUtils.getAbbreviatedByteLabel(1e25));
assertEquals("82.7YiB", UnitUtils.getAbbreviatedByteLabel(1e26));
assertEquals("827.2YiB", UnitUtils.getAbbreviatedByteLabel(1e27));
assertEquals("8271.8YiB", UnitUtils.getAbbreviatedByteLabel(1e28));
assertEquals("82718.1YiB", UnitUtils.getAbbreviatedByteLabel(1e29));
assertEquals("827180.6YiB", UnitUtils.getAbbreviatedByteLabel(1e30));
assertEquals(local(10, "B"), UnitUtils.getAbbreviatedByteLabel(1e1));
assertEquals(local(100, "B"), UnitUtils.getAbbreviatedByteLabel(1e2));
assertEquals(local(1000, "B"), UnitUtils.getAbbreviatedByteLabel(1e3));
assertEquals(local(9.8, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e4));
assertEquals(local(97.7, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e5));
assertEquals(local(976.6, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e6));
assertEquals(local(9.5, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e7));
assertEquals(local(95.4, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e8));
assertEquals(local(953.7, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e9));
assertEquals(local(9.3, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e10));
assertEquals(local(93.1, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e11));
assertEquals(local(931.3, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e12));
assertEquals(local(9.1, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e13));
assertEquals(local(90.9, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e14));
assertEquals(local(909.5, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e15));
assertEquals(local(8.9, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e16));
assertEquals(local(88.8, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e17));
assertEquals(local(888.2, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e18));
assertEquals(local(8.7, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e19));
assertEquals(local(86.7, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e20));
assertEquals(local(867.4, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e21));
assertEquals(local(8.5, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e22));
assertEquals(local(84.7, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e23));
assertEquals(local(847.0, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e24));
assertEquals(local(8.3, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e25));
assertEquals(local(82.7, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e26));
assertEquals(local(827.2, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e27));
assertEquals(local(8271.8, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e28));
assertEquals(local(82718.1, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e29));
assertEquals(local(827180.6, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e30));
}

/**
* Helper method to ensure the strings tested here match the
* {@link java.util.Locale} of the current JVM.
*/
private String local(final double value, final String unit) {
final String format = UnitUtils.format(unit.equals("B") ? 0 : 1);
return String.format(format, value, unit);
}
}