Salome HOME
More business logic has been moved from BO to services. ServiceLocator is created...
[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  int major;
22     private  int minor;
23     private  int branch;
24
25     public static class Format {
26 //  --------------------------
27       private String pattern;
28
29       public Format (String pattern) {
30         this.pattern = pattern;
31       }
32
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]);
38
39         for (int i=0; i<format.length; i++) {
40           char token = format[i];
41           if  (token == '%') {
42             i += 1;
43             token = format[i];
44             if        (token == 'M') {
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]);
50             }
51           } else if (token == '[') {
52                 if (branch == 0) while (format[i] != ']') i += 1;
53           } else if (token == ']') {
54                 continue;
55           } else {
56             version.put(token);
57           }
58         }
59         return  new String(version.array(), 0, version.position());
60       }
61
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);
68
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];
72           if   (token == '%') {
73                 i += 1;
74                 token = format[i];
75             if        (token == 'M') {
76               while (cursor < version.length) {
77                 if (!Character.isDigit(version[cursor])) break;
78                 major.put(version[cursor]);
79                 cursor += 1;
80               }
81             } else if (token == 'm') {
82               while (cursor < version.length) {
83                 if (!Character.isDigit(version[cursor])) break;
84                 minor.put(version[cursor]);
85                 cursor += 1;
86               }
87             } else if (token == 's') {
88               while (cursor < version.length) {
89                 if (!Character.isDigit(version[cursor])) break;
90                 branch.put(version[cursor]);
91                 cursor += 1;
92               }
93             }
94           } else if (token == '[' || token == ']') {
95                 continue;
96           } else {
97             if (version[cursor] != token) throw new ParseException(verstring, cursor);
98             cursor += 1;
99           }
100           if (cursor >= version.length) break;
101         }
102         if (major.position() == 0) throw  new ParseException(verstring, 0);
103
104         String  majnum = new String(major.array(), 0, major.position());
105         if (minor.position() == 0) {
106           return   new Revision(Integer.valueOf(majnum), 0);
107         } else {
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));
111           } else {
112                 String  branum = new String(branch.array(), 0, branch.position());
113             return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum), branum);
114           }
115         }
116       }
117
118       public String toPattern () {
119         return pattern;
120       }
121     }
122
123 //  ==============================================================================================================================
124 //  Constructors
125 //  ==============================================================================================================================
126 /**
127  * Constructs a Revision object from the internal representation of a version number (m.n.s).
128  */
129     public Revision (String value) {
130 //  ------------------------------
131       String[] vernum = value.split("\\x2E");
132       try {
133         this.major  = Integer.valueOf(vernum[0]);
134         this.minor  = Integer.valueOf(vernum[1]);
135         this.branch = Integer.valueOf(vernum[2]);
136       }
137       catch (Exception e) {     // NumberFormat or OutOfBound exception if value is not of the form m.n.s
138             this.major  = 0;
139             this.minor  = 0;
140             this.branch = 0;
141           }
142     }
143     public Revision () {
144 //  ------------------
145       this.major  = 0;
146       this.minor  = 0;
147       this.branch = 0;
148     }
149     private Revision (int major, int minor) {
150 //  ---------------------------------------
151       this.major  = major;
152       this.minor  = minor;
153       this.branch = 0;
154     }
155     private Revision (int major, int minor, String branch) {
156 //  ------------------------------------------------------
157       this.major  = major;
158       this.minor  = minor;
159       this.branch = Integer.valueOf(branch);
160     }
161
162 //  ==============================================================================================================================
163 //  Public member function
164 //  ==============================================================================================================================
165
166     public Revision incrementAs (ProgressState state) {
167 //  -------------------------------------------------
168       if      (state == ProgressState.inWORK || state == ProgressState.inDRAFT) minor += 1;
169       else if (state == ProgressState.inCHECK) {
170         major = major + 1;
171         minor = 0;
172       }
173       return this;
174     }
175
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);
181     }
182
183     public boolean isMinor () {
184 //  -------------------------
185       return (minor != 0);
186     }
187
188     public boolean isNull () {
189 //  ------------------------
190       return (major+minor == 0);
191     }
192 /**
193  * Sets the branch name of this revision.
194  * 
195  * @param name the branch name or the internal representation of a version number (m.n.s)
196  * @return this revision object
197  */
198     public Revision setBranch (String name) {
199 //  ---------------------------------------
200       String[] vernum = name.split("\\x2E");
201
202       branch = Integer.valueOf(vernum[vernum.length-1]);
203       return this;
204     }
205 /**
206  * Returns the internal representation of a version number (m.n.s) represented by this Revision object.
207  */
208     public String toString () {
209 //  -------------------------
210       StringBuffer version = new StringBuffer();
211       return version.append(major).append(".").append(minor).append(".").append(branch).toString();
212     }
213 }