File: FrameworkName.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (222 lines) | stat: -rw-r--r-- 8,300 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Text;

namespace System.Runtime.Versioning {
#if !SILVERLIGHT
    [Serializable]
#endif
    public sealed class FrameworkName : IEquatable<FrameworkName> {

        // ---- SECTION:  members supporting exposed properties -------------*
        #region members supporting exposed properties
        readonly String m_identifier = null;
        readonly Version m_version = null;
        readonly String m_profile = null;
        String m_fullName = null;

        const Char c_componentSeparator = ',';
        const Char c_keyValueSeparator = '=';
        const Char c_versionValuePrefix = 'v';
        const String c_versionKey = "Version";
        const String c_profileKey = "Profile";
        #endregion members supporting exposed properties


        // ---- SECTION: public properties --------------*
        #region public properties
        public String Identifier {
            get {
                Contract.Assert(m_identifier != null);
                return m_identifier;
            }
        }

        public Version Version {
            get {
                Contract.Assert(m_version != null);
                return m_version;
            }
        }

        public String Profile {
            get {
                Contract.Assert(m_profile != null);
                return m_profile;
            }
        }

        public String FullName {
            get {
                if (m_fullName == null) {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(Identifier);
                    sb.Append(c_componentSeparator);
                    sb.Append(c_versionKey).Append(c_keyValueSeparator);
                    sb.Append(c_versionValuePrefix);
                    sb.Append(Version);
                    if (!String.IsNullOrEmpty(Profile)) {
                        sb.Append(c_componentSeparator);
                        sb.Append(c_profileKey).Append(c_keyValueSeparator);
                        sb.Append(Profile);
                    }                    
                    m_fullName = sb.ToString();
                }
                Contract.Assert(m_fullName != null);
                return m_fullName;
            }
        }
        #endregion public properties


        // ---- SECTION: public instance methods --------------*
        #region public instance methods

        public override Boolean Equals(Object obj) {
            return Equals(obj as FrameworkName);
        }

        public Boolean Equals(FrameworkName other) {
            if (Object.ReferenceEquals(other, null)) {
                return false;
            }

            return Identifier == other.Identifier &&
                Version == other.Version &&
                Profile == other.Profile;
        }

        public override Int32 GetHashCode() {
            return Identifier.GetHashCode() ^ Version.GetHashCode() ^ Profile.GetHashCode();
        }

        public override String ToString() {
            return FullName;
        }
        #endregion public instance methods


        // -------- SECTION: constructors -----------------*
        #region constructors

        public FrameworkName(String identifier, Version version)
            : this(identifier, version, null) {}

        public FrameworkName(String identifier, Version version, String profile) {
            if (identifier == null) {
                throw new ArgumentNullException("identifier");
            }
            if (identifier.Trim().Length == 0) {
                throw new ArgumentException(SR.GetString(SR.net_emptystringcall, "identifier"), "identifier");
            }
            if (version == null) {
                throw new ArgumentNullException("version");
            }
            Contract.EndContractBlock();

            m_identifier = identifier.Trim();
            m_version = (Version)version.Clone();
            m_profile = (profile == null) ? String.Empty : profile.Trim();
        }

        // Parses strings in the following format: "<identifier>, Version=[v|V]<version>, Profile=<profile>"
        //  - The identifier and version is required, profile is optional
        //  - Only three components are allowed.
        //  - The version string must be in the System.Version format; an optional "v" or "V" prefix is allowed
        public FrameworkName(String frameworkName) {
            if (frameworkName == null) {
                throw new ArgumentNullException("frameworkName");
            }
            if (frameworkName.Length == 0) {
                throw new ArgumentException(SR.GetString(SR.net_emptystringcall, "frameworkName"), "frameworkName");
            }
            Contract.EndContractBlock();

            string[] components = frameworkName.Split(c_componentSeparator);

            // Identifer and Version are required, Profile is optional.
            if (components.Length < 2 || components.Length > 3) {
                throw new ArgumentException(SR.GetString(SR.Argument_FrameworkNameTooShort), "frameworkName");
            }

            //
            // 1) Parse the "Identifier", which must come first. Trim any whitespace
            //
            m_identifier = components[0].Trim();

            if (m_identifier.Length == 0) {
                    throw new ArgumentException(SR.GetString(SR.Argument_FrameworkNameInvalid), "frameworkName");
            }          

            bool versionFound = false;
            m_profile = String.Empty;

            // 
            // The required "Version" and optional "Profile" component can be in any order
            //
            for (int i = 1; i < components.Length; i++) {
                // Get the key/value pair separated by '='
                string[] keyValuePair = components[i].Split(c_keyValueSeparator);

                if (keyValuePair.Length != 2) {
                    throw new ArgumentException(SR.GetString(SR.Argument_FrameworkNameInvalid), "frameworkName");
                }

                // Get the key and value, trimming any whitespace
                string key = keyValuePair[0].Trim();
                string value = keyValuePair[1].Trim();

                //
                // 2) Parse the required "Version" key value
                //
                if (key.Equals(c_versionKey, StringComparison.OrdinalIgnoreCase)) {
                    versionFound = true;

                    // Allow the version to include a 'v' or 'V' prefix...
                    if (value.Length > 0 && (value[0] == c_versionValuePrefix || value[0] == 'V')) {
                        value = value.Substring(1);
                    }
                    try {
                        m_version = new Version(value);
                    }
                    catch (Exception e) {
                        throw new ArgumentException(SR.GetString(SR.Argument_FrameworkNameInvalidVersion), "frameworkName", e);
                    }
                }
                //
                // 3) Parse the optional "Profile" key value
                //
                else if (key.Equals(c_profileKey, StringComparison.OrdinalIgnoreCase)) {
                    if (!String.IsNullOrEmpty(value)) {
                        m_profile = value;
                    }
                }
                else {
                    throw new ArgumentException(SR.GetString(SR.Argument_FrameworkNameInvalid), "frameworkName");
                }
            }

            if (!versionFound) {
                throw new ArgumentException(SR.GetString(SR.Argument_FrameworkNameMissingVersion), "frameworkName");
            }
        }
        #endregion constructors


        // -------- SECTION: public static methods -----------------*
        #region public static methods
        public static Boolean operator ==(FrameworkName left, FrameworkName right) {
            if (Object.ReferenceEquals(left, null)) {
                return Object.ReferenceEquals(right, null);
            }
            return left.Equals(right);
        }

        public static Boolean operator !=(FrameworkName left, FrameworkName right) {
            return !(left == right);
        }
        #endregion public static methods
    }
}