2002-06-08 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / class / corlib / System / Version.cs
index e3919fa682e9857f7feeae8503cd597a4d7bf79d..616e7d5d0308f223ce67b5080138a87a4d744b4c 100644 (file)
@@ -7,19 +7,75 @@
 // (C) Ximian, Inc.  http://www.ximian.com
 //
 
+using System.Globalization;
+
 namespace System {
+       
+       [Serializable]
+       public sealed class Version : ICloneable, IComparable {
 
-       public class Version : ICloneable, IComparable {
                int major, minor, build, revision;
 
-               const int MAXINT = int.MaxValue;
+               private const int UNDEFINED = -1;
                
+               private void CheckedSet (int defined, int major, int minor, int build, int revision)
+               {
+                       // defined should be 2, 3 or 4
+
+                       if (major < 0) {
+                               throw new ArgumentOutOfRangeException ("major");
+                       }
+                       this.major = major;
+
+                       if (minor < 0) {
+                               throw new ArgumentOutOfRangeException ("minor");
+                       }
+                       this.minor = minor;
+
+                       if (defined == 2) {
+                               this.build = UNDEFINED;
+                               this.revision = UNDEFINED;
+                               return;
+                       }
+
+                       if (build < 0) {
+                               throw new ArgumentOutOfRangeException ("build");
+                       }
+                       this.build = build;
+
+                       if (defined == 3) {
+                               this.revision = UNDEFINED;
+                               return;
+                       }
+
+                       if (revision < 0) {
+                               throw new ArgumentOutOfRangeException ("revision");
+                       }
+                       this.revision = revision;
+               }
+
+               public Version ()
+               {
+                       CheckedSet (2, 0, 0, -1, -1);
+               }
+                       
                public Version (string version)
                {
                        int n;
-                       string [] vals = version.Split (new Char [] {'.'});
+                       string [] vals;
+                       int major = -1, minor = -1, build = -1, revision = -1;
                        
+                       if (version == null) {
+                               throw new ArgumentNullException ("version");
+                       }
+
+                       vals = version.Split (new Char [] {'.'});
                        n = vals.Length;
+
+                       if (n < 2 || n > 4) {
+                               throw new ArgumentException (Locale.GetText ("There must be 2, 3 or 4 components in the version string"));
+                       }
+       
                        if (n > 0)
                                major = int.Parse (vals [0]);
                        if (n > 1)
@@ -27,31 +83,24 @@ namespace System {
                        if (n > 2)
                                build = int.Parse (vals [2]);
                        if (n > 3)
-                               build = int.Parse (vals [3]);
+                               revision = int.Parse (vals [3]);
+
+                       CheckedSet (n, major, minor, build, revision);
                }
                
                public Version (int major, int minor)
                {
-                       this.major = major;
-                       this.minor = minor;
-                       this.build = MAXINT;
-                       this.revision = MAXINT;
+                       CheckedSet (2, major, minor, 0, 0);
                }
 
                public Version (int major, int minor, int build)
                {
-                       this.major = major;
-                       this.minor = minor;
-                       this.build = build;
-                       this.revision = MAXINT;
+                       CheckedSet (3, major, minor, build, 0);
                }
 
                public Version (int major, int minor, int build, int revision)
                {
-                       this.major = major;
-                       this.minor = minor;
-                       this.build = build;
-                       this.revision = revision;
+                       CheckedSet (4, major, minor, build, revision);
                }
 
                public int Build {
@@ -87,10 +136,15 @@ namespace System {
                {
                        Version v;
                        
+                       // LAMESPEC: Docs are unclear whether an 
+                       // ArgumentNullException should be thrown are 
+                       // that a value > 0 should be returned.
                        if (version == null)
-                               throw new ArgumentNullException ("version");
+                               return 1;
+                       //      throw new ArgumentNullException ("version");
+                       
                        if (! (version is Version))
-                               throw new ArgumentException ("version");
+                               throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version"));
 
                        v = version as Version;
 
@@ -101,15 +155,14 @@ namespace System {
 
                        if (this.minor > v.minor)
                                return 1;
-                       else if (this.minor < this.minor)
+                       else if (this.minor < v.minor)
                                return -1;
 
                        if (this.build > v.build)
                                return 1;
-                       else if (this.build < this.build)
+                       else if (this.build < v.build)
                                return -1;
 
-                       // FIXME: Compare revision or build first?
                        if (this.revision > v.revision)
                                return 1;
                        else if (this.revision < v.revision)
@@ -122,9 +175,7 @@ namespace System {
                {
                        Version x;
                        
-                       if (obj == null)
-                               throw new ArgumentNullException ("obj");
-                       if (!(obj is Version))
+                       if (obj == null || !(obj is Version))
                                return false;
 
                        x = (Version) obj;
@@ -137,11 +188,6 @@ namespace System {
                        return false;
                }
 
-               // <summary>
-               //   This is sort of lame: the documentation claims that the
-               //   return value is a 32-bit integer, but "int" can be 64
-               //   on Alphas for example. 
-               // </summary>
                public override int GetHashCode ()
                {
                        return (revision << 24) | (build << 16) | (minor << 8) | major;
@@ -155,9 +201,9 @@ namespace System {
                {
                        string mm = major.ToString () + "." + minor.ToString ();
                        
-                       if (build != MAXINT)
+                       if (build != UNDEFINED)
                                mm = mm + "." + build.ToString ();
-                       if (revision != MAXINT)
+                       if (revision != UNDEFINED)
                                mm = mm + "." + revision.ToString ();
 
                        return mm;
@@ -180,19 +226,50 @@ namespace System {
                        if (fields == 2)
                                return major.ToString () + "." + minor.ToString ();
                        if (fields == 3){
-                               if (build == MAXINT)
-                                       throw new ArgumentException ("fields is larger than the number of components defined in this instance");
+                               if (build == UNDEFINED)
+                                       throw new ArgumentException (Locale.GetText ("fields is larger than the number of components defined in this instance"));
                                return major.ToString () + "." + minor.ToString () + "." +
                                        build.ToString ();
                        }
                        if (fields == 4){
-                               if (build == MAXINT || revision == MAXINT)
-                                       throw new ArgumentException ("fields is larger than the number of components defined in this instance");
+                               if (build == UNDEFINED || revision == UNDEFINED)
+                                       throw new ArgumentException (Locale.GetText ("fields is larger than the number of components defined in this instance"));
                                return major.ToString () + "." + minor.ToString () + "." +
                                        build.ToString () + "." + revision.ToString ();
                        }
-                       throw new ArgumentException ("Invalid fields parameter: " + fields.ToString()); 
+                       throw new ArgumentException (Locale.GetText ("Invalid fields parameter: ") + fields.ToString());        
                }
+
+               public static bool operator== (Version v1, Version v2) 
+               {
+                       return v1.Equals (v2);
+               }
+
+               public static bool operator!= (Version v1, Version v2)
+               {
+                       return !v1.Equals (v2);
+               }
+
+               public static bool operator> (Version v1, Version v2)
+               {
+                       return v1.CompareTo (v2) > 0;
+               }
+
+               public static bool operator>= (Version v1, Version v2)
+               {
+                       return v1.CompareTo (v2) >= 0;
+               }
+
+               public static bool operator< (Version v1, Version v2)
+               {
+                       return v1.CompareTo (v2) < 0;
+               }
+
+               public static bool operator<= (Version v1, Version v2)
+               {
+                       return v1.CompareTo (v2) <= 0;
+               }
+
        }
 }