using System;
namespace ZeroLevel.Services.Collections
{
///
/// Collects data while there is capacity and invokes an action after that (batch processing)
///
///
public sealed class Capacitor
: IDisposable
{
private int _index = -1;
private int _count = 0;
private readonly T[] _buffer;
private readonly Action _dischargeAction;
public int Count => _count;
public Capacitor(int volume, Action dischargeAction)
{
if (volume < 1) volume = 16;
if (dischargeAction == null) throw new ArgumentNullException(nameof(dischargeAction));
_buffer = new T[volume];
_dischargeAction = dischargeAction;
}
public void Add(T val)
{
_index++;
if (_index >= _buffer.Length)
{
_dischargeAction.Invoke(_buffer, _buffer.Length);
_index = 0;
_count = 0;
}
_buffer[_index] = val;
_count++;
}
public void Discharge()
{
if (_count > 0)
{
_dischargeAction.Invoke(_buffer, _count);
}
}
public void Dispose()
{
if (_count > 0)
{
try
{
Discharge();
}
catch (Exception ex)
{
Log.Error(ex, $"[Capacitor.Dispose] Fault discharge in dispose method");
}
}
}
}
}