Browse Source

Move formatter.formatDuration to utils

Fixes CLIENT-41

Change-Id: I8742650e3f2801470446590a32922bef32995fb8
Depends-On: I8269aab84413d3865975a6ca15dc656a525895ea
Depends-On: I5010c1c77247579e586e20a11a1a9b7b73b81307
Reviewed-on: http://gerrit.dmdirc.com/1929
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.6.6b1
Greg Holmes 13 years ago
parent
commit
2c1653bfc4

+ 3
- 50
src/com/dmdirc/ui/messages/Formatter.java View File

@@ -24,6 +24,7 @@ package com.dmdirc.ui.messages;
24 24
 
25 25
 import com.dmdirc.Precondition;
26 26
 import com.dmdirc.config.ConfigManager;
27
+import com.dmdirc.util.DateUtils;
27 28
 
28 29
 import java.util.HashMap;
29 30
 import java.util.IllegalFormatConversionException;
@@ -141,7 +142,8 @@ public final class Formatter {
141 142
                 break;
142 143
             case 'u':
143 144
                 // Duration hacks
144
-                res[i] = formatDuration(Integer.valueOf(String.valueOf(args[i].toString())));
145
+                res[i] = DateUtils.formatDuration(Integer.valueOf(
146
+                        String.valueOf(args[i].toString())));
145 147
                 break;
146 148
             default:
147 149
                 res[i] = args[i];
@@ -153,55 +155,6 @@ public final class Formatter {
153 155
         return res;
154 156
     }
155 157
 
156
-    /**
157
-     * Tests for and adds one component of the duration format.
158
-     *
159
-     * @param builder The string builder to append text to
160
-     * @param current The number of seconds in the duration
161
-     * @param duration The number of seconds in this component
162
-     * @param name The name of this component
163
-     * @return The number of seconds used by this component
164
-     */
165
-    private static int doDuration(final StringBuilder builder, final int current,
166
-            final int duration, final String name) {
167
-        int res = 0;
168
-
169
-        if (current >= duration) {
170
-            final int units = current / duration;
171
-            res = units * duration;
172
-
173
-            if (builder.length() > 0) {
174
-                builder.append(", ");
175
-            }
176
-
177
-            builder.append(units);
178
-            builder.append(' ');
179
-            builder.append(name + (units != 1 ? 's' : ""));
180
-        }
181
-
182
-        return res;
183
-    }
184
-
185
-    /**
186
-     * Formats the specified number of seconds as a string containing the
187
-     * number of days, hours, minutes and seconds.
188
-     *
189
-     * @param duration The duration in seconds to be formatted
190
-     * @return A textual version of the duration
191
-     */
192
-    public static String formatDuration(final int duration) {
193
-        final StringBuilder buff = new StringBuilder();
194
-
195
-        int seconds = duration;
196
-
197
-        seconds -= doDuration(buff, seconds, 60*60*24, "day");
198
-        seconds -= doDuration(buff, seconds, 60*60, "hour");
199
-        seconds -= doDuration(buff, seconds, 60, "minute");
200
-        seconds -= doDuration(buff, seconds, 1, "second");
201
-
202
-        return buff.length() == 0 ? "0 seconds" : buff.toString();
203
-    }
204
-
205 158
     /**
206 159
      * Analyses the specified format string and fills in the format type cache.
207 160
      *

+ 1
- 35
test/com/dmdirc/ui/messages/FormatterTest.java View File

@@ -60,38 +60,4 @@ public class FormatterTest {
60 60
     public void testFormatDuration() {
61 61
         assertEquals("1 minute, 1 second", Formatter.formatMessage(mcm, "1%1$u", "61"));
62 62
     }
63
-    
64
-    @Test
65
-    public void testFormatDurationSeconds() {
66
-        assertEquals("1 second", Formatter.formatDuration(1));
67
-        assertEquals("2 seconds", Formatter.formatDuration(2));
68
-    }
69
-    
70
-    @Test
71
-    public void testFormatDurationMinutes() {
72
-        assertEquals("1 minute", Formatter.formatDuration(60));
73
-        assertEquals("1 minute, 1 second", Formatter.formatDuration(61));
74
-        assertEquals("1 minute, 2 seconds", Formatter.formatDuration(62));
75
-        assertEquals("2 minutes, 2 seconds", Formatter.formatDuration(122));
76
-    }
77
-    
78
-    @Test
79
-    public void testFormatDurationHours() {
80
-        assertEquals("1 hour", Formatter.formatDuration(3600));
81
-        assertEquals("1 hour, 1 second", Formatter.formatDuration(3601));
82
-        assertEquals("2 hours, 1 minute, 5 seconds", Formatter.formatDuration(7265));
83
-    }
84
-    
85
-    @Test
86
-    public void testFormatDurationDays() {
87
-        assertEquals("1 day", Formatter.formatDuration(86400));
88
-        assertEquals("1 day, 10 minutes, 1 second", Formatter.formatDuration(87001));
89
-    }
90
-
91
-    @Test
92
-    public void testFormatNoSeconds() {
93
-        assertEquals("0 seconds", Formatter.formatDuration(0));
94
-        assertEquals("0 seconds", Formatter.formatDuration(-100));
95
-    }
96
-
97
-}
63
+}

Loading…
Cancel
Save