Use the right socket for the data stream
[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                         if (Object.ReferenceEquals (other, null))
111                                 return false;
112
113                         return (other.Version == this.Version &&
114                                 String.Compare (other.Identifier, this.Identifier, StringComparison.Ordinal) == 0 &&
115                                 String.Compare (other.Profile, this.Profile, StringComparison.Ordinal) == 0);
116                 }
117
118                 public override bool Equals (object obj)
119                 {
120                         return Equals (obj as FrameworkName);
121                 }
122
123                 public override int GetHashCode ()
124                 {
125                         if (hashCode == null) {
126                                 hashCode = Version.GetHashCode () ^ Identifier.GetHashCode ();
127                                 string profile = Profile;
128                                 if (profile != null)
129                                         hashCode ^= profile.GetHashCode ();
130                         }
131                         
132                         return (int)hashCode;
133                 }
134
135                 public override string ToString ()
136                 {
137                         return FullName;
138                 }
139
140                 public static bool operator == (FrameworkName left, FrameworkName right)
141                 {
142                         if (((object)left) == null && ((object)right) == null)
143                                 return true;
144
145                         if (((object)left) == null || ((object)right) == null)
146                                 return false;
147
148                         return left.Equals (right);
149                 }
150
151                 public static bool operator != (FrameworkName left, FrameworkName right)
152                 {
153                         if (((object)left) == null && ((object)right) == null)
154                                 return false;
155
156                         if (((object)left) == null || ((object)right) == null)
157                                 return true;
158
159                         return !left.Equals (right);
160                 }
161                 
162                 void ParseFrameworkName (string frameworkName)
163                 {
164                         string[] parts = frameworkName.Split (',');
165                         int len = parts.Length;
166
167                         if (len < 2 || len > 3)
168                                 throw new ArgumentException ("FrameworkName cannot have less than two components or more than three components.");
169
170                         bool invalid = false;
171                         string part;
172                         string[] splitPart;
173                         int splen;
174                         
175                         for (int i = 0; i < len; i++) {
176                                 part = parts [i].Trim ();
177                                 if (part.Length == 0) {
178                                         invalid = true;
179                                         break;
180                                 }
181
182                                 splitPart = part.Split ('=');
183                                 splen = splitPart.Length;
184                                 
185                                 if (String.Compare ("version", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
186                                         if (i == 0 || splen != 2) {
187                                                 invalid = true;
188                                                 break;
189                                         }                                       
190
191                                         try {
192                                                 char first = splitPart [1][0];
193                                                 if (first == 'v' || first == 'V')
194                                                         splitPart [1] = splitPart [1].Substring (1);
195                                                 this.Version = new Version (splitPart [1]);
196                                         } catch (Exception ex) {
197                                                 throw new ArgumentException ("FrameworkName version component is invalid.", ex);
198                                         }                                       
199
200                                         continue;
201                                 }
202
203                                 if (String.Compare ("profile", splitPart [0], StringComparison.OrdinalIgnoreCase) == 0) {
204                                         if (i == 0) {
205                                                 invalid = true;
206                                                 break;
207                                         }
208
209                                         if (splen > 1)
210                                                 Profile = String.Join ("=", splitPart, 1, splen - 1);
211                                         
212                                         continue;
213                                 }
214
215                                 if (i == 0) {
216                                         Identifier = part;
217                                         continue;
218                                 }
219
220                                 invalid = true;
221                                 break;
222                         }
223
224                         if (invalid)
225                                 throw new ArgumentException ("FrameworkName is invalid.");
226
227                         if (Version == null)
228                                 throw new ArgumentException ("FrameworkName version component is missing.");
229                         
230                 }
231         }
232 }
233 #endif