Commit 9213e6d4 authored by Greg Thomas's avatar Greg Thomas

Improve the formatting of the cache expiry time

parent e06143ab
......@@ -835,6 +835,8 @@ global.main=Main
global.continue=Continue
global.none=None
global.refresh=Refresh
global.millisecond=ms
global.milliseconds=ms
global.second=second
global.seconds=seconds
global.minute=minute
......
......@@ -841,6 +841,54 @@ public final class StringUtils {
return zeroPadString(Long.toString(date.getTime()), 15);
}
/**
* Returns a textual representation for the time that has elapsed.
*
* @param delta the elapsed time in milliseconds
* @return textual representation for the time that has elapsed.
*/
public static String getFullElapsedTime(final long delta) {
if (delta < JiveConstants.SECOND) {
return String.format("%d %s", delta, delta == 1 ? LocaleUtils.getLocalizedString("global.millisecond") : LocaleUtils.getLocalizedString("global.milliseconds"));
} else if (delta < JiveConstants.MINUTE) {
final long millis = delta % JiveConstants.SECOND;
final long seconds = delta / JiveConstants.SECOND;
final String secondsString = String.format("%d %s", seconds, seconds == 1 ? LocaleUtils.getLocalizedString("global.second") : LocaleUtils.getLocalizedString("global.seconds"));
if (millis > 0) {
return secondsString + ", " + getFullElapsedTime(millis);
} else {
return secondsString;
}
} else if (delta < JiveConstants.HOUR) {
final long millis = delta % JiveConstants.MINUTE;
final long minutes = delta / JiveConstants.MINUTE;
final String minutesString = String.format("%d %s", minutes, minutes == 1 ? LocaleUtils.getLocalizedString("global.minute") : LocaleUtils.getLocalizedString("global.minutes"));
if (millis > 0) {
return minutesString + ", " + getFullElapsedTime(millis);
} else {
return minutesString;
}
} else if (delta < JiveConstants.DAY) {
final long millis = delta % JiveConstants.HOUR;
final long hours = delta / JiveConstants.HOUR;
final String daysString = String.format("%d %s", hours, hours == 1 ? LocaleUtils.getLocalizedString("global.hour") : LocaleUtils.getLocalizedString("global.hours"));
if (millis > 0) {
return daysString + ", " + getFullElapsedTime(millis);
} else {
return daysString;
}
} else {
final long millis = delta % JiveConstants.DAY;
final long days = delta / JiveConstants.DAY;
final String daysString = String.format("%d %s", days, days == 1 ? LocaleUtils.getLocalizedString("global.day") : LocaleUtils.getLocalizedString("global.days"));
if (millis > 0) {
return daysString + ", " + getFullElapsedTime(millis);
} else {
return daysString;
}
}
}
/**
* Returns a textual representation for the time that has elapsed.
*
......@@ -1105,4 +1153,4 @@ public final class StringUtils {
public static String getString(byte[] input) {
return new String(input, StandardCharsets.UTF_8);
}
}
\ No newline at end of file
}
package org.jivesoftware.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import org.junit.Test;
......@@ -55,4 +57,39 @@ public class StringUtilsTest {
//assertEquals("Unexpected cause: " + iae.getMessage(), expectedCause, iae.getMessage());
}
}
@Test
public void testElapsedTimeInMilliseconds() throws Exception {
assertThat(StringUtils.getFullElapsedTime(0), is("0 ms"));
assertThat(StringUtils.getFullElapsedTime(1), is("1 ms"));
assertThat(StringUtils.getFullElapsedTime(250), is("250 ms"));
}
@Test
public void testElapsedTimeInSeconds() throws Exception {
assertThat(StringUtils.getFullElapsedTime(JiveConstants.SECOND), is("1 second"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.SECOND + 1), is("1 second, 1 ms"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.SECOND * 30 + 30), is("30 seconds, 30 ms"));
}
@Test
public void testElapsedTimeInMinutes() throws Exception {
assertThat(StringUtils.getFullElapsedTime(JiveConstants.MINUTE), is("1 minute"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.MINUTE + JiveConstants.SECOND + 1), is("1 minute, 1 second, 1 ms"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.MINUTE * 30 + JiveConstants.SECOND * 30), is("30 minutes, 30 seconds"));
}
@Test
public void testElapsedTimeInHours() throws Exception {
assertThat(StringUtils.getFullElapsedTime(JiveConstants.HOUR), is("1 hour"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.HOUR + JiveConstants.MINUTE + JiveConstants.SECOND + 1), is("1 hour, 1 minute, 1 second, 1 ms"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.HOUR * 10 + JiveConstants.MINUTE * 30), is("10 hours, 30 minutes"));
}
@Test
public void testElapsedTimeInDays() throws Exception {
assertThat(StringUtils.getFullElapsedTime(JiveConstants.DAY), is("1 day"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.DAY + JiveConstants.HOUR + JiveConstants.MINUTE + JiveConstants.SECOND + 1), is("1 day, 1 hour, 1 minute, 1 second, 1 ms"));
assertThat(StringUtils.getFullElapsedTime(JiveConstants.DAY * 10 + JiveConstants.HOUR * 10), is("10 days, 10 hours"));
}
}
......@@ -220,7 +220,7 @@
</td>
<td class="c2">
<% if (cache.getMaxLifetime() != -1) { %>
<%= cache.getMaxLifetime() %> ms
<%= StringUtils.getFullElapsedTime(cache.getMaxLifetime()) %>
<% } else { %>
<fmt:message key="global.unlimited" />
<% } %>
......
Markdown is supported
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