File: MemberCollection.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 (347 lines) | stat: -rw-r--r-- 14,953 bytes parent folder | download | duplicates (6)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//---------------------------------------------------------------------
// <copyright file="MemberCollection.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.Reflection;
using System.Text;
using System.Diagnostics;
using System.Globalization;

namespace System.Data.Metadata.Edm
{
    /// <summary>
    /// Class representing a collection of member objects
    /// </summary>
    internal sealed class MemberCollection : MetadataCollection<EdmMember>
    {
        // This way this collection works is that it has storage for members on the current type and access to
        // members in the base types. As of that requirement, MemberCollection has a reference back to the declaring
        // type that owns this collection. Whenever MemberCollection is asked to do a look by name, it looks at the
        // current collection, if it doesn't find it, then it ask for it from the declaring type's base type's
        // MemberCollection. Because of this order, members in derived types hide members in the base type if they
        // have the same name. For look up by index, base type members have lower index then current type's members.
        // Add/Update/Remove operations on this collection is only allowed for members owned by this MemberCollection
        // and not allowed for members owned by MemberCollections in the base types. For example, if the caller tries
        // to remove a member by ordinal which is within the base type's member ordinal range, it throws an exception.
        // Hence, base type members are in a sense "readonly" to this MemberCollection. When enumerating all the
        // members, the enumeration starts from members in the root type in the inheritance chain. With this special
        // enumeration requirement, we have a specialized enumerator class for this MemberCollection. See the
        // Enumerator class for details on how it works.

        #region Constructors
        /// <summary>
        /// Default constructor for constructing an empty collection
        /// </summary>
        /// <param name="declaringType">The type that has this member collection</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the declaring type is null</exception>
        public MemberCollection(StructuralType declaringType)
            : this(declaringType, null)
        {
        }

        /// <summary>
        /// The constructor for constructing the collection with the given items
        /// </summary>
        /// <param name="declaringType">The type that has this member collection</param>
        /// <param name="items">The items to populate the collection</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the declaring type is null</exception>
        public MemberCollection(StructuralType declaringType, IEnumerable<EdmMember> items)
            : base(items)
        {
            Debug.Assert(declaringType != null, "This member collection must belong to a declaring type");
            _declaringType = declaringType;
        }
        #endregion

        #region Fields
        private StructuralType _declaringType;
        #endregion

        #region Properties
        /// <summary>
        /// Returns the collection as a readonly collection
        /// </summary>
        public override System.Collections.ObjectModel.ReadOnlyCollection<EdmMember> AsReadOnly
        {
            get
            {
                return new System.Collections.ObjectModel.ReadOnlyCollection<EdmMember>(this);
            }
        }

        /// <summary>
        /// Gets the count on the number of items in the collection
        /// </summary>
        public override int Count
        {
            get
            {
                return GetBaseTypeMemberCount() + base.Count;
            }
        }

        /// <summary>
        /// Gets an item from the collection with the given index
        /// </summary>
        /// <param name="index">The index to search for</param>
        /// <returns>An item from the collection</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the index is out of the range for the Collection</exception>
        /// <exception cref="System.InvalidOperationException">Always thrown on setter</exception>
        public override EdmMember this[int index]
        {
            get
            {
                int relativeIndex = GetRelativeIndex(index);
                if (relativeIndex < 0)
                {
                    // This means baseTypeMemberCount must be non-zero, so we can safely cast the base type to StructuralType
                    return ((StructuralType)_declaringType.BaseType).Members[index];
                }

                return base[relativeIndex];
            }
            set
            {
                throw EntityUtil.OperationOnReadOnlyCollection();
            }
        }

        /// <summary>
        /// Gets an item from the collection with the given identity
        /// </summary>
        /// <param name="identity">The identity of the item to search for</param>
        /// <returns>An item from the collection</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if identity argument passed in is null</exception>
        /// <exception cref="System.ArgumentException">Thrown if the Collection does not have an item with the given identity</exception>
        /// <exception cref="System.InvalidOperationException">Always thrown on setter</exception>
        public override EdmMember this[string identity]
        {
            get
            {
                return GetValue(identity, false);
            }
            set
            {
                throw EntityUtil.OperationOnReadOnlyCollection();
            }
        }

        /// <summary>
        /// Adds an item to the collection 
        /// </summary>
        /// <param name="member">The item to add to the list</param>
        /// <exception cref="System.ArgumentNullException">Thrown if member argument is null</exception>
        /// <exception cref="System.InvalidOperationException">Thrown if the member passed in or the collection itself instance is in ReadOnly state</exception>
        /// <exception cref="System.ArgumentException">Thrown if the member that is being added already belongs to another MemberCollection</exception>
        /// <exception cref="System.ArgumentException">Thrown if the MemberCollection already contains a member with the same identity</exception>
        public override void Add(EdmMember member)
        {
            // Make sure the member is valid for the add operation. 
            ValidateMemberForAdd(member, "member");
            
            base.Add(member);

            // Fix up the declaring type
            member.ChangeDeclaringTypeWithoutCollectionFixup(_declaringType);
        }

