using System;
using System.Collections.Generic;
using System.Linq;
using Chernobyl.Utility;
namespace Chernobyl.Creation.Collections
{
///
/// An and that stores
/// other instances and acts as a composite
/// (http://en.wikipedia.org/wiki/Composite_pattern) for those instances.
///
/// The type that is to
/// be held by this .
/// The type that is created by the
/// instances that will be contained within this instance.
public class BuilderCollection : BuilderEnumerable, ICollection
where TFactory : IBuilder
{
///
/// Initializes a new instance of the
/// class.
///
public BuilderCollection()
: this(Enumerable.Empty())
{}
///
/// Initializes a new instance of the
/// class.
///
/// The instance whose
/// event is to be forwarded through
/// this instance.
public BuilderCollection(IEnumerable builders)
{
builders.ThrowIfNull("builders");
_builders = new List(builders);
foreach (TFactory builder in _builders)
builder.Created += OnItemCreated;
}
///
/// Adds an item to the .
///
/// The object to add to the
/// .
public void Add(TFactory item)
{
_builders.Add(item);
item.Created += OnItemCreated;
}
///
/// Removes all items from the .
///
public void Clear()
{
foreach (TFactory builder in _builders)
builder.Created -= OnItemCreated;
_builders.Clear();
}
///
/// Determines whether the contains a
/// specific value.
///
/// The object to locate in the
/// .
/// True if is found in the
/// ; otherwise, false.
///
public bool Contains(TFactory item)
{
return _builders.Contains(item);
}
///
/// Copies the elements of the to an
/// , starting at a particular
/// index.
///
/// The one-dimensional that is
/// the destination of the elements copied from .
/// The must have zero-based indexing.
/// The zero-based index in
/// at which copying begins.
public void CopyTo(TFactory[] array, int arrayIndex)
{
_builders.CopyTo(array, arrayIndex);
}
///
/// Gets the number of elements contained in the .
///
///
/// The number of elements contained in the .
///
public int Count
{
get { return _builders.Count; }
}
///
/// Gets a value indicating whether the is
/// read-only.
///
/// true if the is read-only;
/// otherwise, false.
public bool IsReadOnly
{
get { return _builders.IsReadOnly; }
}
///
/// Removes the first occurrence of a specific object from the
/// .
///
/// The object to remove from the
/// .
///
/// true if was successfully removed from the
/// ; otherwise, false. This method also
/// returns false if is not found in the
/// original .
///
public bool Remove(TFactory item)
{
bool result = _builders.Remove(item);
if (result)
item.Created -= OnItemCreated;
return result;
}
///
/// The of
/// instances whose event is being
/// listened to by this instance.
///
protected override IEnumerable Builders
{
get { return _builders; }
}
///
/// The backing field to .
///
readonly ICollection _builders;
}
}