1 //---------------------------------------------------------------------
2 // <copyright file="SchemaElementLookUpTable.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 namespace System.Data.EntityModel.SchemaObjectModel
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Data.Metadata.Edm;
16 using System.Diagnostics;
19 /// Summary description for SchemaElementLookUpTable.
21 internal sealed class SchemaElementLookUpTable<T> : IEnumerable<T>, ISchemaElementLookUpTable<T>
22 where T : SchemaElement
24 #region Instance Fields
25 private Dictionary<string,T> _keyToType = null;
26 private List<string> _keysInDefOrder = new List<string>();
29 #region Public Methods
33 public SchemaElementLookUpTable()
44 return KeyToType.Count;
51 /// <param name="key"></param>
52 /// <returns></returns>
53 public bool ContainsKey(string key)
55 return KeyToType.ContainsKey(KeyFromName(key));
61 /// <param name="key"></param>
62 /// <returns></returns>
63 public T LookUpEquivalentKey(string key)
65 key = KeyFromName(key);
68 if (KeyToType.TryGetValue(key, out element))
78 public T this[string key]
82 return KeyToType[KeyFromName(key)];
89 public T GetElementAt(int index)
91 return KeyToType[_keysInDefOrder[index]];
97 /// <returns></returns>
98 public IEnumerator<T> GetEnumerator()
100 return new SchemaElementLookUpTableEnumerator<T,T>(KeyToType,_keysInDefOrder);
102 IEnumerator System.Collections.IEnumerable.GetEnumerator()
104 return new SchemaElementLookUpTableEnumerator<T,T>(KeyToType,_keysInDefOrder);
110 /// <returns></returns>
111 public IEnumerator<S> GetFilteredEnumerator<S>()
114 return new SchemaElementLookUpTableEnumerator<S,T>(KeyToType,_keysInDefOrder);
118 /// Add the given type to the schema look up table. If there is an error, it
119 /// adds the error and returns false. otherwise, it adds the type to the lookuptable
122 public AddErrorKind TryAdd(T type)
124 Debug.Assert(type != null, "type parameter is null");
126 if (String.IsNullOrEmpty(type.Identity))
128 return AddErrorKind.MissingNameError;
131 string key = KeyFromElement(type);
133 if (KeyToType.TryGetValue(key, out element))
135 return AddErrorKind.DuplicateNameError;
138 KeyToType.Add(key,type);
139 _keysInDefOrder.Add(key);
141 return AddErrorKind.Succeeded;
144 public void Add(T type, bool doNotAddErrorForEmptyName, Func<object, string> duplicateKeyErrorFormat)
146 Debug.Assert(type != null, "type parameter is null");
147 Debug.Assert(null != duplicateKeyErrorFormat, "duplicateKeyErrorFormat cannot be null");
149 AddErrorKind error = TryAdd(type);
151 if (error == AddErrorKind.MissingNameError)
153 if (!doNotAddErrorForEmptyName)
155 type.AddError(ErrorCode.InvalidName, EdmSchemaErrorSeverity.Error,
156 System.Data.Entity.Strings.MissingName);
160 else if (error == AddErrorKind.DuplicateNameError)
162 type.AddError(ErrorCode.AlreadyDefined, EdmSchemaErrorSeverity.Error,
163 duplicateKeyErrorFormat(type.FQName));
167 Debug.Assert(error == AddErrorKind.Succeeded, "Invalid error encountered");
173 #region Internal Methods
176 #region Private Methods
180 /// <param name="type"></param>
181 /// <returns></returns>
182 private static string KeyFromElement(T type)
184 return KeyFromName(type.Identity);
190 /// <param name="unnormalizedKey"></param>
191 /// <returns></returns>
192 private static string KeyFromName(string unnormalizedKey)
194 Debug.Assert(!String.IsNullOrEmpty(unnormalizedKey), "unnormalizedKey parameter is null or empty");
196 return unnormalizedKey;
200 #region Private Properties
204 private Dictionary<string,T> KeyToType
208 if ( _keyToType == null )
210 _keyToType = new Dictionary<string,T>(StringComparer.Ordinal);