Browse Source

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 years ago
parent
commit
ff6ee4874a

+ 8
- 2
src/com/dmdirc/util/CommandUtils.java View File

27
 import java.util.List;
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
      * Parses the specified command into an array of arguments. Arguments are
40
      * Parses the specified command into an array of arguments. Arguments are
35
      * separated by spaces. Multi-word arguments may be specified by starting
41
      * separated by spaces. Multi-word arguments may be specified by starting

+ 0
- 2
src/com/dmdirc/util/ConfigFile.java View File

34
 
34
 
35
 /**
35
 /**
36
  * Reads and writes a standard DMDirc config file.
36
  * Reads and writes a standard DMDirc config file.
37
- *
38
- * @author chris
39
  */
37
  */
40
 public class ConfigFile extends TextFile {
38
 public class ConfigFile extends TextFile {
41
 
39
 

+ 84
- 0
src/com/dmdirc/util/DateUtils.java View File

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 View File

25
 /**
25
 /**
26
  * Defines the method that objects interested in receiving download progress
26
  * Defines the method that objects interested in receiving download progress
27
  * updates should implement.
27
  * updates should implement.
28
- *
29
- * @author chris
30
  */
28
  */
31
 public interface DownloadListener {
29
 public interface DownloadListener {
32
 
30
 

+ 0
- 2
src/com/dmdirc/util/Downloader.java View File

40
 
40
 
41
 /**
41
 /**
42
  * Allows easy downloading of files from HTTP sites.
42
  * Allows easy downloading of files from HTTP sites.
43
- *
44
- * @author Chris
45
  */
43
  */
46
 public final class Downloader {
44
 public final class Downloader {
47
 
45
 

+ 0
- 1
src/com/dmdirc/util/EquatableWeakReference.java View File

30
  * method.
30
  * method.
31
  *
31
  *
32
  * @param <T> The type of object that this reference contains
32
  * @param <T> The type of object that this reference contains
33
- * @author chris
34
  */
33
  */
35
 public class EquatableWeakReference<T> extends WeakReference<T> {
34
 public class EquatableWeakReference<T> extends WeakReference<T> {
36
 
35
 

+ 0
- 1
src/com/dmdirc/util/InvalidConfigFileException.java View File

24
 
24
 
25
 /**
25
 /**
26
  * Thrown to indicate that a config file is invalid.
26
  * Thrown to indicate that a config file is invalid.
27
- * @author chris
28
  */
27
  */
29
 public class InvalidConfigFileException extends Exception {
28
 public class InvalidConfigFileException extends Exception {
30
 
29
 

+ 0
- 2
src/com/dmdirc/util/ListenerList.java View File

30
 /**
30
 /**
31
  * Represents a list of event listeners, similar to EventListenerList, but
31
  * Represents a list of event listeners, similar to EventListenerList, but
32
  * not swing specific.
32
  * not swing specific.
33
- *
34
- * @author chris
35
  */
33
  */
36
 public class ListenerList {
34
 public class ListenerList {
37
 
35
 

+ 0
- 3
src/com/dmdirc/util/RollingList.java View File

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

+ 4
- 5
src/com/dmdirc/util/StreamReader.java View File

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

+ 0
- 1
src/com/dmdirc/util/StreamUtil.java View File

29
  * Utilities for dealing with streams.
29
  * Utilities for dealing with streams.
30
  *
30
  *
31
  * @since 0.6.3m2
31
  * @since 0.6.3m2
32
- * @author chris
33
  */
32
  */
34
 public final class StreamUtil {
33
 public final class StreamUtil {
35
 
34
 

+ 0
- 5
src/com/dmdirc/util/TextFile.java View File

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

+ 0
- 1
src/com/dmdirc/util/WeakList.java View File

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

+ 0
- 1
src/com/dmdirc/util/WeakMapList.java View File

32
  *
32
  *
33
  * @param <S> the type of keys maintained by this map
33
  * @param <S> the type of keys maintained by this map
34
  * @param <T> the type of mapped values
34
  * @param <T> the type of mapped values
35
- * @author chris
36
  */
35
  */
37
 public class WeakMapList<S,T> extends MapList<S,T> {
36
 public class WeakMapList<S,T> extends MapList<S,T> {
38
 
37
 

+ 63
- 0
test/com/dmdirc/util/DateUtilsTest.java View File

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…
Cancel
Save