remove debug writelines
[mono.git] / mcs / class / System / System.Runtime.Versioning / FrameworkName.cs
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.
27 //
28 using System;
29 using System.Text;
30
31 #if NET_4_0
32 namespace System.Runtime.Versioning
33 {
34         [Serializable]
35         public sealed class FrameworkName : IEquatable <FrameworkName>
36         {               
37                 string fullName;
38                 int? hashCode;
39                 
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 ());
46
47                                         string profile = Profile;
48                                         if (!String.IsNullOrEmpty (profile)) {
49                                                 sb.Append (",Profile=");
50                                                 sb.Append (profile);
51                                         }
52
53                                         fullName = sb.ToString ();
54                                 }
55
56                                 return fullName;
57                         }
58                 }
59                 
60                 public string Identifier {
61                         get; private set;
62                 }
63
64                 public string Profile {
65                         get; private set;
66                 }
67                 
68                 public Version Version {
69                         get; private set;
70                 }
71                 
72                 public FrameworkName (string frameworkName)
73                 {
74                         if (frameworkName == null)
75                                 throw new ArgumentNullException ("frameworkName");
76
77                         if (frameworkName.Length == 0)
78                                 throw new ArgumentException ("The parameter 'frameworkName' cannot be an empty string.", "frameworkName");
79
80                         this.Profile = String.Empty;
81                         ParseFrameworkName (frameworkName);
82                 }
83
84                 public FrameworkName (string identifier, Version version)
85                         : this (identifier, version, String.Empty)
86                 {
87                 }
88
89                 public FrameworkName (string identifier, Version version, string profile)
90                 {
91                         if (identifier == null)
92                                 throw new ArgumentNullException ("identifier");
93
94                         if (version == null)
95                                 throw new ArgumentNullException ("version");
96
97                         if (identifier.Length == 0)
98                                 throw new ArgumentException ("The parameter 'identifier' cannot be an empty string.", "identifier");
99                         
100                         this.Identifier = identifier;
101                         this.Version = version;
102                         if (profile == null)
103                                 this.Profile = String.Empty;
104                         else
105                                 this.Profile = profile;
106                 }
107
108                 public bool Equals (FrameworkName other)
109                 {
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);
113                 }
114
115                 public override int GetHashCode ()
116                 {
117                         if (hashCode == null) {
118                                 hashCode = Version.GetHashCode () ^ Identifier.GetHashCode ();
119                                 string profile = Profile;
120                                 if (profile != null)
121                                         hashCode ^= profile.GetHashCode ();
122                         }
123                         
124                         return (int)hashCode;
125                 }
126
127                 public override string ToString ()
128                 {
129                         return FullName;
130                 }
131
132                 public static bool operator == (FrameworkName left, FrameworkName right)
133                 {
134                         if (((object)left) == null && ((object)right) == null)
135                                 return true;
136
137                         if (((object)left) == null || ((object)right) == null)
138                                 return false;
139
140                         return left.Equals (right);
141                 }
142
143                 public static bool operator != (FrameworkName left, FrameworkName right)
144                 {
145                         if (((object)left) == null && ((object)right) == null)
146                                 return false;
147
148                         if (((object)left) == null || ((object)right) == null)
149                                 return true;
150
151                         return !left.Equals (right);
152                 }
153                 
154                 void ParseFrameworkName (string frameworkName)
155                 {
156                         string[] parts = frameworkName.Split (',');
157                         int len = parts.Length;
158
159                         if (len < 2 || len > 3)
160                                 throw new ArgumentException ("FrameworkName cannot have less than two components or more than three components.");
161
162                         bool invalid = false;
163                         string part;
164                         string[] splitPart;
165                         int splen;
166                         
167                         for (int i = 0; i < len; i++) {
168                                 part = parts [i].Trim ();
169                                 if (part.Length == 0) {
170                                         invalid = true;
171                                         break;
172                                 }
173
174                                 splitPart = part.Split ('=');
175                                 splen = splitPart.Length;
176                                 
177                                 if (String.Compare ("version", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
178                                         if (i == 0 || splen != 2) {
179                                                 invalid = true;
180                                                 break;
181                                         }                                       
182
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);
190                                         }                                       
191
192                                         continue;
193                                 }
194
195                                 if (String.Compare ("profile", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
196                                         if (i == 0) {
197                                                 invalid = true;
198                                                 break;
199                                         }
200
201                                         if (splen > 1)
202                                                 Profile = String.Join ("=", splitPart, 1, splen - 1);
203                                         
204                                         continue;
205                                 }
206
207                                 if (i == 0) {
208                                         Identifier = part;
209                                         continue;
210                                 }
211
212                                 invalid = true;
213                                 break;
214                         }
215
216                         if (invalid)
217                                 throw new ArgumentException ("FrameworkName is invalid.");
218
219                         if (Version == null)
220                                 throw new ArgumentException ("FrameworkName version component is missing.");
221                         
222                 }
223         }
224 }
225 #endif