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;
17 public class Revision {
23 public static class Format {
24 // --------------------------
25 private String pattern;
27 public Format (String pattern) {
28 this.pattern = pattern;
31 public String format (String verstring) {
32 CharBuffer version = CharBuffer.allocate(pattern.length() + 2*2); // Maximum possible size
33 char[] format = pattern.toCharArray();
34 String[] vernum = verstring.split("\\x2E"); // version is suposed of the internal form (m.n.s)
35 int branch = Integer.valueOf(vernum[2]);
37 for (int i=0; i<format.length; i++) {
38 char token = format[i];
43 version.put(vernum[0]);
44 } else if (token == 'm') {
45 version.put(vernum[1]);
46 } else if (token == 's') {
47 version.put(vernum[2]);
49 } else if (token == '[') {
50 if (branch == 0) while (format[i] != ']') i += 1;
51 } else if (token == ']') {
57 return new String(version.array(), 0, version.position());
60 public Revision parse (String verstring) throws ParseException {
61 char[] format = pattern.toCharArray();
62 char[] version = verstring.toCharArray();
63 CharBuffer major = CharBuffer.allocate(4);
64 CharBuffer minor = CharBuffer.allocate(4);
65 CharBuffer branch = CharBuffer.allocate(4);
67 int cursor = 0; // Index into version array
68 for (int i=0; i<format.length; i++) { // The parsed string may not include the branch ID
69 char token = format[i];
74 while (cursor < version.length) {
75 if (!Character.isDigit(version[cursor])) break;
76 major.put(version[cursor]);
79 } else if (token == 'm') {
80 while (cursor < version.length) {
81 if (!Character.isDigit(version[cursor])) break;
82 minor.put(version[cursor]);
85 } else if (token == 's') {
86 while (cursor < version.length) {
87 if (!Character.isDigit(version[cursor])) break;
88 branch.put(version[cursor]);
92 } else if (token == '[' || token == ']') {
95 if (version[cursor] != token) throw new ParseException(verstring, cursor);
98 if (cursor >= version.length) break;
100 if (major.position() == 0) throw new ParseException(verstring, 0);
102 String majnum = new String(major.array(), 0, major.position());
103 if (minor.position() == 0) {
104 return new Revision(Integer.valueOf(majnum), 0);
106 String minum = new String(minor.array(), 0, minor.position());
107 if (branch.position() == 0) {
108 return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum));
110 String branum = new String(branch.array(), 0, branch.position());
111 return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum), branum);
116 public String toPattern () {
121 // ==============================================================================================================================
123 // ==============================================================================================================================
125 * Constructs a Revision object from the internal representation of a version number (m.n.s).
127 public Revision (String value) {
128 // ------------------------------
129 String[] vernum = value.split("\\x2E");
131 this.major = Integer.valueOf(vernum[0]);
132 this.minor = Integer.valueOf(vernum[1]);
133 this.branch = Integer.valueOf(vernum[2]);
135 catch (Exception e) { // NumberFormat or OutOfBound exception if value is not of the form m.n.s
142 // ------------------
147 private Revision (int major, int minor) {
148 // ---------------------------------------
153 private Revision (int major, int minor, String branch) {
154 // ------------------------------------------------------
157 this.branch = Integer.valueOf(branch);
160 // ==============================================================================================================================
161 // Public member function
162 // ==============================================================================================================================
164 public Revision incrementAs (ProgressState state) {
165 // -------------------------------------------------
166 if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) minor += 1;
167 else if (state == ProgressState.inCHECK) {
174 public boolean isGraterThan (Revision base) {
175 // ------------------------------------------
176 if (this.major > base.major) return true;
177 if (this.major < base.major) return false;
178 return (this.minor > base.minor);
181 public boolean isMinor () {
182 // -------------------------
186 public boolean isNull () {
187 // ------------------------
188 return (major+minor == 0);
191 * Sets the branch name of this revision.
193 * @param name the branch name or the internal representation of a version number (m.n.s)
194 * @return this revision object
196 public Revision setBranch (String name) {
197 // ---------------------------------------
198 String[] vernum = name.split("\\x2E");
200 branch = Integer.valueOf(vernum[vernum.length-1]);
204 * Returns the internal representation of a version number (m.n.s) represented by this Revision object.
206 public String toString () {
207 // -------------------------
208 StringBuffer version = new StringBuffer();
209 return version.append(major).append(".").append(minor).append(".").append(branch).toString();