Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several optimizations in the method formatDurationWords #1018

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -179,48 +179,63 @@ public static String formatDurationWords(
// there are a series of tweaks and special cases that require
// trickery to replicate.
String duration = formatDuration(durationMillis, "d' days 'H' hours 'm' minutes 's' seconds'");
boolean hasDays = true;
boolean hasHours = true;
boolean hasMinutes = true;
boolean hasSeconds = true;
if (suppressLeadingZeroElements) {
// this is a temporary marker on the front. Like ^ in regexp.
duration = " " + duration;
String tmp = StringUtils.replaceOnce(duration, " 0 days", StringUtils.EMPTY);
String tmp = StringUtils.removeStart(duration, "0 days ");
if (tmp.length() != duration.length()) {
hasDays = false;
duration = tmp;
tmp = StringUtils.replaceOnce(duration, " 0 hours", StringUtils.EMPTY);
tmp = StringUtils.removeStart(duration, "0 hours ");
if (tmp.length() != duration.length()) {
hasHours = false;
duration = tmp;
tmp = StringUtils.replaceOnce(duration, " 0 minutes", StringUtils.EMPTY);
duration = tmp;
tmp = StringUtils.removeStart(duration, "0 minutes ");
if (tmp.length() != duration.length()) {
duration = StringUtils.replaceOnce(tmp, " 0 seconds", StringUtils.EMPTY);
hasMinutes = false;
duration = tmp;
}
}
}
if (!duration.isEmpty()) {
// strip the space off again
duration = duration.substring(1);
}
}
if (suppressTrailingZeroElements) {
String tmp = StringUtils.replaceOnce(duration, " 0 seconds", StringUtils.EMPTY);
String tmp = StringUtils.removeEnd(duration, " 0 seconds");
if (tmp.length() != duration.length()) {
hasSeconds = false;
duration = tmp;
tmp = StringUtils.replaceOnce(duration, " 0 minutes", StringUtils.EMPTY);
if (tmp.length() != duration.length()) {
duration = tmp;
tmp = StringUtils.replaceOnce(duration, " 0 hours", StringUtils.EMPTY);
if (hasMinutes) {
tmp = StringUtils.removeEnd(duration, " 0 minutes");
if (tmp.length() != duration.length()) {
duration = StringUtils.replaceOnce(tmp, " 0 days", StringUtils.EMPTY);
hasMinutes = false;
duration = tmp;
if (hasHours) {
tmp = StringUtils.removeEnd(duration, " 0 hours");
if (tmp.length() != duration.length()) {
hasHours = false;
duration = tmp;
}
}
}
}
}
}
// handle plurals
duration = " " + duration;
duration = StringUtils.replaceOnce(duration, " 1 seconds", " 1 second");
duration = StringUtils.replaceOnce(duration, " 1 minutes", " 1 minute");
duration = StringUtils.replaceOnce(duration, " 1 hours", " 1 hour");
duration = StringUtils.replaceOnce(duration, " 1 days", " 1 day");
return duration.trim();
if (hasSeconds) {
duration = StringUtils.replaceOnce(duration, " 1 seconds", " 1 second");
}
if (hasMinutes) {
duration = StringUtils.replaceOnce(duration, " 1 minutes", " 1 minute");
}
if (hasHours) {
duration = StringUtils.replaceOnce(duration, " 1 hours", " 1 hour");
}
if (hasDays) {
duration = StringUtils.replaceOnce(duration, " 1 days", " 1 day");
}
return duration.substring(1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ public void testFormatDurationPluralWords() {
public void testFormatDurationWords() {
String text;

text = DurationFormatUtils.formatDurationWords(0, true, false);
assertEquals("0 seconds", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, true, false);
assertEquals("50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, true, false);
Expand All @@ -344,6 +346,8 @@ public void testFormatDurationWords() {
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, false);
assertEquals("1 day 0 hours 0 minutes 0 seconds", text);

text = DurationFormatUtils.formatDurationWords(0, true, true);
assertEquals("0 seconds", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, true, true);
assertEquals("50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, true, true);
Expand All @@ -357,6 +361,8 @@ public void testFormatDurationWords() {
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, true, true);
assertEquals("1 day", text);

text = DurationFormatUtils.formatDurationWords(0, false, true);
assertEquals("0 days", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, false, true);
assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, false, true);
Expand All @@ -370,6 +376,8 @@ public void testFormatDurationWords() {
text = DurationFormatUtils.formatDurationWords(24 * 60 * 60 * 1000, false, true);
assertEquals("1 day", text);

text = DurationFormatUtils.formatDurationWords(0, false, false);
assertEquals("0 days 0 hours 0 minutes 0 seconds", text);
text = DurationFormatUtils.formatDurationWords(50 * 1000, false, false);
assertEquals("0 days 0 hours 0 minutes 50 seconds", text);
text = DurationFormatUtils.formatDurationWords(65 * 1000, false, false);
Expand Down