KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > scm > ScmVersion


1 package org.antmod.scm;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 /**
6  * Represents the version of a module in the SCM system.
7  *
8  * @author Klaas Waslander
9  */

10 public final class ScmVersion implements Comparable JavaDoc {
11     public final static String JavaDoc VERSIONSTRING_TRUNK = "trunk";
12
13     int major = -1;
14     int minor = -1;
15     int patch = -1;
16     String JavaDoc moduleName;
17
18     /**
19      *
20      * @param versionString "modulename-revisionString", revisionString being either "trunk" or a two digit / three digit revision string.
21      */

22     public ScmVersion(String JavaDoc versionString) {
23         int i = versionString.indexOf("-");
24         if (i < 0) {
25             throw new IllegalArgumentException JavaDoc("Separator '-' missing between modulename and moduleversion");
26         }
27         this.moduleName = versionString.substring(0, i);
28         parseRevision(versionString.substring(i+1));
29     }
30
31     /**
32      *
33      * @param moduleName The name of the module this version applies to
34      * @param revisionString Either "trunk" or a two digit / three digit revision string.
35      */

36     public ScmVersion(String JavaDoc moduleName, String JavaDoc revisionString) {
37         this.moduleName = moduleName;
38         parseRevision(revisionString);
39     }
40
41     /**
42      * Parses the given revision string, handling both "-" and "." as separator between the
43      * up to three digits. The revision string is also allowed to be "trunk".
44      * @param revision Either "trunk" or a two digit / three digit revision string.
45      */

46     private void parseRevision(String JavaDoc revision) {
47         //System.err.println("PARSE REV \"" + revision + "\"");
48
if (revision != null && !revision.equalsIgnoreCase("trunk") && !revision.equalsIgnoreCase("head")) {
49             String JavaDoc versionSeparator = ".";
50             if (revision.indexOf("-") > 0) {
51                 versionSeparator = "-";
52             }
53             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(revision, versionSeparator);
54             this.major = Integer.parseInt(st.nextToken());
55             if (st.hasMoreTokens()) {
56                 this.minor = Integer.parseInt(st.nextToken());
57             }
58             if (st.hasMoreTokens()) {
59                 this.patch = Integer.parseInt(st.nextToken());
60             }
61         }
62     }
63
64     public ScmVersion(String JavaDoc moduleName, int major, int minor, int patch) {
65         this.moduleName = moduleName;
66         this.major = major;
67         this.minor = minor;
68         this.patch = patch;
69     }
70
71     public ScmVersion getParentBranch() {
72         if (isTrunk() || isBranch()) {
73             return getTrunkBranch();
74         } else {
75             return new ScmVersion(this.moduleName, major, minor, -1);
76         }
77     }
78
79     public ScmVersion getTrunkBranch() {
80         return getTrunkBranch(this.moduleName);
81     }
82
83     public static ScmVersion getTrunkBranch(String JavaDoc moduleName) {
84         return new ScmVersion(moduleName, -1, -1, -1);
85     }
86
87     public ScmVersion getNextIncrement() {
88         if (this.moduleName == null || this.moduleName.trim().length() == 0) {
89             throw new RuntimeException JavaDoc("Modulename unknown - next increment not possible.");
90         }
91
92         if (isBranch()) {
93             return new ScmVersion(this.moduleName, major, minor + 1, -1);
94         }
95         if (isTag()) {
96             return new ScmVersion(this.moduleName, major, minor, patch + 1);
97         }
98         return new ScmVersion(this.moduleName, 1, 0, -1);
99     }
100
101     public boolean isBranch() {
102         return patch < 0 && major >= 0;
103     }
104
105     public boolean isTag() {
106         return patch >= 0;
107     }
108
109     public boolean isTrunk() {
110         return major < 0;
111     }
112
113     public String JavaDoc getModuleName() {
114         return moduleName;
115     }
116     
117     public int getMajor() {
118         return major;
119     }
120     
121     public int getMinor() {
122         return minor;
123     }
124     
125     public int getPatch() {
126         return patch;
127     }
128
129     public boolean equals(Object JavaDoc obj) {
130         if (obj instanceof ScmVersion) {
131             ScmVersion other = (ScmVersion) obj;
132             if (moduleName != null && other.getModuleName() != null) {
133                 if (!moduleName.equals(other.getModuleName())) {
134                     return false;
135                 }
136             }
137         }
138         return toString().equals(obj.toString()) || (isTrunk() && obj.toString().equalsIgnoreCase("trunk"));
139     }
140
141     public String JavaDoc toString() {
142         return toString('-');
143     }
144
145     public String JavaDoc toString(char versionSeparator) {
146         if (major >= 0) {
147             String JavaDoc result = String.valueOf(major) + versionSeparator + String.valueOf(minor);
148             if (patch >= 0) {
149                 // String.valueOf() is very very important here: otherwise it is added as charachter value (45)...
150
result += versionSeparator + String.valueOf(patch);
151             }
152             return result;
153         } else {
154             return VERSIONSTRING_TRUNK;
155         }
156     }
157
158     public String JavaDoc toFullString() {
159         return getModuleName() + "_" + toString();
160     }
161     
162     public String JavaDoc toFullString(char versionSeparator) {
163         return getModuleName() + "_" + toString(versionSeparator);
164     }
165
166     public int compareTo(Object JavaDoc obj) {
167         if (!(obj instanceof ScmVersion)) {
168             throw new IllegalArgumentException JavaDoc("Cannot compare object of instance " + obj.getClass() + " to ScmVersion object");
169         }
170         ScmVersion other = (ScmVersion) obj;
171         if (major > other.major) return -1;
172         if (major < other.major) return 1;
173         if (minor > other.minor) return -1;
174         if (minor < other.minor) return 1;
175         if (patch > other.patch) return -1;
176         if (patch < other.patch) return 1;
177         return 0;
178     }
179 }
180
Popular Tags