5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 public sealed class Version : ICloneable, IComparable
38 , IComparable<Version>, IEquatable <Version>
42 int _Major, _Minor, _Build, _Revision;
44 private const int UNDEFINED = -1;
46 private void CheckedSet (int defined, int major, int minor, int build, int revision)
48 // defined should be 2, 3 or 4
51 throw new ArgumentOutOfRangeException ("major");
56 throw new ArgumentOutOfRangeException ("minor");
61 this._Build = UNDEFINED;
62 this._Revision = UNDEFINED;
67 throw new ArgumentOutOfRangeException ("build");
72 this._Revision = UNDEFINED;
77 throw new ArgumentOutOfRangeException ("revision");
79 this._Revision = revision;
84 CheckedSet (2, 0, 0, -1, -1);
87 public Version (string version)
91 int major = -1, minor = -1, build = -1, revision = -1;
93 if (version == null) {
94 throw new ArgumentNullException ("version");
97 vals = version.Split (new Char [] {'.'});
100 if (n < 2 || n > 4) {
101 throw new ArgumentException (Locale.GetText
102 ("There must be 2, 3 or 4 components in the version string."));
106 major = int.Parse (vals [0]);
108 minor = int.Parse (vals [1]);
110 build = int.Parse (vals [2]);
112 revision = int.Parse (vals [3]);
114 CheckedSet (n, major, minor, build, revision);
117 public Version (int major, int minor)
119 CheckedSet (2, major, minor, 0, 0);
122 public Version (int major, int minor, int build)
124 CheckedSet (3, major, minor, build, 0);
127 public Version (int major, int minor, int build, int revision)
129 CheckedSet (4, major, minor, build, revision);
150 public int Revision {
156 public object Clone ()
159 return new Version (_Major, _Minor);
160 else if (_Revision == -1)
161 return new Version (_Major, _Minor, _Build);
163 return new Version (_Major, _Minor, _Build, _Revision);
166 public int CompareTo (object version)
171 if (! (version is Version))
172 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
174 return this.CompareTo ((Version) version);
177 public override bool Equals (object obj)
179 return this.Equals (obj as Version);
185 int CompareTo (Version v)
190 if (this._Major > v._Major)
192 else if (this._Major < v._Major)
195 if (this._Minor > v._Minor)
197 else if (this._Minor < v._Minor)
200 if (this._Build > v._Build)
202 else if (this._Build < v._Build)
205 if (this._Revision > v._Revision)
207 else if (this._Revision < v._Revision)
216 bool Equals (Version x)
218 return ((x != null) &&
219 (x._Major == _Major) &&
220 (x._Minor == _Minor) &&
221 (x._Build == _Build) &&
222 (x._Revision == _Revision));
225 public override int GetHashCode ()
227 return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
231 // Returns a stringified representation of the version, format:
232 // major.minor[.build[.revision]]
234 public override string ToString ()
236 string mm = _Major.ToString () + "." + _Minor.ToString ();
238 if (_Build != UNDEFINED)
239 mm = mm + "." + _Build.ToString ();
240 if (_Revision != UNDEFINED)
241 mm = mm + "." + _Revision.ToString ();
247 // LAME: This API is lame, since there is no way of knowing
248 // how many fields a Version object has, it is unfair to throw
249 // an ArgumentException, but this is what the spec claims.
251 // ie, Version a = new Version (1, 2); a.ToString (3) should
252 // throw the expcetion.
254 public string ToString (int fields)
259 return _Major.ToString ();
261 return _Major.ToString () + "." + _Minor.ToString ();
263 if (_Build == UNDEFINED)
264 throw new ArgumentException (Locale.GetText
265 ("fields is larger than the number of components defined in this instance."));
266 return _Major.ToString () + "." + _Minor.ToString () + "." +
270 if (_Build == UNDEFINED || _Revision == UNDEFINED)
271 throw new ArgumentException (Locale.GetText
272 ("fields is larger than the number of components defined in this instance."));
273 return _Major.ToString () + "." + _Minor.ToString () + "." +
274 _Build.ToString () + "." + _Revision.ToString ();
276 throw new ArgumentException (Locale.GetText ("Invalid fields parameter: ") + fields.ToString());
279 public static bool operator== (Version v1, Version v2)
281 return Equals (v1, v2);
284 public static bool operator!= (Version v1, Version v2)
286 return !Equals (v1, v2);
289 public static bool operator> (Version v1, Version v2)
291 return v1.CompareTo (v2) > 0;
294 public static bool operator>= (Version v1, Version v2)
296 return v1.CompareTo (v2) >= 0;
299 public static bool operator< (Version v1, Version v2)
301 return v1.CompareTo (v2) < 0;
304 public static bool operator<= (Version v1, Version v2)
306 return v1.CompareTo (v2) <= 0;
309 // a very gentle way to construct a Version object which takes
310 // the first four numbers in a string as the version
311 internal static Version CreateFromString (string info)
318 int number = UNDEFINED; // string may not begin with a digit
321 return new Version (0, 0, 0, 0);
323 for (int i=0; i < info.Length; i++) {
325 if (Char.IsDigit (c)) {
330 number = (number * 10) + (c - '0');
333 else if (number >= 0) {
352 // ignore end of string
374 return new Version (major, minor, build, revision);