5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
12 public class Version : ICloneable, IComparable {
13 int major, minor, build, revision;
15 const int MAXINT = int.MaxValue;
17 public Version (string version)
20 string [] vals = version.Split (new Char [] {'.'});
24 major = int.Parse (vals [0]);
26 minor = int.Parse (vals [1]);
28 build = int.Parse (vals [2]);
30 revision = int.Parse (vals [3]);
33 public Version (int major, int minor)
38 this.revision = MAXINT;
41 public Version (int major, int minor, int build)
46 this.revision = MAXINT;
49 public Version (int major, int minor, int build, int revision)
54 this.revision = revision;
81 public object Clone ()
83 return new Version (major, minor, build, revision);
86 public int CompareTo (object version)
91 throw new ArgumentNullException ("version");
92 if (! (version is Version))
93 throw new ArgumentException ("version");
95 v = version as Version;
97 if (this.major > v.major)
99 else if (this.major < v.major)
102 if (this.minor > v.minor)
104 else if (this.minor < this.minor)
107 if (this.build > v.build)
109 else if (this.build < this.build)
112 // FIXME: Compare revision or build first?
113 if (this.revision > v.revision)
115 else if (this.revision < v.revision)
121 public override bool Equals (object obj)
126 throw new ArgumentNullException ("obj");
127 if (!(obj is Version))
132 if ((x.major == major) &&
133 (x.minor == minor) &&
134 (x.build == build) &&
135 (x.revision == revision))
140 public override int GetHashCode ()
142 return (revision << 24) | (build << 16) | (minor << 8) | major;
146 // Returns a stringified representation of the version, format:
147 // major.minor[.build[.revision]]
149 public override string ToString ()
151 string mm = major.ToString () + "." + minor.ToString ();
154 mm = mm + "." + build.ToString ();
155 if (revision != MAXINT)
156 mm = mm + "." + revision.ToString ();
162 // LAME: This API is lame, since there is no way of knowing
163 // how many fields a Version object has, it is unfair to throw
164 // an ArgumentException, but this is what the spec claims.
166 // ie, Version a = new Version (1, 2); a.ToString (3) should
167 // throw the expcetion.
169 public string ToString (int fields)
174 return major.ToString ();
176 return major.ToString () + "." + minor.ToString ();
179 throw new ArgumentException ("fields is larger than the number of components defined in this instance");
180 return major.ToString () + "." + minor.ToString () + "." +
184 if (build == MAXINT || revision == MAXINT)
185 throw new ArgumentException ("fields is larger than the number of components defined in this instance");
186 return major.ToString () + "." + minor.ToString () + "." +
187 build.ToString () + "." + revision.ToString ();
189 throw new ArgumentException ("Invalid fields parameter: " + fields.ToString());