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,
38 IComparable, IComparable<Version>
44 int _Major, _Minor, _Build, _Revision;
46 private const int UNDEFINED = -1;
48 private void CheckedSet (int defined, int major, int minor, int build, int revision)
50 // defined should be 2, 3 or 4
53 throw new ArgumentOutOfRangeException ("major");
58 throw new ArgumentOutOfRangeException ("minor");
63 this._Build = UNDEFINED;
64 this._Revision = UNDEFINED;
69 throw new ArgumentOutOfRangeException ("build");
74 this._Revision = UNDEFINED;
79 throw new ArgumentOutOfRangeException ("revision");
81 this._Revision = revision;
86 CheckedSet (2, 0, 0, -1, -1);
89 public Version (string version)
93 int major = -1, minor = -1, build = -1, revision = -1;
95 if (version == null) {
96 throw new ArgumentNullException ("version");
99 vals = version.Split (new Char [] {'.'});
102 if (n < 2 || n > 4) {
103 throw new ArgumentException (Locale.GetText
104 ("There must be 2, 3 or 4 components in the version string."));
108 major = int.Parse (vals [0]);
110 minor = int.Parse (vals [1]);
112 build = int.Parse (vals [2]);
114 revision = int.Parse (vals [3]);
116 CheckedSet (n, major, minor, build, revision);
119 public Version (int major, int minor)
121 CheckedSet (2, major, minor, 0, 0);
124 public Version (int major, int minor, int build)
126 CheckedSet (3, major, minor, build, 0);
129 public Version (int major, int minor, int build, int revision)
131 CheckedSet (4, major, minor, build, revision);
152 public int Revision {
158 public object Clone ()
161 return new Version (_Major, _Minor);
162 else if (_Revision == -1)
163 return new Version (_Major, _Minor, _Build);
165 return new Version (_Major, _Minor, _Build, _Revision);
168 public int CompareTo (object version)
173 if (! (version is Version))
174 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
176 return this.CompareTo ((Version) version);
179 public override bool Equals (object obj)
181 return this.Equals (obj as Version);
187 int CompareTo (Version v)
192 if (this._Major > v._Major)
194 else if (this._Major < v._Major)
197 if (this._Minor > v._Minor)
199 else if (this._Minor < v._Minor)
202 if (this._Build > v._Build)
204 else if (this._Build < v._Build)
207 if (this._Revision > v._Revision)
209 else if (this._Revision < v._Revision)
218 bool Equals (Version x)
220 return ((x != null) &&
221 (x._Major == _Major) &&
222 (x._Minor == _Minor) &&
223 (x._Build == _Build) &&
224 (x._Revision == _Revision));
227 public override int GetHashCode ()
229 return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
233 // Returns a stringified representation of the version, format:
234 // major.minor[.build[.revision]]
236 public override string ToString ()
238 string mm = _Major.ToString () + "." + _Minor.ToString ();
240 if (_Build != UNDEFINED)
241 mm = mm + "." + _Build.ToString ();
242 if (_Revision != UNDEFINED)
243 mm = mm + "." + _Revision.ToString ();
249 // LAME: This API is lame, since there is no way of knowing
250 // how many fields a Version object has, it is unfair to throw
251 // an ArgumentException, but this is what the spec claims.
253 // ie, Version a = new Version (1, 2); a.ToString (3) should
254 // throw the expcetion.
256 public string ToString (int fields)
261 return _Major.ToString ();
263 return _Major.ToString () + "." + _Minor.ToString ();
265 if (_Build == UNDEFINED)
266 throw new ArgumentException (Locale.GetText
267 ("fields is larger than the number of components defined in this instance."));
268 return _Major.ToString () + "." + _Minor.ToString () + "." +
272 if (_Build == UNDEFINED || _Revision == UNDEFINED)
273 throw new ArgumentException (Locale.GetText
274 ("fields is larger than the number of components defined in this instance."));
275 return _Major.ToString () + "." + _Minor.ToString () + "." +
276 _Build.ToString () + "." + _Revision.ToString ();
278 throw new ArgumentException (Locale.GetText ("Invalid fields parameter: ") + fields.ToString());
281 public static bool operator== (Version v1, Version v2)
283 return Equals (v1, v2);
286 public static bool operator!= (Version v1, Version v2)
288 return !Equals (v1, v2);
291 public static bool operator> (Version v1, Version v2)
293 return v1.CompareTo (v2) > 0;
296 public static bool operator>= (Version v1, Version v2)
298 return v1.CompareTo (v2) >= 0;
301 public static bool operator< (Version v1, Version v2)
303 return v1.CompareTo (v2) < 0;
306 public static bool operator<= (Version v1, Version v2)
308 return v1.CompareTo (v2) <= 0;
311 // a very gentle way to construct a Version object which takes
312 // the first four numbers in a string as the version
313 internal static Version CreateFromString (string info)
320 int number = UNDEFINED; // string may not begin with a digit
323 return new Version (0, 0, 0, 0);
325 for (int i=0; i < info.Length; i++) {
327 if (Char.IsDigit (c)) {
332 number = (number * 10) + (c - '0');
335 else if (number >= 0) {
354 // ignore end of string
376 return new Version (major, minor, build, revision);