using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rssdp
{
///
/// Represents a collection of instances keyed by the property value.
///
///
/// Items added to this collection are keyed by their property value, at the time they were added. If the name changes after they were added to the collection, the key is not updated unless the item is manually removed and re-added to the collection.
///
public class SsdpDevicePropertiesCollection : IEnumerable
{
#region Fields
private IDictionary _Properties;
#endregion
#region Constructors
///
/// Default constructor.
///
public SsdpDevicePropertiesCollection()
{
_Properties = new Dictionary();
}
///
/// Full constructor.
///
/// Specifies the initial capacity of the collection.
public SsdpDevicePropertiesCollection(int capacity)
{
_Properties = new Dictionary(capacity);
}
#endregion
#region Public Methpds
///
/// Adds a instance to the collection.
///
/// The property instance to add to the collection.
///
///
///
/// Thrown if is null.
/// Thrown if the property of the argument is null or empty string, or if the collection already contains an item with the same key.
public void Add(SsdpDeviceProperty customDeviceProperty)
{
if (customDeviceProperty == null) throw new ArgumentNullException("customDeviceProperty");
if (String.IsNullOrEmpty(customDeviceProperty.FullName)) throw new ArgumentException("customDeviceProperty.FullName cannot be null or empty.");
lock (_Properties)
{
_Properties.Add(customDeviceProperty.FullName, customDeviceProperty);
}
}
#region Remove Overloads
///
/// Removes the specified property instance from the collection.
///
/// The instance to remove from the collection.
///
/// Only remove the specified property if that instance was in the collection, if another property with the same full name exists in the collection it is not removed.
///
/// True if an item was removed from the collection, otherwise false (because it did not exist or was not the same instance).
///
/// Thrown if the is null.
/// Thrown if the property of the argument is null or empty string, or if the collection already contains an item with the same key.
public bool Remove(SsdpDeviceProperty customDeviceProperty)
{
if (customDeviceProperty == null) throw new ArgumentNullException("customDeviceProperty");
if (String.IsNullOrEmpty(customDeviceProperty.FullName)) throw new ArgumentException("customDeviceProperty.FullName cannot be null or empty.");
lock (_Properties)
{
if (_Properties.ContainsKey(customDeviceProperty.FullName) && _Properties[customDeviceProperty.FullName] == customDeviceProperty)
return _Properties.Remove(customDeviceProperty.FullName);
}
return false;
}
///
/// Removes the property with the specified key ( from the collection.
///
/// The full name of the instance to remove from the collection.
/// True if an item was removed from the collection, otherwise false (because no item exists in the collection with that key).
/// Thrown if the argument is null or empty string.
public bool Remove(string customDevicePropertyFullName)
{
if (String.IsNullOrEmpty(customDevicePropertyFullName)) throw new ArgumentException("customDevicePropertyFullName cannot be null or empty.");
lock (_Properties)
{
return _Properties.Remove(customDevicePropertyFullName);
}
}
#endregion
///
/// Returns a boolean indicating whether or not the specified instance is in the collection.
///
/// An instance to check the collection for.
/// True if the specified instance exists in the collection, otherwise false.
public bool Contains(SsdpDeviceProperty customDeviceProperty)
{
if (customDeviceProperty == null) throw new ArgumentNullException("customDeviceProperty");
if (String.IsNullOrEmpty(customDeviceProperty.FullName)) throw new ArgumentException("customDeviceProperty.FullName cannot be null or empty.");
lock (_Properties)
{
if (_Properties.ContainsKey(customDeviceProperty.FullName))
return _Properties[customDeviceProperty.FullName] == customDeviceProperty;
}
return false;
}
///
/// Returns a boolean indicating whether or not a instance with the specified full name value exists in the collection.
///
/// A string containing the full name of the instance to check for.
/// True if an item with the specified full name exists in the collection, otherwise false.
public bool Contains(string customDevicePropertyFullName)
{
if (String.IsNullOrEmpty(customDevicePropertyFullName)) throw new ArgumentException("customDevicePropertyFullName cannot be null or empty.");
lock (_Properties)
{
return _Properties.ContainsKey(customDevicePropertyFullName);
}
}
#endregion
#region Public Properties
///
/// Returns the number of items in the collection.
///
public int Count
{
get { return _Properties.Count; }
}
///
/// Returns the instance from the collection that has the specified value.
///
/// The full name of the property to return.
/// A instance from the collection.
/// Thrown if no item exists in the collection with the specified value.
public SsdpDeviceProperty this[string fullName]
{
get
{
return _Properties[fullName];
}
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator of instances in this collection.
///
/// An enumerator of instances in this collection.
public IEnumerator GetEnumerator()
{
lock (_Properties)
{
return _Properties.Values.GetEnumerator();
}
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator of instances in this collection.
///
/// An enumerator of instances in this collection.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
lock (_Properties)
{
return _Properties.Values.GetEnumerator();
}
}
#endregion
}
}