3 * Class providing operations on version numbers such as incrementation and comparison.
4 * Revision objects are created from and converted to strings in the internal format of version numbers (major.minor.branch).<br/>
5 * Revision objects can also be created from and converted to strings in user-defined format, thanks to a formating tool
6 * provided by the Format nested class.
9 * @author Daniel Brunier-Coulin
10 * @copyright OPEN CASCADE 2012
13 import java.nio.CharBuffer;
14 import java.text.ParseException;
16 import org.splat.dal.bo.som.ProgressState;
19 public class Revision {
25 public static class Format {
26 // --------------------------
27 private String pattern;
29 public Format (String pattern) {
30 this.pattern = pattern;
33 public String format (String verstring) {
34 CharBuffer version = CharBuffer.allocate(pattern.length() + 2*2); // Maximum possible size
35 char[] format = pattern.toCharArray();
36 String[] vernum = verstring.split("\\x2E"); // version is suposed of the internal form (m.n.s)
37 int branch = Integer.valueOf(vernum[2]);
39 for (int i=0; i<format.length; i++) {
40 char token = format[i];
45 version.put(vernum[0]);
46 } else if (token == 'm') {
47 version.put(vernum[1]);
48 } else if (token == 's') {
49 version.put(vernum[2]);
51 } else if (token == '[') {
52 if (branch == 0) while (format[i] != ']') i += 1;
53 } else if (token == ']') {
59 return new String(version.array(), 0, version.position());
62 public Revision parse (String verstring) throws ParseException {
63 char[] format = pattern.toCharArray();
64 char[] version = verstring.toCharArray();
65 CharBuffer major = CharBuffer.allocate(4);
66 CharBuffer minor = CharBuffer.allocate(4);
67 CharBuffer branch = CharBuffer.allocate(4);
69 int cursor = 0; // Index into version array
70 for (int i=0; i<format.length; i++) { // The parsed string may not include the branch ID
71 char token = format[i];
76 while (cursor < version.length) {
77 if (!Character.isDigit(version[cursor])) break;
78 major.put(version[cursor]);
81 } else if (token == 'm') {
82 while (cursor < version.length) {
83 if (!Character.isDigit(version[cursor])) break;
84 minor.put(version[cursor]);
87 } else if (token == 's') {
88 while (cursor < version.length) {
89 if (!Character.isDigit(version[cursor])) break;
90 branch.put(version[cursor]);
94 } else if (token == '[' || token == ']') {
97 if (version[cursor] != token) throw new ParseException(verstring, cursor);
100 if (cursor >= version.length) break;
102 if (major.position() == 0) throw new ParseException(verstring, 0);
104 String majnum = new String(major.array(), 0, major.position());
105 if (minor.position() == 0) {
106 return new Revision(Integer.valueOf(majnum), 0);
108 String minum = new String(minor.array(), 0, minor.position());
109 if (branch.position() == 0) {
110 return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum));
112 String branum = new String(branch.array(), 0, branch.position());
113 return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum), branum);
118 public String toPattern () {
123 // ==============================================================================================================================
125 // ==============================================================================================================================
127 * Constructs a Revision object from the internal representation of a version number (m.n.s).
129 public Revision (String value) {
130 // ------------------------------
131 String[] vernum = value.split("\\x2E");
133 this.major = Integer.valueOf(vernum[0]);
134 this.minor = Integer.valueOf(vernum[1]);
135 this.branch = Integer.valueOf(vernum[2]);
137 catch (Exception e) { // NumberFormat or OutOfBound exception if value is not of the form m.n.s
144 // ------------------
149 private Revision (int major, int minor) {
150 // ---------------------------------------
155 private Revision (int major, int minor, String branch) {
156 // ------------------------------------------------------
159 this.branch = Integer.valueOf(branch);
162 // ==============================================================================================================================
163 // Public member function
164 // ==============================================================================================================================
166 public Revision incrementAs (ProgressState state) {
167 // -------------------------------------------------
168 if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) minor += 1;
169 else if (state == ProgressState.inCHECK) {
176 public boolean isGraterThan (Revision base) {
177 // ------------------------------------------
178 if (this.major > base.major) return true;
179 if (this.major < base.major) return false;
180 return (this.minor > base.minor);
183 public boolean isMinor () {
184 // -------------------------
188 public boolean isNull () {
189 // ------------------------
190 return (major+minor == 0);
193 * Sets the branch name of this revision.
195 * @param name the branch name or the internal representation of a version number (m.n.s)
196 * @return this revision object
198 public Revision setBranch (String name) {
199 // ---------------------------------------
200 String[] vernum = name.split("\\x2E");
202 branch = Integer.valueOf(vernum[vernum.length-1]);
206 * Returns the internal representation of a version number (m.n.s) represented by this Revision object.
208 public String toString () {
209 // -------------------------
210 StringBuffer version = new StringBuffer();
211 return version.append(major).append(".").append(minor).append(".").append(branch).toString();