mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: ScrollQueue - added OnItemTrimmed event
This commit is contained in:
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|||||||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Added
|
||||||
|
- ScrollQueue - Added OnItemTrimmed event
|
||||||
|
- ScrollQueueTests - Added OnItemTrimmed event test
|
||||||
|
|
||||||
## [11.0.0] - 2020-03-20
|
## [11.0.0] - 2020-03-20
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using ICD.Common.Utils.Collections;
|
using ICD.Common.Utils.Collections;
|
||||||
|
using ICD.Common.Utils.EventArguments;
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Tests.Collections
|
namespace ICD.Common.Utils.Tests.Collections
|
||||||
{
|
{
|
||||||
@@ -76,5 +77,43 @@ namespace ICD.Common.Utils.Tests.Collections
|
|||||||
|
|
||||||
Assert.AreEqual(0, test.Peek());
|
Assert.AreEqual(0, test.Peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test, UsedImplicitly]
|
||||||
|
public void OnItemTrimmedTest()
|
||||||
|
{
|
||||||
|
ScrollQueue<int> test = new ScrollQueue<int>(3);
|
||||||
|
|
||||||
|
int? removedItem = null;
|
||||||
|
|
||||||
|
test.OnItemTrimmed += (sender, args) => removedItem = args.Data;
|
||||||
|
|
||||||
|
test.Enqueue(1);
|
||||||
|
test.Enqueue(2);
|
||||||
|
test.Enqueue(3);
|
||||||
|
|
||||||
|
Assert.IsNull(removedItem, "Raised Early");
|
||||||
|
|
||||||
|
test.Enqueue(4);
|
||||||
|
|
||||||
|
Assert.True(removedItem.HasValue, "Not Raised");
|
||||||
|
if (removedItem.HasValue)
|
||||||
|
Assert.AreEqual(1, removedItem.Value, "Incorrect Value");
|
||||||
|
|
||||||
|
removedItem = null;
|
||||||
|
|
||||||
|
test.Enqueue(5);
|
||||||
|
|
||||||
|
Assert.True(removedItem.HasValue, "Not Raised");
|
||||||
|
if (removedItem.HasValue)
|
||||||
|
Assert.AreEqual(2, removedItem.Value, "Incorrect Value");
|
||||||
|
|
||||||
|
removedItem = null;
|
||||||
|
|
||||||
|
test.MaxSize = 4;
|
||||||
|
|
||||||
|
test.Enqueue(6);
|
||||||
|
|
||||||
|
Assert.False(removedItem.HasValue, "Raised Early");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
|
using ICD.Common.Utils.EventArguments;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Collections
|
namespace ICD.Common.Utils.Collections
|
||||||
{
|
{
|
||||||
@@ -15,6 +17,8 @@ namespace ICD.Common.Utils.Collections
|
|||||||
private readonly LinkedList<TContents> m_Collection;
|
private readonly LinkedList<TContents> m_Collection;
|
||||||
private int m_MaxSize;
|
private int m_MaxSize;
|
||||||
|
|
||||||
|
public event EventHandler<GenericEventArgs<TContents>> OnItemTrimmed;
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -141,7 +145,11 @@ namespace ICD.Common.Utils.Collections
|
|||||||
private void Trim()
|
private void Trim()
|
||||||
{
|
{
|
||||||
while (Count > MaxSize)
|
while (Count > MaxSize)
|
||||||
|
{
|
||||||
|
TContents removed = m_Collection.First.Value;
|
||||||
m_Collection.RemoveFirst();
|
m_Collection.RemoveFirst();
|
||||||
|
OnItemTrimmed.Raise(this, new GenericEventArgs<TContents>(removed));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
Reference in New Issue
Block a user