// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero 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;
namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers
{
///
/// The property bag defines a dynamic property collection (dictionary)
///
public class DtoPropertyAttributeMap : IDictionary
{
///
/// Holds the property dictionary
///
private readonly IDictionary dict = new Dictionary();
///
/// Adds a property attribute mapping to the dictionary
///
/// The item to add
public void Add(DtoPropertyAttributeMapping item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
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)
{
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");
}
AddOrUpdate(propName,
new DtoPropertyAttributeMapping
{
PropertyName = propName,
AttributeName = attrName,
Importance = importance
});
}
///
/// Adds a or updtes a property attribute mapping to the dictionary
///
/// The item to add
public void AddOrUpdate(DtoPropertyAttributeMapping item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
AddOrUpdate(item.PropertyName, item);
}
///
/// Adds or updates new items in the dictionary
///
/// The name of the property
/// The value
public void AddOrUpdate(string propName, DtoPropertyAttributeMapping item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
if (!propName.HasValidStringValue())
{
throw new DtoPropertyMapException("Property name (or key) is empty");
}
if (!dict.ContainsKey(propName))
{
Add(item.PropertyName, item);
}
else
{
dict[propName] = item;
}
}
///
/// Adds or updates new items in the dictionary
///
/// The key value pair collection to handle
public void AddOrUpdateRange(IEnumerable coll)
{
foreach (DtoPropertyAttributeMapping item in coll)
{
AddOrUpdate(item);
}
}
///
/// Gets an item by property name
///
/// The name of the property to look for
/// The item if found null otherwise
public DtoPropertyAttributeMapping GetByPropertyName(string name)
{
return 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 DtoPropertyAttributeMapping GetByPropertyName(string name, StringComparison comparisonType)
{
IEnumerable q = from x in 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 DtoPropertyAttributeMapping GetByAttributeName(string name)
{
return 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 DtoPropertyAttributeMapping GetByAttributeName(string name, StringComparison comparisonType)
{
IEnumerable q = from x in dict.Values
where x.AttributeName.Equals(name, comparisonType)
select x;
if (q.Any())
{
return q.Single();
}
return null;
}
#region IDictionary Members
///
/// Adds a property attribute mapping to the dictionary
///
/// The name of the property
/// The value
public void Add(string propName, DtoPropertyAttributeMapping 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)");
}
dict.Add(propName, item);
}
///
/// Determinses wheter the dictionary contains the specified key
///
/// The name of the property
///
public bool ContainsKey(string propName)
{
return dict.ContainsKey(propName);
}
///
/// Gets the containing keys
///
public ICollection Keys
{
get
{
return 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 dict.Remove(propName);
}
///
///
///
///
///
///
public bool TryGetValue(string propName, out DtoPropertyAttributeMapping value)
{
return dict.TryGetValue(propName, out value);
}
///
///
///
public ICollection Values
{
get
{
return dict.Values;
}
}
///
///
///
///
///
public DtoPropertyAttributeMapping this[string propName]
{
get
{
return dict[propName];
}
set
{
dict[propName] = value;
}
}
///
///
///
///
public void Add(KeyValuePair item)
{
dict.Add(item.Key, item.Value);
}
///
///
///
public void Clear()
{
dict.Clear();
}
///
///
///
///
///
public bool Contains(KeyValuePair item)
{
return dict.Contains(item);
}
///
///
///
///
///
public void CopyTo(KeyValuePair[] array, int arrayIndex)
{
dict.CopyTo(array, arrayIndex);
}
///
///
///
public int Count => dict.Count;
///
///
///
public bool IsReadOnly => false;
///
///
///
///
///
public bool Remove(KeyValuePair item)
{
return dict.Remove(item.Key);
}
///
///
///
///
public IEnumerator> GetEnumerator()
{
return dict.GetEnumerator();
}
///
///
///
///
IEnumerator IEnumerable.GetEnumerator()
{
return dict.GetEnumerator();
}
#endregion
}
}