Got rid of duplicated code to format durations

Added new functions and test cases.
master
Evan Harris 5 years ago
parent 0660c4f3e6
commit a793deab0b
No known key found for this signature in database
GPG Key ID: FF3BD4DA59FF9EDC
  1. 28
      airsonic-main/src/main/java/org/airsonic/player/domain/MediaFile.java
  2. 22
      airsonic-main/src/main/java/org/airsonic/player/service/upnp/CustomContentDirectory.java
  3. 32
      airsonic-main/src/main/java/org/airsonic/player/util/StringUtil.java
  4. 28
      airsonic-main/src/test/java/org/airsonic/player/util/StringUtilTestCase.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() {

@ -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() {

@ -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);
}
/**

@ -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");

Loading…
Cancel
Save