View Javadoc

1   package org_scala_tools_maven;
2   
3   
4   import java.util.regex.Matcher;
5   import java.util.regex.Pattern;
6   
7   
8   public class VersionNumber /*implements Serializable*/ implements Comparable<VersionNumber>{
9       private static Pattern _regexp = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?([-\\.]\\w+)?");
10  
11      public int major;
12      public int minor;
13      public int bugfix;
14      public String modifier;
15  
16      public VersionNumber(){
17      }
18  
19      public VersionNumber(String s) {
20          Matcher match = _regexp.matcher(s);
21          if (!match.find()) {
22              throw new IllegalArgumentException("invalid versionNumber : major.minor(.bugfix)(modifier) :" + s);
23          }
24          major = Integer.parseInt(match.group(1));
25          minor = Integer.parseInt(match.group(2));
26          if ((match.group(3) != null) && (match.group(3).length() > 1)){
27              bugfix = Integer.parseInt(match.group(3).substring(1));
28          }
29          if ((match.group(4) != null) && (match.group(4).length() > 1)){
30              modifier = match.group(4);
31          }
32      }
33  
34      @Override
35      public String toString() {
36           StringBuilder str = new StringBuilder();
37           str.append(major)
38                   .append('.')
39                   .append(minor)
40                   .append('.')
41                   .append(bugfix)
42                   ;
43           if ((modifier != null) && (modifier.length() > 0)){
44               str.append(modifier);
45           }
46           return str.toString();
47       }
48  
49      /**
50       * Doesn't compare modifier
51       */
52      public int compareTo(VersionNumber o) {
53          int back = 0;
54          if ((back == 0) && (major > o.major)) {
55              back = 1;
56          }
57          if ((back == 0) && (major < o.major)) {
58              back = -1;
59          }
60          if ((back == 0) && (minor > o.minor)) {
61              back = 1;
62          }
63          if ((back == 0) && (minor < o.minor)) {
64              back = -1;
65          }
66          if ((back == 0) && (bugfix > o.bugfix)) {
67              back = 1;
68          }
69          if ((back == 0) && (bugfix < o.bugfix)) {
70              back = -1;
71          }
72          return back;
73      }
74  
75  
76  }