Salome HOME
7ba7ed23ca529ce874545b839887fed8bb1b7b4e
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / Revision.java
1 package org.splat.som;
2 /**
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.
7  * 
8  * @see Revision.Format
9  * @author    Daniel Brunier-Coulin
10  * @copyright OPEN CASCADE 2012
11  */
12
13 import java.nio.CharBuffer;
14 import java.text.ParseException;
15
16 import org.splat.dal.bo.som.ProgressState;
17
18
19 public class Revision {
20
21     private transient int _major;
22     private transient int _minor;
23     private transient int _branch;
24
25     public static class Format {
26       private transient final String _pattern;
27
28       public Format (final String pattern) {
29         this._pattern = pattern;
30       }
31
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]);
37
38         for (int i=0; i<format.length; i++) {
39           char token = format[i];
40           if  (token == '%') {
41             i += 1;
42             token = format[i];
43             if        (token == 'M') {
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]);
49             }
50           } else if (token == '[') {
51                 if (branch == 0) {
52                                 while (format[i] != ']') {
53                                         i += 1;
54                                 }
55                         }
56           } else if (token == ']') {
57                 continue;
58           } else {
59             version.put(token);
60           }
61         }
62         return  new String(version.array(), 0, version.position());
63       }
64
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);
71
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];
75           if   (token == '%') {
76                 i += 1;
77                 token = format[i];
78             if        (token == 'M') {
79               while (cursor < version.length) {
80                 if (!Character.isDigit(version[cursor])) {
81                                         break;
82                                 }
83                 major.put(version[cursor]);
84                 cursor += 1;
85               }
86             } else if (token == 'm') {
87               while (cursor < version.length) {
88                 if (!Character.isDigit(version[cursor])) {
89                                         break;
90                                 }
91                 minor.put(version[cursor]);
92                 cursor += 1;
93               }
94             } else if (token == 's') {
95               while (cursor < version.length) {
96                 if (!Character.isDigit(version[cursor])) {
97                                         break;
98                                 }
99                 branch.put(version[cursor]);
100                 cursor += 1;
101               }
102             }
103           } else if (token == '[' || token == ']') {
104                 continue;
105           } else {
106             if (version[cursor] != token) {
107                                 throw new ParseException(verstring, cursor);
108                         }
109             cursor += 1;
110           }
111           if (cursor >= version.length) {
112                         break;
113                 }
114         }
115         if (major.position() == 0) {
116                         throw  new ParseException(verstring, 0);
117                 }
118
119         String  majnum = new String(major.array(), 0, major.position());
120         if (minor.position() == 0) {
121           return   new Revision(Integer.valueOf(majnum), 0);
122         } else {
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));
126           } else {
127                 String  branum = new String(branch.array(), 0, branch.position());
128             return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum), branum);
129           }
130         }
131       }
132
133       public String toPattern () {
134         return _pattern;
135       }
136     }
137
138 //  ==============================================================================================================================
139 //  Constructors
140 //  ==============================================================================================================================
141 /**
142  * Constructs a Revision object from the internal representation of a version number (m.n.s).
143  */
144     public Revision (final String value) {
145 //  ------------------------------
146       String[] vernum = value.split("\\x2E");
147       try {
148         this._major  = Integer.valueOf(vernum[0]);
149         this._minor  = Integer.valueOf(vernum[1]);
150         this._branch = Integer.valueOf(vernum[2]);
151       }
152       catch (Exception e) {     // NumberFormat or OutOfBound exception if value is not of the form m.n.s
153             this._major  = 0;
154             this._minor  = 0;
155             this._branch = 0;
156           }
157     }
158     public Revision () {
159 //  ------------------
160       this._major  = 0;
161       this._minor  = 0;
162       this._branch = 0;
163     }
164     private Revision (final int major, final int minor) {
165 //  ---------------------------------------
166       this._major  = major;
167       this._minor  = minor;
168       this._branch = 0;
169     }
170     private Revision (final int major, final int minor, final String branch) {
171 //  ------------------------------------------------------
172       this._major  = major;
173       this._minor  = minor;
174       this._branch = Integer.valueOf(branch);
175     }
176
177 //  ==============================================================================================================================
178 //  Public member function
179 //  ==============================================================================================================================
180
181     public Revision incrementAs (final ProgressState state) {
182 //  -------------------------------------------------
183       if      (state == ProgressState.inWORK || state == ProgressState.inDRAFT) {
184                 _minor += 1;
185         } else if (state == ProgressState.inCHECK || state == ProgressState.EXTERN) {
186         _major = _major + 1;
187         _minor = 0;
188       }
189       return this;
190     }
191
192     public boolean isGraterThan (final Revision base) {
193 //  ------------------------------------------
194       if (this._major > base._major) {
195                 return true;
196         }
197       if (this._major < base._major) {
198                 return false;
199         }
200       return (this._minor > base._minor);
201     }
202
203     public boolean isMinor () {
204 //  -------------------------
205       return (_minor != 0);
206     }
207
208     public boolean isNull () {
209 //  ------------------------
210       return (_major+_minor == 0);
211     }
212 /**
213  * Sets the branch name of this revision.
214  * 
215  * @param name the branch name or the internal representation of a version number (m.n.s)
216  * @return this revision object
217  */
218     public Revision setBranch (final String name) {
219 //  ---------------------------------------
220       String[] vernum = name.split("\\x2E");
221
222       _branch = Integer.valueOf(vernum[vernum.length-1]);
223       return this;
224     }
225 /**
226  * Returns the internal representation of a version number (m.n.s) represented by this Revision object.
227  */
228     @Override
229         public String toString () {
230 //  -------------------------
231       StringBuffer version = new StringBuffer();
232       return version.append(_major).append(".").append(_minor).append(".").append(_branch).toString();
233     }
234 }