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