Commit 7e6f2589 authored by Gregory.Shrago's avatar Gregory.Shrago Committed by Vasily Chernov
Browse files

make 1KB=1024B; drop weeks & make 1mo=30d

(cherry picked from commit 98ed1566)
(cherry picked from commit 9335cf06)
parent ed250b3c
Showing with 90 additions and 51 deletions
+90 -51
......@@ -1450,15 +1450,20 @@ public class StringUtil extends StringUtilRt {
@NotNull
@Contract(pure = true)
public static String[] surround(@NotNull String[] strings1, String prefix, String suffix) {
String[] result = ArrayUtil.newStringArray(strings1.length);
public static String[] surround(@NotNull String[] strings, @Nullable String prefix, @Nullable String suffix) {
String[] result = ArrayUtil.newStringArray(strings.length);
for (int i = 0; i < result.length; i++) {
result[i] = prefix + strings1[i] + suffix;
result[i] = surround(strings[i], prefix, suffix);
}
return result;
}
@NotNull
public static String surround(@NotNull String string, @Nullable String prefix, @Nullable String suffix) {
if (prefix == null && suffix == null) return string;
return notNullize(prefix) + string + notNullize(suffix);
}
@NotNull
@Contract(pure = true)
public static <T> String join(@NotNull T[] items, @NotNull Function<T, String> f, @NotNull @NonNls String separator) {
......@@ -1584,6 +1589,20 @@ public class StringUtil extends StringUtilRt {
return text;
}
@NotNull
@Contract(pure = true)
public static String formatNumber(long number) {
return formatNumber(number, "");
}
@NotNull
@Contract(pure = true)
public static String formatNumber(long number, @NotNull String unitSeparator) {
return formatValue(number, null,
unitSeparator, new String[]{"", "K", "M", "G", "T", "P", "E"},
new long[]{1, 1000, 1000, 1000, 1000, 1000, 1000});
}
/**
* Formats the specified file size as a string.
*
......@@ -1594,71 +1613,71 @@ public class StringUtil extends StringUtilRt {
@NotNull
@Contract(pure = true)
public static String formatFileSize(long fileSize) {
return formatFileSize(fileSize, null);
return formatFileSize(fileSize, "");
}
/**
* Formats the specified file size as a string.
*
* @param fileSize the size to format.
* @param spaceBeforeUnits space to be used between counts and measurement units
* @return the size formatted as a string.
* @since 5.0.1
*/
@NotNull
@Contract(pure = true)
public static String formatFileSize(long fileSize, final String spaceBeforeUnits) {
public static String formatFileSize(long fileSize, @NotNull String unitSeparator) {
return formatValue(fileSize, null,
new String[]{"B", "K", "M", "G", "T", "P", "E"},
new long[]{1000, 1000, 1000, 1000, 1000, 1000}, spaceBeforeUnits);
unitSeparator, new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB"},
new long[]{1, 1024, 1024, 1024, 1024, 1024, 1024});
}
@NotNull
@Contract(pure = true)
public static String formatDuration(long duration) {
return formatDuration(duration, null);
return formatDuration(duration, "");
}
@NotNull
@Contract(pure = true)
public static String formatDuration(long duration, final String spaceBeforeUnits) {
return formatValue(duration, " ",
new String[]{"ms", "s", "m", "h", "d", "w", "mo", "yr", "c", "ml", "ep"},
new long[]{1000, 60, 60, 24, 7, 4, 12, 100, 10, 10000}, spaceBeforeUnits);
public static String formatDuration(long duration, @NotNull String unitSeparator) {
return formatValue(duration, " ", unitSeparator,
new String[]{"ms", "s", "m", "h", "d", "mo", "yr", "c", "ml", "ep"},
new long[]{1, 1000, 60, 60, 24, 30, 12, 100, 10, 10000});
}
@NotNull
private static String formatValue(long value, String partSeparator, String[] units, long[] multipliers, final String spaceBeforeUnits) {
private static String formatValue(long value,
@Nullable String partSeparator, @NotNull String unitSeparator,
@NotNull String[] units, @NotNull long[] multipliers) {
LOG.assertTrue(units.length == multipliers.length);
StringBuilder sb = new StringBuilder();
long count = value;
long remainder = 0;
int i = 0;
for (; i < units.length; i++) {
long multiplier = i < multipliers.length ? multipliers[i] : -1;
if (multiplier == -1 || count < multiplier) break;
int i = 1;
for (; i < units.length && count > 0; i++) {
long multiplier = multipliers[i];
if (count < multiplier) break;
remainder = count % multiplier;
count /= multiplier;
if (partSeparator != null && (remainder != 0 || sb.length() > 0)) {
sb.insert(0, units[i]);
if (spaceBeforeUnits != null) {
sb.insert(0, spaceBeforeUnits);
if (!units[i - 1].isEmpty()) {
sb.insert(0, units[i - 1]);
sb.insert(0, unitSeparator);
}
sb.insert(0, remainder).insert(0, partSeparator);
}
else {
remainder = Math.round(remainder * 100 / (double)multiplier);
count += remainder / 100;
remainder %= 100;
}
}
if (partSeparator != null || remainder == 0) {
sb.insert(0, units[i]);
if (spaceBeforeUnits != null) {
sb.insert(0, spaceBeforeUnits);
if (!units[i - 1].isEmpty()) {
sb.insert(0, units[i - 1]);
sb.insert(0, unitSeparator);
}
sb.insert(0, count);
}
else if (remainder > 0) {
sb.append(String.format(Locale.US, "%.2f", count + (double)remainder / multipliers[i - 1]));
if (spaceBeforeUnits != null) {
sb.append(spaceBeforeUnits);
sb.append(count).append(".").append(remainder / 10 == 0 ? "0" : "").append(remainder);
if (!units[i - 1].isEmpty()) {
sb.append(unitSeparator);
sb.append(units[i - 1]);
}
sb.append(units[i]);
}
return sb.toString();
}
......
......@@ -489,28 +489,46 @@ public class StringUtilTest {
@Test
public void testFormatFileSize() {
assertEquals("0B", StringUtil.formatFileSize(0));
assertEquals("1B", StringUtil.formatFileSize(1));
assertEquals("2.15G", StringUtil.formatFileSize(Integer.MAX_VALUE));
assertEquals("9.22E", StringUtil.formatFileSize(Long.MAX_VALUE));
assertEquals("0 B", StringUtil.formatFileSize(0, " "));
assertEquals("1 B", StringUtil.formatFileSize(1, " "));
assertEquals("2 GB", StringUtil.formatFileSize(Integer.MAX_VALUE, " "));
assertEquals("8 EB", StringUtil.formatFileSize(Long.MAX_VALUE, " "));
assertEquals("60.10K", StringUtil.formatFileSize(60100));
assertEquals("58.69 KB", StringUtil.formatFileSize(60100, " "));
assertEquals("1.23K", StringUtil.formatFileSize(1234));
assertEquals("12.35K", StringUtil.formatFileSize(12345));
assertEquals("123.46K", StringUtil.formatFileSize(123456));
assertEquals("1.23M", StringUtil.formatFileSize(1234567));
assertEquals("12.35M", StringUtil.formatFileSize(12345678));
assertEquals("123.46M", StringUtil.formatFileSize(123456789));
assertEquals("1.23G", StringUtil.formatFileSize(1234567890));
assertEquals("1.21 KB", StringUtil.formatFileSize(1234, " "));
assertEquals("12.06 KB", StringUtil.formatFileSize(12345, " "));
assertEquals("120.56 KB", StringUtil.formatFileSize(123456, " "));
assertEquals("1.18 MB", StringUtil.formatFileSize(1234567, " "));
assertEquals("11.77 MB", StringUtil.formatFileSize(12345678, " "));
assertEquals("117.74 MB", StringUtil.formatFileSize(123456789, " "));
assertEquals("1.15 GB", StringUtil.formatFileSize(1234567890, " "));
}
@Test
public void testFormatNumber() {
assertEquals("0", StringUtil.formatNumber(0, " "));
assertEquals("1", StringUtil.formatNumber(1, " "));
assertEquals("2.15 G", StringUtil.formatNumber(Integer.MAX_VALUE, " "));
assertEquals("9.22 E", StringUtil.formatNumber(Long.MAX_VALUE, " "));
assertEquals("60.10 K", StringUtil.formatNumber(60100, " "));
assertEquals("1.23 K", StringUtil.formatNumber(1234, " "));
assertEquals("12.35 K", StringUtil.formatNumber(12345, " "));
assertEquals("123.46 K", StringUtil.formatNumber(123456, " "));
assertEquals("1.23 M", StringUtil.formatNumber(1234567, " "));
assertEquals("12.35 M", StringUtil.formatNumber(12345678, " "));
assertEquals("123.46 M", StringUtil.formatNumber(123456789, " "));
assertEquals("1.23 G", StringUtil.formatNumber(1234567890, " "));
}
@Test
public void testFormatDuration() {
assertEquals("0ms", StringUtil.formatDuration(0));
assertEquals("1ms", StringUtil.formatDuration(1));
assertEquals("3w 3d 20h 31m 23s 647ms", StringUtil.formatDuration(Integer.MAX_VALUE));
assertEquals("31ep 7714ml 2c 59yr 5mo 0w 3d 7h 12m 55s 807ms", StringUtil.formatDuration(Long.MAX_VALUE));
assertEquals("24d 20h 31m 23s 647ms", StringUtil.formatDuration(Integer.MAX_VALUE));
assertEquals("29ep 6533ml 3c 8yr 9mo 17d 7h 12m 55s 807ms", StringUtil.formatDuration(Long.MAX_VALUE));
assertEquals("1m 0s 100ms", StringUtil.formatDuration(60100));
......@@ -520,7 +538,9 @@ public class StringUtilTest {
assertEquals("20m 34s 567ms", StringUtil.formatDuration(1234567));
assertEquals("3h 25m 45s 678ms", StringUtil.formatDuration(12345678));
assertEquals("1d 10h 17m 36s 789ms", StringUtil.formatDuration(123456789));
assertEquals("2w 0d 6h 56m 7s 890ms", StringUtil.formatDuration(1234567890));
assertEquals("14d 6h 56m 7s 890ms", StringUtil.formatDuration(1234567890));
assertEquals("1yr 1mo 1d 1h 1m 1s 1ms", StringUtil.formatDuration(33786061001L));
}
@Test
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment