remove debug writelines
[mcs.git] / class / System / System.Runtime.Versioning / FrameworkName.cs
blob2c15444f72372e763d1f7c4890c34edee408e8b6
1 //
2 // System.Runtime.Versioning.FrameworkName class
3 //
4 // Authors
5 // Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2009 Novell, Inc (http://novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System;
29 using System.Text;
31 #if NET_4_0
32 namespace System.Runtime.Versioning
34 [Serializable]
35 public sealed class FrameworkName : IEquatable <FrameworkName>
37 string fullName;
38 int? hashCode;
40 public string FullName {
41 get {
42 if (fullName == null) {
43 var sb = new StringBuilder (Identifier);
44 sb.Append (",Version=v");
45 sb.Append (Version.ToString ());
47 string profile = Profile;
48 if (!String.IsNullOrEmpty (profile)) {
49 sb.Append (",Profile=");
50 sb.Append (profile);
53 fullName = sb.ToString ();
56 return fullName;
60 public string Identifier {
61 get; private set;
64 public string Profile {
65 get; private set;
68 public Version Version {
69 get; private set;
72 public FrameworkName (string frameworkName)
74 if (frameworkName == null)
75 throw new ArgumentNullException ("frameworkName");
77 if (frameworkName.Length == 0)
78 throw new ArgumentException ("The parameter 'frameworkName' cannot be an empty string.", "frameworkName");
80 this.Profile = String.Empty;
81 ParseFrameworkName (frameworkName);
84 public FrameworkName (string identifier, Version version)
85 : this (identifier, version, String.Empty)
89 public FrameworkName (string identifier, Version version, string profile)
91 if (identifier == null)
92 throw new ArgumentNullException ("identifier");
94 if (version == null)
95 throw new ArgumentNullException ("version");
97 if (identifier.Length == 0)
98 throw new ArgumentException ("The parameter 'identifier' cannot be an empty string.", "identifier");
100 this.Identifier = identifier;
101 this.Version = version;
102 if (profile == null)
103 this.Profile = String.Empty;
104 else
105 this.Profile = profile;
108 public bool Equals (FrameworkName other)
110 return (other.Version == this.Version &&
111 String.Compare (other.Identifier, this.Identifier, StringComparison.Ordinal) == 0 &&
112 String.Compare (other.Profile, this.Profile, StringComparison.Ordinal) == 0);
115 public override int GetHashCode ()
117 if (hashCode == null) {
118 hashCode = Version.GetHashCode () ^ Identifier.GetHashCode ();
119 string profile = Profile;
120 if (profile != null)
121 hashCode ^= profile.GetHashCode ();
124 return (int)hashCode;
127 public override string ToString ()
129 return FullName;
132 public static bool operator == (FrameworkName left, FrameworkName right)
134 if (((object)left) == null && ((object)right) == null)
135 return true;
137 if (((object)left) == null || ((object)right) == null)
138 return false;
140 return left.Equals (right);
143 public static bool operator != (FrameworkName left, FrameworkName right)
145 if (((object)left) == null && ((object)right) == null)
146 return false;
148 if (((object)left) == null || ((object)right) == null)
149 return true;
151 return !left.Equals (right);
154 void ParseFrameworkName (string frameworkName)
156 string[] parts = frameworkName.Split (',');
157 int len = parts.Length;
159 if (len < 2 || len > 3)
160 throw new ArgumentException ("FrameworkName cannot have less than two components or more than three components.");
162 bool invalid = false;
163 string part;
164 string[] splitPart;
165 int splen;
167 for (int i = 0; i < len; i++) {
168 part = parts [i].Trim ();
169 if (part.Length == 0) {
170 invalid = true;
171 break;
174 splitPart = part.Split ('=');
175 splen = splitPart.Length;
177 if (String.Compare ("version", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
178 if (i == 0 || splen != 2) {
179 invalid = true;
180 break;
183 try {
184 char first = splitPart [1][0];
185 if (first == 'v' || first == 'V')
186 splitPart [1] = splitPart [1].Substring (1);
187 this.Version = new Version (splitPart [1]);
188 } catch (Exception ex) {
189 throw new ArgumentException ("FrameworkName version component is invalid.", ex);
192 continue;
195 if (String.Compare ("profile", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
196 if (i == 0) {
197 invalid = true;
198 break;
201 if (splen > 1)
202 Profile = String.Join ("=", splitPart, 1, splen - 1);
204 continue;
207 if (i == 0) {
208 Identifier = part;
209 continue;
212 invalid = true;
213 break;
216 if (invalid)
217 throw new ArgumentException ("FrameworkName is invalid.");
219 if (Version == null)
220 throw new ArgumentException ("FrameworkName version component is missing.");
225 #endif