瀏覽代碼

Improvements to Version

Implement compareTo(),
Add isValid(),
Accept integers in the String ctor,
Added unit test
tags/0.6.3m1rc1
Chris Smith 15 年之前
父節點
當前提交
f6daef7b3c
共有 2 個檔案被更改,包括 117 行新增5 行删除
  1. 47
    5
      src/com/dmdirc/updater/Version.java
  2. 70
    0
      test/com/dmdirc/updater/VersionTest.java

+ 47
- 5
src/com/dmdirc/updater/Version.java 查看文件

@@ -31,8 +31,8 @@ package com.dmdirc.updater;
31 31
  */
32 32
 public class Version implements Comparable<Version> {
33 33
 
34
-    private final int intVersion;
35
-    private final String strVersion;
34
+    protected final int intVersion;
35
+    protected final String strVersion;
36 36
 
37 37
     public Version(final int version) {
38 38
         this.intVersion = version;
@@ -40,16 +40,58 @@ public class Version implements Comparable<Version> {
40 40
     }
41 41
 
42 42
     public Version(final String version) {
43
-        this.intVersion = -1;
44
-        this.strVersion = version;
43
+        if (version.matches("^[0-9]+$")) {
44
+            this.intVersion = Integer.parseInt(version);
45
+            this.strVersion = null;
46
+        } else if (version.matches("^[0-9]+(\\.[0-9]+)*(\\-[0-9]+\\-g[a-z0-9]{7})?$")) {
47
+            this.intVersion = Integer.MIN_VALUE;
48
+            this.strVersion = version;
49
+        } else {
50
+            this.intVersion = Integer.MIN_VALUE;
51
+            this.strVersion = null;
52
+        }
45 53
     }
46 54
 
47 55
     /** {@inheritDoc} */
48 56
     @Override
49 57
     public int compareTo(final Version o) {
50
-        throw new UnsupportedOperationException("Not supported yet.");
58
+        if (o.intVersion > Integer.MIN_VALUE && this.intVersion > Integer.MIN_VALUE) {
59
+            return this.intVersion - o.intVersion;
60
+        } else if (o.strVersion == null || this.strVersion == null) {
61
+            return 0;
62
+        } else {
63
+            final String myParts[] = this.strVersion.split("-");
64
+            final String thParts[] = o.strVersion.split("-");
65
+
66
+            final String myFirstParts[] = myParts[0].split("\\.");
67
+            final String thFirstParts[] = thParts[0].split("\\.");
68
+
69
+            for (int i = 0; i < Math.max(myFirstParts.length, thFirstParts.length); i++) {
70
+                final int myInt = myFirstParts.length > i ? Integer.parseInt(myFirstParts[i]) : 0;
71
+                final int thInt = thFirstParts.length > i ? Integer.parseInt(thFirstParts[i]) : 0;
72
+
73
+                if (myInt != thInt) {
74
+                    return myInt - thInt;
75
+                }
76
+            }
77
+
78
+            final int myInt = myParts.length > 1 ? Integer.parseInt(myParts[1]) : 0;
79
+            final int thInt = thParts.length > 1 ? Integer.parseInt(thParts[1]) : 0;
80
+
81
+            return myInt - thInt;
82
+        }
83
+    }
84
+
85
+    /**
86
+     * Determines whether or not this represents a valid version.
87
+     *
88
+     * @return True if the version is valid, false otherwise
89
+     */
90
+    public boolean isValid() {
91
+        return intVersion > Integer.MIN_VALUE || strVersion != null;
51 92
     }
52 93
 
94
+    /** {@inheritDoc} */
53 95
     @Override
54 96
     public String toString() {
55 97
         return strVersion == null ? String.valueOf(intVersion) : strVersion;

+ 70
- 0
test/com/dmdirc/updater/VersionTest.java 查看文件

@@ -0,0 +1,70 @@
1
+/*
2
+ * Copyright (c) 2006-2009 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.updater;
24
+
25
+import java.util.Arrays;
26
+import java.util.List;
27
+import org.junit.Test;
28
+import org.junit.runner.RunWith;
29
+import org.junit.runners.Parameterized;
30
+import static org.junit.Assert.*;
31
+
32
+@RunWith(Parameterized.class)
33
+public class VersionTest {
34
+
35
+    private final Version v1, v2;
36
+    private final int min, max;
37
+
38
+    public VersionTest(Version v1, Version v2, int max, int min) {
39
+        this.v1 = v1;
40
+        this.v2 = v2;
41
+        this.min = min;
42
+        this.max = max;
43
+    }
44
+
45
+    @Test
46
+    public void testData() {
47
+        assertTrue(max + " > " + v1 + ".compareTo(" + v2 + ")", max > v1.compareTo(v2));
48
+        assertTrue(min + " < " + v1 + ".compareTo(" + v2 + ")", min < v1.compareTo(v2));
49
+    }
50
+
51
+    @Parameterized.Parameters
52
+    public static List<Object[]> data() {
53
+        return Arrays.asList(new Object[][]{
54
+            {new Version(1), new Version(2), 0, Integer.MIN_VALUE},
55
+            {new Version(1), new Version(2000), 0, Integer.MIN_VALUE},
56
+            {new Version(2), new Version(1), Integer.MAX_VALUE, 0},
57
+            {new Version(1), new Version(1), 1, -1},
58
+            {new Version(0), new Version(0), 1, -1},
59
+            {new Version("0.6"), new Version(1), 1, -1},
60
+            {new Version("0.6"), new Version("0.6"), 1, -1},
61
+            {new Version("0.6"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
62
+            {new Version("0.6-149-gaaaaaaa"), new Version("0.6.3"), 0, Integer.MIN_VALUE},
63
+            {new Version("0.6-149-gaaaaaaa"), new Version("0.6.3-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
64
+            {new Version("0.6.3"), new Version("0.6.3-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
65
+            {new Version("0.6.3-148-gaaaaaaa"), new Version("0.6.3-149-gaaaaaaa"), 0, Integer.MIN_VALUE},
66
+            {new Version("0.6.3-148-gaaaaaaa"), new Version("0.6.3-148-gaaaaaaa"), 1, -1},
67
+        });
68
+    }
69
+
70
+}

Loading…
取消
儲存