//--------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- namespace System.Data.EntityModel.SchemaObjectModel { using System; using System.Collections; using System.Collections.Generic; using System.Data.Metadata.Edm; using System.Diagnostics; /// /// Summary description for SchemaElementLookUpTable. /// internal sealed class SchemaElementLookUpTable : IEnumerable, ISchemaElementLookUpTable where T : SchemaElement { #region Instance Fields private Dictionary _keyToType = null; private List _keysInDefOrder = new List(); #endregion #region Public Methods /// /// /// public SchemaElementLookUpTable() { } /// /// /// public int Count { get { return KeyToType.Count; } } /// /// /// /// /// public bool ContainsKey(string key) { return KeyToType.ContainsKey(KeyFromName(key)); } /// /// /// /// /// public T LookUpEquivalentKey(string key) { key = KeyFromName(key); T element; if (KeyToType.TryGetValue(key, out element)) { return element; } return null; } /// /// /// public T this[string key] { get { return KeyToType[KeyFromName(key)]; } } /// /// /// public T GetElementAt(int index) { return KeyToType[_keysInDefOrder[index]]; } /// /// /// /// public IEnumerator GetEnumerator() { return new SchemaElementLookUpTableEnumerator(KeyToType,_keysInDefOrder); } IEnumerator System.Collections.IEnumerable.GetEnumerator() { return new SchemaElementLookUpTableEnumerator(KeyToType,_keysInDefOrder); } /// /// /// /// public IEnumerator GetFilteredEnumerator() where S : T { return new SchemaElementLookUpTableEnumerator(KeyToType,_keysInDefOrder); } /// /// Add the given type to the schema look up table. If there is an error, it /// adds the error and returns false. otherwise, it adds the type to the lookuptable /// and returns true /// public AddErrorKind TryAdd(T type) { Debug.Assert(type != null, "type parameter is null"); if (String.IsNullOrEmpty(type.Identity)) { return AddErrorKind.MissingNameError; } string key = KeyFromElement(type); T element; if (KeyToType.TryGetValue(key, out element)) { return AddErrorKind.DuplicateNameError; } KeyToType.Add(key,type); _keysInDefOrder.Add(key); return AddErrorKind.Succeeded; } public void Add(T type, bool doNotAddErrorForEmptyName, Func duplicateKeyErrorFormat) { Debug.Assert(type != null, "type parameter is null"); Debug.Assert(null != duplicateKeyErrorFormat, "duplicateKeyErrorFormat cannot be null"); AddErrorKind error = TryAdd(type); if (error == AddErrorKind.MissingNameError) { if (!doNotAddErrorForEmptyName) { type.AddError(ErrorCode.InvalidName, EdmSchemaErrorSeverity.Error, System.Data.Entity.Strings.MissingName); } return; } else if (error == AddErrorKind.DuplicateNameError) { type.AddError(ErrorCode.AlreadyDefined, EdmSchemaErrorSeverity.Error, duplicateKeyErrorFormat(type.FQName)); } else { Debug.Assert(error == AddErrorKind.Succeeded, "Invalid error encountered"); } } #endregion #region Internal Methods #endregion #region Private Methods /// /// /// /// /// private static string KeyFromElement(T type) { return KeyFromName(type.Identity); } /// /// /// /// /// private static string KeyFromName(string unnormalizedKey) { Debug.Assert(!String.IsNullOrEmpty(unnormalizedKey), "unnormalizedKey parameter is null or empty"); return unnormalizedKey; } #endregion #region Private Properties /// /// /// private Dictionary KeyToType { get { if ( _keyToType == null ) { _keyToType = new Dictionary(StringComparer.Ordinal); } return _keyToType; } } #endregion } enum AddErrorKind { Succeeded, MissingNameError, DuplicateNameError, } }