Salome HOME
Refactoring: kernel and som are moved to Siman-Common.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / Revision.java
diff --git a/Workspace/Siman-Common/src/org/splat/som/Revision.java b/Workspace/Siman-Common/src/org/splat/som/Revision.java
new file mode 100644 (file)
index 0000000..ea06e7f
--- /dev/null
@@ -0,0 +1,211 @@
+package org.splat.som;
+/**
+ * Class providing operations on version numbers such as incrementation and comparison.
+ * Revision objects are created from and converted to strings in the internal format of version numbers (major.minor.branch).<br/>
+ * Revision objects can also be created from and converted to strings in user-defined format, thanks to a formating tool
+ * provided by the Format nested class.
+ * 
+ * @see Revision.Format
+ * @author    Daniel Brunier-Coulin
+ * @copyright OPEN CASCADE 2012
+ */
+
+import java.nio.CharBuffer;
+import java.text.ParseException;
+
+
+public class Revision {
+
+    private  int major;
+    private  int minor;
+    private  int branch;
+
+    public static class Format {
+//  --------------------------
+      private String pattern;
+
+      public Format (String pattern) {
+        this.pattern = pattern;
+      }
+
+      public String format (String verstring) {
+       CharBuffer  version = CharBuffer.allocate(pattern.length() + 2*2);   // Maximum possible size
+        char[]      format  = pattern.toCharArray();
+        String[]    vernum  = verstring.split("\\x2E");                      // version is suposed of the internal form (m.n.s)
+        int         branch  = Integer.valueOf(vernum[2]);
+
+        for (int i=0; i<format.length; i++) {
+          char token = format[i];
+          if  (token == '%') {
+            i += 1;
+            token = format[i];
+            if        (token == 'M') {
+              version.put(vernum[0]);
+            } else if (token == 'm') {
+              version.put(vernum[1]);
+            } else if (token == 's') {
+              version.put(vernum[2]);
+            }
+          } else if (token == '[') {
+               if (branch == 0) while (format[i] != ']') i += 1;
+          } else if (token == ']') {
+               continue;
+          } else {
+            version.put(token);
+          }
+        }
+       return  new String(version.array(), 0, version.position());
+      }
+
+      public Revision parse (String verstring) throws ParseException {
+        char[]      format  = pattern.toCharArray();
+        char[]      version = verstring.toCharArray();
+        CharBuffer  major   = CharBuffer.allocate(4);
+        CharBuffer  minor   = CharBuffer.allocate(4);
+        CharBuffer  branch  = CharBuffer.allocate(4);
+
+        int  cursor = 0;                         // Index into version array
+        for (int i=0; i<format.length; i++) {    // The parsed string may not include the branch ID
+          char  token = format[i];
+          if   (token == '%') {
+               i += 1;
+               token = format[i];
+            if        (token == 'M') {
+              while (cursor < version.length) {
+                if (!Character.isDigit(version[cursor])) break;
+                major.put(version[cursor]);
+                cursor += 1;
+              }
+            } else if (token == 'm') {
+              while (cursor < version.length) {
+                if (!Character.isDigit(version[cursor])) break;
+                minor.put(version[cursor]);
+                cursor += 1;
+              }
+            } else if (token == 's') {
+              while (cursor < version.length) {
+                if (!Character.isDigit(version[cursor])) break;
+                branch.put(version[cursor]);
+                cursor += 1;
+              }
+            }
+          } else if (token == '[' || token == ']') {
+               continue;
+          } else {
+            if (version[cursor] != token) throw new ParseException(verstring, cursor);
+            cursor += 1;
+          }
+          if (cursor >= version.length) break;
+        }
+        if (major.position() == 0) throw  new ParseException(verstring, 0);
+
+        String  majnum = new String(major.array(), 0, major.position());
+        if (minor.position() == 0) {
+          return   new Revision(Integer.valueOf(majnum), 0);
+        } else {
+          String  minum = new String(minor.array(), 0, minor.position());
+          if (branch.position() == 0) {
+            return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum));
+          } else {
+               String  branum = new String(branch.array(), 0, branch.position());
+            return new Revision(Integer.valueOf(majnum), Integer.valueOf(minum), branum);
+          }
+        }
+      }
+
+      public String toPattern () {
+        return pattern;
+      }
+    }
+
+//  ==============================================================================================================================
+//  Constructors
+//  ==============================================================================================================================
+/**
+ * Constructs a Revision object from the internal representation of a version number (m.n.s).
+ */
+    public Revision (String value) {
+//  ------------------------------
+      String[] vernum = value.split("\\x2E");
+      try {
+        this.major  = Integer.valueOf(vernum[0]);
+        this.minor  = Integer.valueOf(vernum[1]);
+        this.branch = Integer.valueOf(vernum[2]);
+      }
+      catch (Exception e) {     // NumberFormat or OutOfBound exception if value is not of the form m.n.s
+           this.major  = 0;
+           this.minor  = 0;
+           this.branch = 0;
+         }
+    }
+    public Revision () {
+//  ------------------
+      this.major  = 0;
+      this.minor  = 0;
+      this.branch = 0;
+    }
+    private Revision (int major, int minor) {
+//  ---------------------------------------
+      this.major  = major;
+      this.minor  = minor;
+      this.branch = 0;
+    }
+    private Revision (int major, int minor, String branch) {
+//  ------------------------------------------------------
+      this.major  = major;
+      this.minor  = minor;
+      this.branch = Integer.valueOf(branch);
+    }
+
+//  ==============================================================================================================================
+//  Public member function
+//  ==============================================================================================================================
+
+    public Revision incrementAs (ProgressState state) {
+//  -------------------------------------------------
+      if      (state == ProgressState.inWORK || state == ProgressState.inDRAFT) minor += 1;
+      else if (state == ProgressState.inCHECK) {
+        major = major + 1;
+        minor = 0;
+      }
+      return this;
+    }
+
+    public boolean isGraterThan (Revision base) {
+//  ------------------------------------------
+      if (this.major > base.major) return true;
+      if (this.major < base.major) return false;
+      return (this.minor > base.minor);
+    }
+
+    public boolean isMinor () {
+//  -------------------------
+      return (minor != 0);
+    }
+
+    public boolean isNull () {
+//  ------------------------
+      return (major+minor == 0);
+    }
+/**
+ * Sets the branch name of this revision.
+ * 
+ * @param name the branch name or the internal representation of a version number (m.n.s)
+ * @return this revision object
+ */
+    public Revision setBranch (String name) {
+//  ---------------------------------------
+      String[] vernum = name.split("\\x2E");
+
+      branch = Integer.valueOf(vernum[vernum.length-1]);
+      return this;
+    }
+/**
+ * Returns the internal representation of a version number (m.n.s) represented by this Revision object.
+ */
+    public String toString () {
+//  -------------------------
+      StringBuffer version = new StringBuffer();
+      return version.append(major).append(".").append(minor).append(".").append(branch).toString();
+    }
+}
\ No newline at end of file