// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the application DAM - Clients Library. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Deltares.Standard.IO.DtoAssembler; namespace Deltares.Standard { /// /// The property bag defines a dynamic property collection (dictionary) /// public class DtoPropertyAttributeMap : IDictionary { /// /// Holds the property dictionary /// private readonly IDictionary dict = new Dictionary(); #region IDictionary Members /// /// Adds a property attribute mapping to the dictionary /// /// The name of the property /// The value public void Add(string propName, DtoPropertyAttriubteMapping item) { if (item == null) throw new DtoPropertyMapException("Cannot add an empty or null item to the map"); if (!item.PropertyName.HasValidStringValue() || propName != item.PropertyName) throw new DtoPropertyMapException("Property name is empty or doesn't match dictionary property name (key)"); this.dict.Add(propName, item); } /// /// Determinses wheter the dictionary contains the specified key /// /// The name of the property /// public bool ContainsKey(string propName) { return this.dict.ContainsKey(propName); } /// /// Gets the containing keys /// public ICollection Keys { get { return this.dict.Keys; } } /// /// Removes a key from the dictionary /// /// The key or property name /// Booleand value indicating when the item is successfuly removed public bool Remove(string propName) { return this.dict.Remove(propName); } /// /// /// /// /// /// public bool TryGetValue(string propName, out DtoPropertyAttriubteMapping value) { return this.dict.TryGetValue(propName, out value); } /// /// /// public ICollection Values { get { return this.dict.Values; } } /// /// /// /// /// public DtoPropertyAttriubteMapping this[string propName] { get { return this.dict[propName]; } set { this.dict[propName] = value; } } /// /// /// /// public void Add(KeyValuePair item) { this.dict.Add(item.Key, item.Value); } /// /// /// public void Clear() { this.dict.Clear(); } /// /// /// /// /// public bool Contains(KeyValuePair item) { return ((IDictionary) this.dict).Contains(item); } /// /// /// /// /// public void CopyTo(KeyValuePair[] array, int arrayIndex) { ((IDictionary) this.dict).CopyTo(array, arrayIndex); } /// /// /// public int Count { get { return this.dict.Count; } } /// /// /// public bool IsReadOnly { get { return false; } } /// /// /// /// /// public bool Remove(KeyValuePair item) { return this.dict.Remove(item.Key); } /// /// /// /// public IEnumerator> GetEnumerator() { return this.dict.GetEnumerator(); } /// /// /// /// IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) ((IDictionary) this.dict).GetEnumerator(); } #endregion /// /// Adds a property attribute mapping to the dictionary /// /// The item to add public void Add(DtoPropertyAttriubteMapping item) { if (item == null) throw new ArgumentNullException("item"); this.Add(item.PropertyName, item); } /// /// Adds a or updtes a property attribute mapping to the dictionary /// /// The item to add public void AddOrUpdate(string propName, string attrName) { this.AddOrUpdate(propName, attrName, DtoPropertyImportance.Optional); } /// /// Adds a or updtes a property attribute mapping to the dictionary /// /// The item to add public void AddOrUpdate(string propName, string attrName, DtoPropertyImportance importance) { if (!attrName.HasValidStringValue()) throw new DtoPropertyMapException("attrName"); this.AddOrUpdate(propName, new DtoPropertyAttriubteMapping() { PropertyName = propName, AttributeName = attrName, Importance = importance }); } /// /// Adds a or updtes a property attribute mapping to the dictionary /// /// The item to add public void AddOrUpdate(DtoPropertyAttriubteMapping item) { if (item == null) throw new ArgumentNullException("item"); this.AddOrUpdate(item.PropertyName, item); } /// /// Adds or updates new items in the dictionary /// /// The name of the property /// The value public void AddOrUpdate(string propName, DtoPropertyAttriubteMapping item) { if (item == null) throw new ArgumentNullException("item"); if (!propName.HasValidStringValue()) throw new DtoPropertyMapException("Property name (or key) is empty"); if (!this.dict.ContainsKey(propName)) { this.Add(item.PropertyName, item); } else { this.dict[propName] = item; } } /// /// Adds or updates new items in the dictionary /// /// The key value pair collection to handle public void AddOrUpdateRange(IEnumerable coll) { foreach (DtoPropertyAttriubteMapping item in coll) { this.AddOrUpdate(item); } } /// /// Gets an item by property name /// /// The name of the property to look for /// The item if found null otherwise public DtoPropertyAttriubteMapping GetByPropertyName(string name) { return this.GetByPropertyName(name, StringComparison.InvariantCultureIgnoreCase); } /// /// Gets an item by property name /// /// The name of the property to look for /// The string type /// The item if found null otherwise public DtoPropertyAttriubteMapping GetByPropertyName(string name, StringComparison comparisonType) { IEnumerable q = from x in this.dict.Values where x.PropertyName.Equals(name, comparisonType) select x; if (q.Any()) return q.Single(); return null; } /// /// Gets an item by attribute name /// /// The name of the property to look for /// The item if found null otherwise public DtoPropertyAttriubteMapping GetByAttributeName(string name) { return this.GetByAttributeName(name, StringComparison.InvariantCultureIgnoreCase); } /// /// Gets an item by attribute name /// /// The name of the property to look for /// The string type /// The item if found null otherwise public DtoPropertyAttriubteMapping GetByAttributeName(string name, StringComparison comparisonType) { IEnumerable q = from x in this.dict.Values where x.AttributeName.Equals(name, comparisonType) select x; if (q.Any()) return q.Single(); return null; } } }