Selaa lähdekoodia

Add DateUtils and a formatDuration method. Minor tidying.

Change-Id: I8269aab84413d3865975a6ca15dc656a525895ea
Reviewed-on: http://gerrit.dmdirc.com/1928
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 13 vuotta sitten
vanhempi
commit
ff6ee4874a

+ 8
- 2
src/com/dmdirc/util/CommandUtils.java Näytä tiedosto

@@ -27,9 +27,15 @@ import java.util.ArrayList;
27 27
 import java.util.List;
28 28
 
29 29
 /**
30
- * Utilities for manipulating strings
30
+ * Utilities methods associated with commands.
31 31
  */
32
-public class CommandUtils {
32
+public final class CommandUtils {
33
+
34
+    /** Private contructor to stop instantiation of class. */
35
+    private CommandUtils() {
36
+        //Shouldn't be used.
37
+    }
38
+
33 39
     /**
34 40
      * Parses the specified command into an array of arguments. Arguments are
35 41
      * separated by spaces. Multi-word arguments may be specified by starting

+ 0
- 2
src/com/dmdirc/util/ConfigFile.java Näytä tiedosto

@@ -34,8 +34,6 @@ import java.util.Map;
34 34
 
35 35
 /**
36 36
  * Reads and writes a standard DMDirc config file.
37
- *
38
- * @author chris
39 37
  */
40 38
 public class ConfigFile extends TextFile {
41 39
 

+ 84
- 0
src/com/dmdirc/util/DateUtils.java Näytä tiedosto

@@ -0,0 +1,84 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+/**
26
+ * Utility methods associated with dates.
27
+ */
28
+public final class DateUtils {
29
+
30
+    /** Private contructor to stop instantiation of class. */
31
+    private DateUtils() {
32
+        //Shouldn't be used
33
+    }
34
+
35
+    /**
36
+     * Tests for and adds one component of the duration format.
37
+     *
38
+     * @param builder The string builder to append text to
39
+     * @param current The number of seconds in the duration
40
+     * @param duration The number of seconds in this component
41
+     * @param name The name of this component
42
+     * @return The number of seconds used by this component
43
+     */
44
+    private static int doDuration(final StringBuilder builder, final int current,
45
+            final int duration, final String name) {
46
+        int res = 0;
47
+
48
+        if (current >= duration) {
49
+            final int units = current / duration;
50
+            res = units * duration;
51
+
52
+            if (builder.length() > 0) {
53
+                builder.append(", ");
54
+            }
55
+
56
+            builder.append(units);
57
+            builder.append(' ');
58
+            builder.append(name);
59
+            builder.append(units == 1 ? "" : 's');
60
+        }
61
+
62
+        return res;
63
+    }
64
+
65
+    /**
66
+     * Formats the specified number of seconds as a string containing the
67
+     * number of days, hours, minutes and seconds.
68
+     *
69
+     * @param duration The duration in seconds to be formatted
70
+     * @return A textual version of the duration
71
+     */
72
+    public static String formatDuration(final int duration) {
73
+        final StringBuilder buff = new StringBuilder();
74
+
75
+        int seconds = duration;
76
+
77
+        seconds -= doDuration(buff, seconds, 60*60*24, "day");
78
+        seconds -= doDuration(buff, seconds, 60*60, "hour");
79
+        seconds -= doDuration(buff, seconds, 60, "minute");
80
+        seconds -= doDuration(buff, seconds, 1, "second");
81
+
82
+        return buff.length() == 0 ? "0 seconds" : buff.toString();
83
+    }
84
+}

+ 0
- 2
src/com/dmdirc/util/DownloadListener.java Näytä tiedosto

@@ -25,8 +25,6 @@ package com.dmdirc.util;
25 25
 /**
26 26
  * Defines the method that objects interested in receiving download progress
27 27
  * updates should implement.
28
- *
29
- * @author chris
30 28
  */
31 29
 public interface DownloadListener {
32 30
 

+ 0
- 2
src/com/dmdirc/util/Downloader.java Näytä tiedosto

@@ -40,8 +40,6 @@ import java.util.Map;
40 40
 
41 41
 /**
42 42
  * Allows easy downloading of files from HTTP sites.
43
- *
44
- * @author Chris
45 43
  */
46 44
 public final class Downloader {
47 45
 

+ 0
- 1
src/com/dmdirc/util/EquatableWeakReference.java Näytä tiedosto

@@ -30,7 +30,6 @@ import java.lang.ref.WeakReference;
30 30
  * method.
31 31
  *
32 32
  * @param <T> The type of object that this reference contains
33
- * @author chris
34 33
  */
35 34
 public class EquatableWeakReference<T> extends WeakReference<T> {
36 35
 

+ 0
- 1
src/com/dmdirc/util/InvalidConfigFileException.java Näytä tiedosto

@@ -24,7 +24,6 @@ package com.dmdirc.util;
24 24
 
25 25
 /**
26 26
  * Thrown to indicate that a config file is invalid.
27
- * @author chris
28 27
  */
29 28
 public class InvalidConfigFileException extends Exception {
30 29
 

+ 0
- 2
src/com/dmdirc/util/ListenerList.java Näytä tiedosto

@@ -30,8 +30,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
30 30
 /**
31 31
  * Represents a list of event listeners, similar to EventListenerList, but
32 32
  * not swing specific.
33
- *
34
- * @author chris
35 33
  */
36 34
 public class ListenerList {
37 35
 

+ 0
- 3
src/com/dmdirc/util/RollingList.java Näytä tiedosto

@@ -19,7 +19,6 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 20
  * SOFTWARE.
21 21
  */
22
-
23 22
 package com.dmdirc.util;
24 23
 
25 24
 import java.util.ArrayList;
@@ -30,7 +29,6 @@ import java.util.List;
30 29
  * removes the oldest elements from the list to maintain this capacity.
31 30
  *
32 31
  * @param <T> The type if items that this list contains
33
- * @author chris
34 32
  */
35 33
 public class RollingList<T> {
36 34
 
@@ -216,5 +214,4 @@ public class RollingList<T> {
216 214
     public List<T> getList() {
217 215
         return new ArrayList<T>(items);
218 216
     }
219
-
220 217
 }

+ 4
- 5
src/com/dmdirc/util/StreamReader.java Näytä tiedosto

@@ -34,13 +34,11 @@ import java.util.List;
34 34
 public class StreamReader extends Thread {
35 35
 
36 36
     /** This is the Input Stream we are reading. */
37
-    private InputStream stream;
38
-
37
+    private final InputStream stream;
39 38
     /** List to store output in. */
40 39
     private List<String> list = null;
41
-
42 40
     /** StringBuffer to store output in. */
43
-    private StringBuffer buffer = null;
41
+    private StringBuffer buffer = null; //NOPMD
44 42
 
45 43
     /**
46 44
      * Create a new Stream Reader that discards output.
@@ -93,7 +91,8 @@ public class StreamReader extends Thread {
93 91
      */
94 92
     @Override
95 93
     public void run() {
96
-        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
94
+        final BufferedReader reader = new BufferedReader(
95
+                new InputStreamReader(stream));
97 96
         try {
98 97
             String line;
99 98
             while ((line = reader.readLine()) != null) {

+ 0
- 1
src/com/dmdirc/util/StreamUtil.java Näytä tiedosto

@@ -29,7 +29,6 @@ import java.io.IOException;
29 29
  * Utilities for dealing with streams.
30 30
  *
31 31
  * @since 0.6.3m2
32
- * @author chris
33 32
  */
34 33
 public final class StreamUtil {
35 34
 

+ 0
- 5
src/com/dmdirc/util/TextFile.java Näytä tiedosto

@@ -36,20 +36,15 @@ import java.util.List;
36 36
 
37 37
 /**
38 38
  * Allows reading and writing to a plain text file via a list of lines.
39
- *
40
- * @author chris
41 39
  */
42 40
 public class TextFile {
43 41
 
44 42
     /** The file we're dealing with. */
45 43
     private File file;
46
-
47 44
     /** The input stream we're dealing with. */
48 45
     private InputStream is;
49
-
50 46
     /** The lines we've read from the file. */
51 47
     private List<String> lines;
52
-
53 48
     /** The charset to use to read the file. */
54 49
     private final Charset charset;
55 50
 

+ 0
- 1
src/com/dmdirc/util/WeakList.java Näytä tiedosto

@@ -34,7 +34,6 @@ import java.util.ListIterator;
34 34
  * garbage collection) are handled transparently.
35 35
  *
36 36
  * @param <T> The type of object that this list will contain.
37
- * @author chris
38 37
  */
39 38
 public class WeakList<T> implements List<T> {
40 39
 

+ 0
- 1
src/com/dmdirc/util/WeakMapList.java Näytä tiedosto

@@ -32,7 +32,6 @@ import java.util.List;
32 32
  *
33 33
  * @param <S> the type of keys maintained by this map
34 34
  * @param <T> the type of mapped values
35
- * @author chris
36 35
  */
37 36
 public class WeakMapList<S,T> extends MapList<S,T> {
38 37
 

+ 63
- 0
test/com/dmdirc/util/DateUtilsTest.java Näytä tiedosto

@@ -0,0 +1,63 @@
1
+/*
2
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+import org.junit.Test;
26
+import static org.junit.Assert.*;
27
+
28
+public class DateUtilsTest {
29
+
30
+    @Test
31
+    public void testFormatDurationSeconds() {
32
+        assertEquals("1 second", DateUtils.formatDuration(1));
33
+        assertEquals("2 seconds", DateUtils.formatDuration(2));
34
+    }
35
+
36
+    @Test
37
+    public void testFormatDurationMinutes() {
38
+        assertEquals("1 minute", DateUtils.formatDuration(60));
39
+        assertEquals("1 minute, 1 second", DateUtils.formatDuration(61));
40
+        assertEquals("1 minute, 2 seconds", DateUtils.formatDuration(62));
41
+        assertEquals("2 minutes, 2 seconds", DateUtils.formatDuration(122));
42
+    }
43
+
44
+    @Test
45
+    public void testFormatDurationHours() {
46
+        assertEquals("1 hour", DateUtils.formatDuration(3600));
47
+        assertEquals("1 hour, 1 second", DateUtils.formatDuration(3601));
48
+        assertEquals("2 hours, 1 minute, 5 seconds", DateUtils.formatDuration(7265));
49
+    }
50
+
51
+    @Test
52
+    public void testFormatDurationDays() {
53
+        assertEquals("1 day", DateUtils.formatDuration(86400));
54
+        assertEquals("1 day, 10 minutes, 1 second", DateUtils.formatDuration(87001));
55
+    }
56
+
57
+    @Test
58
+    public void testFormatNoSeconds() {
59
+        assertEquals("0 seconds", DateUtils.formatDuration(0));
60
+        assertEquals("0 seconds", DateUtils.formatDuration(-100));
61
+    }
62
+
63
+}

Loading…
Peruuta
Tallenna