// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Threading;
namespace RGB.NET.Core;
///
///
/// Represents a ledgroup containing arbitrary .
///
public sealed class ListLedGroup : AbstractLedGroup
{
#region Properties & Fields
private readonly ActionDisposable _unlockDisposable;
///
/// Gets the list containing the of this .
///
private readonly IList _groupLeds = [];
#endregion
#region Constructors
///
///
/// Initializes a new instance of the class.
///
/// Specifies the surface to attach this group to or null if the group should not be attached on creation.
public ListLedGroup(RGBSurface? surface)
: base(surface)
{
_unlockDisposable = new ActionDisposable(Unlock);
}
///
///
/// Initializes a new instance of the class.
///
/// Specifies the surface to attach this group to or null if the group should not be attached on creation.
/// The initial of this .
public ListLedGroup(RGBSurface? surface, IEnumerable leds)
: base(surface)
{
_unlockDisposable = new ActionDisposable(Unlock);
AddLeds(leds);
}
///
///
/// Initializes a new instance of the class.
///
/// Specifies the surface to attach this group to or null if the group should not be attached on creation.
/// The initial of this .
public ListLedGroup(RGBSurface? surface, params Led[] leds)
: base(surface)
{
_unlockDisposable = new ActionDisposable(Unlock);
AddLeds(leds);
}
#endregion
#region Methods
///
/// Adds the specified LED(s) to this .
///
/// The LED(s) to add.
public void AddLed(params Led[] leds) => AddLeds(leds);
///
/// Adds the specified to this .
///
/// The to add.
public void AddLeds(IEnumerable leds)
{
lock (_groupLeds)
foreach (Led led in leds)
if (!ContainsLed(led))
_groupLeds.Add(led);
}
///
/// Removes the specified LED(s) from this .
///
/// The LED(s) to remove.
public void RemoveLed(params Led[] leds) => RemoveLeds(leds);
///
/// Removes the specified from this .
///
/// The to remove.
public void RemoveLeds(IEnumerable leds)
{
lock (_groupLeds)
foreach (Led led in leds)
_groupLeds.Remove(led);
}
///
/// Checks if a specified LED is contained by this ledgroup.
///
/// The LED which should be checked.
/// true if the LED is contained by this ledgroup; otherwise, false.
public bool ContainsLed(Led led)
{
lock (_groupLeds)
return _groupLeds.Contains(led);
}
///
/// Merges the from the specified ledgroup in this ledgroup.
///
/// The ledgroup to merge.
public void MergeLeds(ILedGroup groupToMerge)
{
lock (_groupLeds)
foreach (Led led in groupToMerge)
if (!_groupLeds.Contains(led))
_groupLeds.Add(led);
}
///
///
/// Gets a list containing the from this group.
///
/// The list containing the .
protected override IEnumerable GetLeds() => ToList();
///
///
/// Gets a list containing the from this group.
///
/// The list containing the .
public override IList ToList()
{
lock (_groupLeds)
return [.._groupLeds];
}
protected override IDisposable ToListUnsafe(out IList leds)
{
Monitor.Enter(_groupLeds);
leds = _groupLeds;
return _unlockDisposable;
}
private void Unlock() => Monitor.Exit(_groupLeds);
#endregion
}