From a793deab0ba7148cecf03ecb90ff2572c4d9f440 Mon Sep 17 00:00:00 2001 From: Evan Harris Date: Mon, 21 Oct 2019 07:45:08 -0500 Subject: [PATCH] Got rid of duplicated code to format durations Added new functions and test cases. --- .../org/airsonic/player/domain/MediaFile.java | 28 ++-------------- .../service/upnp/CustomContentDirectory.java | 22 +------------ .../org/airsonic/player/util/StringUtil.java | 32 +++++++++++++------ .../player/util/StringUtilTestCase.java | 28 ++++++++++++++++ 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/domain/MediaFile.java b/airsonic-main/src/main/java/org/airsonic/player/domain/MediaFile.java index f4847ad9..b8e29740 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/domain/MediaFile.java +++ b/airsonic-main/src/main/java/org/airsonic/player/domain/MediaFile.java @@ -22,6 +22,7 @@ package org.airsonic.player.domain; import com.google.common.base.Function; import com.google.common.collect.Lists; import org.airsonic.player.util.FileUtil; +import org.airsonic.player.util.StringUtil; import org.apache.commons.io.FilenameUtils; import java.io.File; @@ -279,31 +280,8 @@ public class MediaFile { if (durationSeconds == null) { return null; } - - StringBuilder result = new StringBuilder(8); - - int seconds = durationSeconds; - - int hours = seconds / 3600; - seconds -= hours * 3600; - - int minutes = seconds / 60; - seconds -= minutes * 60; - - if (hours > 0) { - result.append(hours).append(':'); - if (minutes < 10) { - result.append('0'); - } - } - - result.append(minutes).append(':'); - if (seconds < 10) { - result.append('0'); - } - result.append(seconds); - - return result.toString(); + // Return in M:SS or H:MM:SS + return StringUtil.formatDuration(durationSeconds); } public Long getFileSize() { diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/upnp/CustomContentDirectory.java b/airsonic-main/src/main/java/org/airsonic/player/service/upnp/CustomContentDirectory.java index a607c109..70a84448 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/upnp/CustomContentDirectory.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/upnp/CustomContentDirectory.java @@ -89,27 +89,7 @@ public abstract class CustomContentDirectory extends AbstractContentDirectorySer if (seconds == null) { return null; } - - StringBuilder result = new StringBuilder(8); - - int hours = seconds / 3600; - seconds -= hours * 3600; - - int minutes = seconds / 60; - seconds -= minutes * 60; - - result.append(hours).append(':'); - if (minutes < 10) { - result.append('0'); - } - result.append(minutes).append(':'); - if (seconds < 10) { - result.append('0'); - } - result.append(seconds); - result.append(".0"); - - return result.toString(); + return StringUtil.formatDurationHMMSS((int)seconds) + ".0"; } protected String getBaseUrl() { diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java index b3ebfd58..ddc9b847 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java @@ -226,19 +226,33 @@ public final class StringUtil { } /** - * Formats a duration with minutes and seconds, e.g., "93:45" + * Formats a duration with minutes and seconds, e.g., "4:34" or "93:45" */ public static String formatDurationMSS(int seconds) { - int minutes = seconds / 60; - int secs = seconds % 60; + if (seconds < 0) { + throw new IllegalArgumentException("seconds must be >= 0"); + } + return String.format("%d:%02d", seconds / 60, seconds % 60); + } + + /** + * Formats a duration with H:MM:SS, e.g., "1:33:45" + */ + public static String formatDurationHMMSS(int seconds) { + int hours = seconds / 3600; + seconds -= hours * 3600; - StringBuilder builder = new StringBuilder(6); - builder.append(minutes).append(":"); - if (secs < 10) { - builder.append("0"); + return String.format("%d:%s%s", hours, seconds < 600 ? "0" : "", formatDurationMSS(seconds)); + } + + /** + * Formats a duration to M:SS or H:MM:SS + */ + public static String formatDuration(int seconds) { + if (seconds >= 3600) { + return formatDurationHMMSS(seconds); } - builder.append(secs); - return builder.toString(); + return formatDurationMSS(seconds); } /** diff --git a/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java index f57d62f4..78fccf92 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.java @@ -95,6 +95,34 @@ public class StringUtilTestCase extends TestCase { assertEquals("Error in formatDurationMSS().", "121:40", StringUtil.formatDurationMSS(7300)); } + public void testFormatDurationHMMSS() { + assertEquals("Error in formatDurationHMMSS().", "0:00:00", StringUtil.formatDurationHMMSS(0)); + assertEquals("Error in formatDurationHMMSS().", "0:00:05", StringUtil.formatDurationHMMSS(5)); + assertEquals("Error in formatDurationHMMSS().", "0:00:10", StringUtil.formatDurationHMMSS(10)); + assertEquals("Error in formatDurationHMMSS().", "0:00:59", StringUtil.formatDurationHMMSS(59)); + assertEquals("Error in formatDurationHMMSS().", "0:01:00", StringUtil.formatDurationHMMSS(60)); + assertEquals("Error in formatDurationHMMSS().", "0:01:01", StringUtil.formatDurationHMMSS(61)); + assertEquals("Error in formatDurationHMMSS().", "0:01:10", StringUtil.formatDurationHMMSS(70)); + assertEquals("Error in formatDurationHMMSS().", "0:10:00", StringUtil.formatDurationHMMSS(600)); + assertEquals("Error in formatDurationHMMSS().", "0:45:50", StringUtil.formatDurationHMMSS(2750)); + assertEquals("Error in formatDurationHMMSS().", "1:23:45", StringUtil.formatDurationHMMSS(5025)); + assertEquals("Error in formatDurationHMMSS().", "2:01:40", StringUtil.formatDurationHMMSS(7300)); + } + + public void testFormatDuration() { + assertEquals("Error in formatDuration().", "0:00", StringUtil.formatDuration(0)); + assertEquals("Error in formatDuration().", "0:05", StringUtil.formatDuration(5)); + assertEquals("Error in formatDuration().", "0:10", StringUtil.formatDuration(10)); + assertEquals("Error in formatDuration().", "0:59", StringUtil.formatDuration(59)); + assertEquals("Error in formatDuration().", "1:00", StringUtil.formatDuration(60)); + assertEquals("Error in formatDuration().", "1:01", StringUtil.formatDuration(61)); + assertEquals("Error in formatDuration().", "1:10", StringUtil.formatDuration(70)); + assertEquals("Error in formatDuration().", "10:00", StringUtil.formatDuration(600)); + assertEquals("Error in formatDuration().", "45:50", StringUtil.formatDuration(2750)); + assertEquals("Error in formatDuration().", "1:23:45", StringUtil.formatDuration(5025)); + assertEquals("Error in formatDuration().", "2:01:40", StringUtil.formatDuration(7300)); + } + public void testSplit() { doTestSplit("u2 rem \"greatest hits\"", "u2", "rem", "greatest hits"); doTestSplit("u2", "u2");