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 {
21 private transient int _major;
22 private transient int _minor;
23 private transient int _branch;
25 public static class Format {
26 private transient final String _pattern;
28 public Format (final String pattern) {
29 this._pattern = pattern;
32 public String format (final String verstring) {
33 CharBuffer version = CharBuffer.allocate(_pattern.length() + 2*2); // Maximum possible size
34 char[] format = _pattern.toCharArray();
35 String[] vernum = verstring.split("\\x2E"); // version is suposed of the internal form (m.n.s)
36 int branch = Integer.valueOf(vernum[2]);
38 for (int i=0; i<format.length; i++) {
39 char token = format[i];
44 version.put(vernum[0]);
45 } else if (token == 'm') {
46 version.put(vernum[1]);
47 } else if (token == 's') {
48 version.put(vernum[2]);
50 } else if (token == '[') {
52 while (format[i] != ']') {
56 } else if (token == ']') {
62 return new String(version.array(), 0, version.position());
65 public Revision parse (final String verstring) throws ParseException {
66 char[] format = _pattern.toCharArray();
67 char[] version = verstring.toCharArray();
68 CharBuffer major = CharBuffer.allocate(4);
69 CharBuffer minor = CharBuffer.allocate(4);
70 CharBuffer branch = CharBuffer.allocate(4);
72 int cursor = 0; // Index into version array
73 for (int i=0; i<format.length; i++) { // The parsed string may not include the branch ID
74 char token = format[i];
79 while (cursor < version.length) {
80 if (!Character.isDigit(version[cursor])) {
83 major.put(version[cursor]);
86 } else if (token == 'm') {
87 while (cursor < version.length) {
88 if (!Character.isDigit(version[cursor])) {
91 minor.put(version[cursor]);
94 } else if (token == 's') {
95 while (cursor < version.length) {
96 if (!Character.isDigit(version[cursor])) {
99 branch.put(version[cursor]);
103 } else if (token == '[' || token == ']') {
106 if (version[cursor] != token) {
107 throw new ParseException(verstring, cursor);
111 if (cursor >= version.length) {
115 if (major.position() == 0) {
116 throw new ParseException(verstring, 0);
119 String majnum = new String(major.array(), 0, major.position());
120 if (minor.position() == 0) {
121 return new Revision(Integer.valueOf(majnum), 0);
123 String minum = new String(minor.array(), 0, minor.position());
124 if (branch.position() == 0) {
125 return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum));
127 String branum = new String(branch.array(), 0, branch.position());
128 return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum), branum);
133 public String toPattern () {
138 // ==============================================================================================================================
140 // ==============================================================================================================================
142 * Constructs a Revision object from the internal representation of a version number (m.n.s).
144 public Revision (final String value) {
145 // ------------------------------
146 String[] vernum = value.split("\\x2E");
148 this._major = Integer.valueOf(vernum[0]);
149 this._minor = Integer.valueOf(vernum[1]);
150 this._branch = Integer.valueOf(vernum[2]);
152 catch (Exception e) { // NumberFormat or OutOfBound exception if value is not of the form m.n.s
159 // ------------------
164 private Revision (final int major, final int minor) {
165 // ---------------------------------------
170 private Revision (final int major, final int minor, final String branch) {
171 // ------------------------------------------------------
174 this._branch = Integer.valueOf(branch);
177 // ==============================================================================================================================
178 // Public member function
179 // ==============================================================================================================================
181 public Revision incrementAs (final ProgressState state) {
182 // -------------------------------------------------
183 if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) {
185 } else if (state == ProgressState.inCHECK) {
192 public boolean isGraterThan (final Revision base) {
193 // ------------------------------------------
194 if (this._major > base._major) {
197 if (this._major < base._major) {
200 return (this._minor > base._minor);
203 public boolean isMinor () {
204 // -------------------------
205 return (_minor != 0);
208 public boolean isNull () {
209 // ------------------------
210 return (_major+_minor == 0);
213 * Sets the branch name of this revision.
215 * @param name the branch name or the internal representation of a version number (m.n.s)
216 * @return this revision object
218 public Revision setBranch (final String name) {
219 // ---------------------------------------
220 String[] vernum = name.split("\\x2E");
222 _branch = Integer.valueOf(vernum[vernum.length-1]);
226 * Returns the internal representation of a version number (m.n.s) represented by this Revision object.
229 public String toString () {
230 // -------------------------
231 StringBuffer version = new StringBuffer();
232 return version.append(_major).append(".").append(_minor).append(".").append(_branch).toString();