        /// <summary>
        /// Determines if this collection contains an item of the given identity
        /// </summary>
        /// <param name="identity">The identity of the item to check for</param>
        /// <returns>True if the collection contains the item with the given identity</returns>
        public override bool ContainsIdentity(string identity)
        {
            if (base.ContainsIdentity(identity))
            {
                return true;
            }

            // The item is not in this collection, check the base type member collection
            EdmType baseType = _declaringType.BaseType;
            if (baseType != null && ((StructuralType)baseType).Members.Contains(identity))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Find the index of an item
        /// </summary>
        /// <param name="item">The item whose index is to be looked for</param>
        /// <returns>The index of the found item, -1 if not found</returns>
        public override int IndexOf(EdmMember item)
        {
            // Try to get it from this collection, if found, then the relative index needs to be added with the number
            // of members in the base type to get the absolute index
            int relativeIndex = base.IndexOf(item);
            if (relativeIndex != -1)
            {
                return relativeIndex + GetBaseTypeMemberCount();
            }

            // Try to find it in the base type
            StructuralType baseType = _declaringType.BaseType as StructuralType;
            if (baseType != null)
            {
                return baseType.Members.IndexOf(item);
            }

            return -1;
        }

        /// <summary>
        /// Copies the items in this collection to an array
        /// </summary>
        /// <param name="array">The array to copy to</param>
        /// <param name="arrayIndex">The index in the array at which to start the copy</param>
        /// <exception cref="System.ArgumentNullException">Thrown if array argument is null</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the arrayIndex is less than zero</exception>
        /// <exception cref="System.ArgumentException">Thrown if the array argument passed in with respect to the arrayIndex passed in not big enough to hold the MemberCollection being copied</exception>
        public override void CopyTo(EdmMember[] array, int arrayIndex)
        {
            // Check on the array index
            if (arrayIndex < 0)
            {
                throw EntityUtil.ArgumentOutOfRange("arrayIndex");
            }

            // Check if the array together with the array index has enough room to copy
            int baseTypeMemberCount = GetBaseTypeMemberCount();
            if (base.Count + baseTypeMemberCount > array.Length - arrayIndex)
            {
                throw EntityUtil.Argument("arrayIndex");
            }

            // If the base type has any members, copy those first
            if (baseTypeMemberCount > 0)
            {
                ((StructuralType)_declaringType.BaseType).Members.CopyTo(array, arrayIndex);
            }

            base.CopyTo(array, arrayIndex + baseTypeMemberCount);
        }

        /// <summary>
        /// Gets an item from the collection with the given identity
        /// </summary>
        /// <param name="identity">The identity of the item to search for</param>
        /// <param name="ignoreCase">Whether case is ignore in the search</param>
        /// <param name="item">An item from the collection, null if the item is not found</param>
        /// <returns>True an item is retrieved</returns>
        /// <exception cref="System.ArgumentNullException">if identity argument is null</exception>
        public override bool TryGetValue(string identity, bool ignoreCase, out EdmMember item)
        {
            // See if it's in this collection
            if (!base.TryGetValue(identity, ignoreCase, out item))
            {
                // Now go to the parent type to find it
                EdmType baseType = _declaringType.BaseType;
                if (baseType != null)
                {
                    ((StructuralType)baseType).Members.TryGetValue(identity, ignoreCase, out item);
                }
            }

            return item != null;
        }

        /// <summary>
        /// Gets an itme with identity
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="ignoreCase"></param>
        /// <returns></returns>
        public override EdmMember GetValue(string identity, bool ignoreCase)
        {
            EdmMember item = null;

            if (!TryGetValue(identity, ignoreCase, out item))
            {
                throw EntityUtil.ItemInvalidIdentity(identity, "identity");
            }

            return item;
        }

        /// <summary>
        /// Get the declared only members of a particular type
        /// </summary>
        internal ReadOnlyMetadataCollection<T> GetDeclaredOnlyMembers<T>() where T : EdmMember
        {
            MetadataCollection<T> newCollection = new MetadataCollection<T>();
            for (int i = 0; i < base.Count; i++)
            {
                T member = base[i] as T;
                if (member != null)
                {
                    newCollection.Add(member);
                }
            }

            return newCollection.AsReadOnlyMetadataCollection();
        }

        /// <summary>
        /// Get the number of members the base type has.  If the base type is not a structural type or has no
        /// members, it returns 0
        /// </summary>
        /// <returns>The number of members in the base type</returns>
        private int GetBaseTypeMemberCount()
        {
            // The count of members is what in this collection plus base type's member collection
            StructuralType baseType = _declaringType.BaseType as StructuralType;
            if (baseType != null)
            {
                return baseType.Members.Count;
            }

            return 0;
        }

        /// <summary>
        /// Gets the index relative to this collection for the given index.  For an index to really refers to something in
        /// the base type, the return value is negative relative to this collection.  For an index refers to something in this
        /// collection, the return value is positive.  In both cases, it's simply (index) - (base type member count)
        /// </summary>
        /// <returns>The relative index</returns>
        private int GetRelativeIndex(int index)
        {
            int baseTypeMemberCount = GetBaseTypeMemberCount();
            int thisTypeMemberCount = base.Count;

            // Check if the index is in range
            if (index < 0 || index >= baseTypeMemberCount + thisTypeMemberCount)
            {
                throw EntityUtil.ArgumentOutOfRange("index");
            }

            return index - baseTypeMemberCount;
        }
        
        private void ValidateMemberForAdd(EdmMember member, string argumentName)
        {
            // Check to make sure the given member is not associated with another type
            EntityUtil.GenericCheckArgumentNull(member, argumentName);

            Debug.Assert(member.DeclaringType == null, string.Format(CultureInfo.CurrentCulture, "The member {0} already has a declaring type, it cannot be added to this collection.", argumentName));

            // Validate the item with the declaring type. 
            _declaringType.ValidateMemberForAdd(member);
        }

        #endregion
    }
}