//-----------------------------------------------------------------------
//
// Copyright (c) 2009 Deltares. All rights reserved.
//
// B. Faassen
// barry.faassen@deltares.nl
// 9-2-2009
//
// Defines a type safe collection of DtoPropertyAttributeMapping items
//
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Deltares.DamEngine.Calculators.KernelWrappers.DamMacroStability.Assemblers
{
///
/// 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;
}
}
}