Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Metadata / Edm / CollectionType.cs
1 //---------------------------------------------------------------------
2 // <copyright file="CollectionType.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       [....]
7 // @backupOwner [....]
8 //---------------------------------------------------------------------
9 using System;
10 using System.Collections.Generic;
11 using System.Data.Common;
12 using System.Text;
13
14 namespace System.Data.Metadata.Edm
15 {
16     /// <summary>
17     /// Represents the Edm Collection Type
18     /// </summary>
19     public sealed class CollectionType : EdmType
20     {
21         #region Constructors
22         /// <summary>
23         /// The constructor for constructing a CollectionType object with the element type it contains
24         /// </summary>
25         /// <param name="elementType">The element type that this collection type contains</param>
26         /// <exception cref="System.ArgumentNullException">Thrown if the argument elementType is null</exception>
27         internal CollectionType(EdmType elementType)
28             : this(TypeUsage.Create(elementType))
29         {
30             this.DataSpace = elementType.DataSpace;
31         }
32
33         /// <summary>
34         /// The constructor for constructing a CollectionType object with the element type (as a TypeUsage) it contains
35         /// </summary>
36         /// <param name="elementType">The element type that this collection type contains</param>
37         /// <exception cref="System.ArgumentNullException">Thrown if the argument elementType is null</exception>
38         internal CollectionType(TypeUsage elementType)
39             : base(GetIdentity(EntityUtil.GenericCheckArgumentNull(elementType, "elementType")), 
40                     EdmConstants.TransientNamespace, elementType.EdmType.DataSpace)
41         {
42             _typeUsage = elementType;
43             SetReadOnly();
44         }
45         #endregion
46
47         #region Fields
48         private readonly TypeUsage _typeUsage;
49         #endregion
50
51         #region Properties
52         /// <summary>
53         /// Returns the kind of the type
54         /// </summary>
55         public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.CollectionType; } }
56
57         /// <summary>
58         /// The type of the element that this collection type contains
59         /// </summary>
60         [MetadataProperty(BuiltInTypeKind.TypeUsage, false)]
61         public TypeUsage TypeUsage
62         {
63             get
64             {
65                 return _typeUsage;
66             }
67         }
68         #endregion
69
70         #region Methods
71         /// <summary>
72         /// Constructs the name of the collection type
73         /// </summary>
74         /// <param name="typeUsage">The typeusage for the element type that this collection type refers to</param>
75         /// <returns>The identity of the resulting collection type</returns>
76         private static string GetIdentity(TypeUsage typeUsage)
77         {
78             StringBuilder builder = new StringBuilder(50);
79             builder.Append("collection[");
80             typeUsage.BuildIdentity(builder);
81             builder.Append("]");
82             return builder.ToString();
83         }
84
85         /// <summary>
86         /// Override EdmEquals to support value comparison of TypeUsage property
87         /// </summary>
88         /// <param name="item"></param>
89         /// <returns></returns>
90         internal override bool EdmEquals(MetadataItem item)
91         {
92             // short-circuit if this and other are reference equivalent
93             if (Object.ReferenceEquals(this, item)) { return true; }
94
95             // check type of item
96             if (null == item || BuiltInTypeKind.CollectionType != item.BuiltInTypeKind) { return false; }
97             CollectionType other = (CollectionType)item;
98
99             // compare type usage
100             return this.TypeUsage.EdmEquals(other.TypeUsage);
101         }
102         #endregion
103     }
104 }