using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using Artemis.Core.Events; namespace Artemis.Core; /// public abstract class PinCollection : CorePropertyChanged, IPinCollection { #region Constructors /// /// Creates a new instance of the class /// /// The node the pin collection belongs to /// The name of the pin collection /// The resulting output pin collection protected PinCollection(INode node, string name) { Node = node ?? throw new ArgumentNullException(nameof(node)); Name = name; } #endregion /// public event EventHandler>? PinAdded; /// public event EventHandler>? PinRemoved; #region Properties & Fields /// public INode Node { get; } /// public string Name { get; } /// public abstract PinDirection Direction { get; } /// public abstract Type Type { get; } private readonly List _pins = new(); /// /// Gets a read only observable collection of the pins /// public ReadOnlyCollection Pins => new(_pins); #endregion #region Methods /// public void Add(IPin pin) { if (pin.Direction != Direction) throw new ArtemisCoreException($"Can't add a {pin.Direction} pin to an {Direction} pin collection."); if (pin.Type != Type) throw new ArtemisCoreException($"Can't add a {pin.Type} pin to an {Type} pin collection."); if (_pins.Contains(pin)) return; _pins.Add(pin); PinAdded?.Invoke(this, new SingleValueEventArgs(pin)); } /// public bool Remove(IPin pin) { bool removed = _pins.Remove(pin); if (removed) PinRemoved?.Invoke(this, new SingleValueEventArgs(pin)); return removed; } /// public void Reset() { foreach (IPin pin in _pins) pin.Reset(); } /// public abstract IPin CreatePin(); /// public IEnumerator GetEnumerator() { return Pins.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion }