Skip to content

Commit be8e1a5

Browse files
committed
Merge pull request #170 from scijava/locale-fixes
Removes US Locale bias from UnitUtils and UnitUtilsTest.
2 parents f5708d5 + 4c2429b commit be8e1a5

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

src/main/java/org/scijava/util/UnitUtils.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
package org.scijava.util;
3333

34-
import java.util.Locale;
3534

3635
/**
3736
* Utility methods for working with units.
@@ -41,15 +40,19 @@
4140
*/
4241
public final class UnitUtils {
4342

44-
private static final String[] BYTE_UNITS = { "B", "KiB", "MiB", "GiB",
45-
"TiB", "PiB", "EiB", "ZiB", "YiB" };
43+
private static final String[] BYTE_UNITS = { "B", "KiB", "MiB", "GiB", "TiB",
44+
"PiB", "EiB", "ZiB", "YiB" };
4645

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

4948
private UnitUtils() {
5049
// prevent instantiation of utility class
5150
}
5251

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

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

73+
/**
74+
* @return Format result with 0 decimal places for bytes, or 1 for larger values
75+
*/
76+
public static String format(final double power) {
77+
return power == 0 ? "%.0f%s" : "%.1f%s";
78+
}
7179
}

src/test/java/org/scijava/util/UnitUtilsTest.java

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static org.junit.Assert.assertEquals;
3535

3636
import org.junit.Test;
37-
import org.scijava.util.UnitUtils;
3837

3938
/**
4039
* Tests {@link UnitUtils}.
@@ -45,40 +44,48 @@ public class UnitUtilsTest {
4544

4645
@Test
4746
public void testGetAbbreviatedByteLabel() {
48-
assertEquals("0B", UnitUtils.getAbbreviatedByteLabel(0));
49-
assertEquals("1B", UnitUtils.getAbbreviatedByteLabel(1));
50-
assertEquals("123B", UnitUtils.getAbbreviatedByteLabel(123));
47+
assertEquals(local(0, "B"), UnitUtils.getAbbreviatedByteLabel(0));
48+
assertEquals(local(1, "B"), UnitUtils.getAbbreviatedByteLabel(1));
49+
assertEquals(local(123, "B"), UnitUtils.getAbbreviatedByteLabel(123));
5150

52-
assertEquals("10B", UnitUtils.getAbbreviatedByteLabel(1e1));
53-
assertEquals("100B", UnitUtils.getAbbreviatedByteLabel(1e2));
54-
assertEquals("1000B", UnitUtils.getAbbreviatedByteLabel(1e3));
55-
assertEquals("9.8KiB", UnitUtils.getAbbreviatedByteLabel(1e4));
56-
assertEquals("97.7KiB", UnitUtils.getAbbreviatedByteLabel(1e5));
57-
assertEquals("976.6KiB", UnitUtils.getAbbreviatedByteLabel(1e6));
58-
assertEquals("9.5MiB", UnitUtils.getAbbreviatedByteLabel(1e7));
59-
assertEquals("95.4MiB", UnitUtils.getAbbreviatedByteLabel(1e8));
60-
assertEquals("953.7MiB", UnitUtils.getAbbreviatedByteLabel(1e9));
61-
assertEquals("9.3GiB", UnitUtils.getAbbreviatedByteLabel(1e10));
62-
assertEquals("93.1GiB", UnitUtils.getAbbreviatedByteLabel(1e11));
63-
assertEquals("931.3GiB", UnitUtils.getAbbreviatedByteLabel(1e12));
64-
assertEquals("9.1TiB", UnitUtils.getAbbreviatedByteLabel(1e13));
65-
assertEquals("90.9TiB", UnitUtils.getAbbreviatedByteLabel(1e14));
66-
assertEquals("909.5TiB", UnitUtils.getAbbreviatedByteLabel(1e15));
67-
assertEquals("8.9PiB", UnitUtils.getAbbreviatedByteLabel(1e16));
68-
assertEquals("88.8PiB", UnitUtils.getAbbreviatedByteLabel(1e17));
69-
assertEquals("888.2PiB", UnitUtils.getAbbreviatedByteLabel(1e18));
70-
assertEquals("8.7EiB", UnitUtils.getAbbreviatedByteLabel(1e19));
71-
assertEquals("86.7EiB", UnitUtils.getAbbreviatedByteLabel(1e20));
72-
assertEquals("867.4EiB", UnitUtils.getAbbreviatedByteLabel(1e21));
73-
assertEquals("8.5ZiB", UnitUtils.getAbbreviatedByteLabel(1e22));
74-
assertEquals("84.7ZiB", UnitUtils.getAbbreviatedByteLabel(1e23));
75-
assertEquals("847.0ZiB", UnitUtils.getAbbreviatedByteLabel(1e24));
76-
assertEquals("8.3YiB", UnitUtils.getAbbreviatedByteLabel(1e25));
77-
assertEquals("82.7YiB", UnitUtils.getAbbreviatedByteLabel(1e26));
78-
assertEquals("827.2YiB", UnitUtils.getAbbreviatedByteLabel(1e27));
79-
assertEquals("8271.8YiB", UnitUtils.getAbbreviatedByteLabel(1e28));
80-
assertEquals("82718.1YiB", UnitUtils.getAbbreviatedByteLabel(1e29));
81-
assertEquals("827180.6YiB", UnitUtils.getAbbreviatedByteLabel(1e30));
51+
assertEquals(local(10, "B"), UnitUtils.getAbbreviatedByteLabel(1e1));
52+
assertEquals(local(100, "B"), UnitUtils.getAbbreviatedByteLabel(1e2));
53+
assertEquals(local(1000, "B"), UnitUtils.getAbbreviatedByteLabel(1e3));
54+
assertEquals(local(9.8, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e4));
55+
assertEquals(local(97.7, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e5));
56+
assertEquals(local(976.6, "KiB"), UnitUtils.getAbbreviatedByteLabel(1e6));
57+
assertEquals(local(9.5, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e7));
58+
assertEquals(local(95.4, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e8));
59+
assertEquals(local(953.7, "MiB"), UnitUtils.getAbbreviatedByteLabel(1e9));
60+
assertEquals(local(9.3, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e10));
61+
assertEquals(local(93.1, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e11));
62+
assertEquals(local(931.3, "GiB"), UnitUtils.getAbbreviatedByteLabel(1e12));
63+
assertEquals(local(9.1, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e13));
64+
assertEquals(local(90.9, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e14));
65+
assertEquals(local(909.5, "TiB"), UnitUtils.getAbbreviatedByteLabel(1e15));
66+
assertEquals(local(8.9, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e16));
67+
assertEquals(local(88.8, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e17));
68+
assertEquals(local(888.2, "PiB"), UnitUtils.getAbbreviatedByteLabel(1e18));
69+
assertEquals(local(8.7, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e19));
70+
assertEquals(local(86.7, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e20));
71+
assertEquals(local(867.4, "EiB"), UnitUtils.getAbbreviatedByteLabel(1e21));
72+
assertEquals(local(8.5, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e22));
73+
assertEquals(local(84.7, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e23));
74+
assertEquals(local(847.0, "ZiB"), UnitUtils.getAbbreviatedByteLabel(1e24));
75+
assertEquals(local(8.3, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e25));
76+
assertEquals(local(82.7, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e26));
77+
assertEquals(local(827.2, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e27));
78+
assertEquals(local(8271.8, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e28));
79+
assertEquals(local(82718.1, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e29));
80+
assertEquals(local(827180.6, "YiB"), UnitUtils.getAbbreviatedByteLabel(1e30));
8281
}
8382

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

0 commit comments

Comments
 (0)