Version 1.3.4
[nikiroo-utils.git] / src / be / nikiroo / utils / Version.java
index 550c5e9dfc156f2e11b30b9d461955ee3d6e6ee3..94387373e980daf30b3ca4de01cfa72e490a0307 100644 (file)
@@ -9,12 +9,19 @@ import java.io.InputStream;
  * 
  * @author niki
  */
-public class Version {
+public class Version implements Comparable<Version> {
        private String version;
        private int major;
        private int minor;
        private int patch;
 
+       /**
+        * Create a new, empty {@link Version}.
+        * 
+        */
+       public Version() {
+       }
+
        /**
         * Create a new {@link Version} with the given values.
         * 
@@ -40,16 +47,17 @@ public class Version {
         *            the version (<tt>MAJOR.MINOR.PATCH</tt>)
         */
        public Version(String version) {
-               this.version = version;
                try {
                        String[] tab = version.split("\\.");
                        this.major = Integer.parseInt(tab[0]);
                        this.minor = Integer.parseInt(tab[1]);
                        this.patch = Integer.parseInt(tab[2]);
+                       this.version = version;
                } catch (Exception e) {
                        this.major = 0;
                        this.minor = 0;
                        this.patch = 0;
+                       this.version = null;
                }
        }
 
@@ -95,12 +103,46 @@ public class Version {
         * <p>
         * An empty {@link Version} is always <tt>0.0.0</tt>.
         * 
-        * @return TRUS if it is empty
+        * @return TRUE if it is empty
         */
        public boolean isEmpty() {
                return major == 0 && minor == 0 && patch == 0;
        }
 
+       /**
+        * Check if we are more recent than the given {@link Version}.
+        * 
+        * @param o
+        *            the other {@link Version}
+        * @return TRUE if this {@link Version} is more recent than the given one
+        */
+       public boolean isNewerThan(Version o) {
+               if (major > o.major) {
+                       return true;
+               }
+
+               if (major == o.major && minor > o.minor) {
+                       return true;
+               }
+
+               if (major == o.major && minor == o.minor && patch > o.patch) {
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Check if we are older than the given {@link Version}.
+        * 
+        * @param o
+        *            the other {@link Version}
+        * @return TRUE if this {@link Version} is older than the given one
+        */
+       public boolean isOlderThan(Version o) {
+               return !equals(o) && !isNewerThan(o);
+       }
+
        /**
         * Return the version of the running program if it follows the VERSION
         * convention (i.e., if it has a file called VERSION containing the version
@@ -112,7 +154,7 @@ public class Version {
         * @return the {@link Version} of the program, or an empty {@link Version}
         *         (does not return NULL)
         */
-       public static String getCurrentVersion() {
+       public static Version getCurrentVersion() {
                String version = null;
 
                InputStream in = IOUtils.openResource("VERSION");
@@ -127,7 +169,32 @@ public class Version {
                        }
                }
 
-               return version;
+               return new Version(version);
+       }
+
+       public int compareTo(Version o) {
+               if (equals(o)) {
+                       return 0;
+               } else if (isNewerThan(o)) {
+                       return 1;
+               } else {
+                       return -1;
+               }
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (obj instanceof Version) {
+                       Version o = (Version) obj;
+                       return o.major == major && o.minor == minor && o.patch == patch;
+               }
+
+               return false;
+       }
+
+       @Override
+       public int hashCode() {
+               return version == null ? 0 : version.hashCode();
        }
 
        /**