Added null-check for destructor and health-checker

Added null-check for creator in validate
Simlivied constructor using default values
master
Alexander B 5 years ago
parent 98c732f6ee
commit 3a6990d17f

@ -40,35 +40,6 @@ namespace eu.railduction.netcore.dll.ManagedPool
/// <summary>If more items are in the pool that the buffer, it will delete/kill the overflowing items</summary>
public int DecreaseBuffer { get => decreaseBuffer; set => decreaseBuffer = value; }
/// <summary>
/// Creates a managed pool<para/>
/// Will automatically spawn <paramref name="min"/> items
/// </summary>
/// <param name="min">The minimum amount of items in the pool</param>
/// <param name="max">The maximum amount of items in the pool</param>
/// <param name="batch">Amount if items being spawned at a time if needed</param>
/// <param name="increaseBuffer">If less items are in the pool than the <paramref name="increaseBuffer"/>, it will spawn <paramref name="batch"/>-amount</param>
/// <param name="decreaseBuffer">If more items are in the pool than the <paramref name="decreaseBuffer"/>, it will delete/kill the overflowing items</param>
/// <param name="creator">Method to handle item-spawning (needs to return the item-type)</param>
/// <param name="destructor">Method to handle item-deleting/killing (needs to take the item-type as parameter)</param>
public Pool(int min, int max, int batch, int increaseBuffer, int decreaseBuffer, Func<T> creator, Action<T> destructor)
{
this.min = min;
this.max = max;
this.batch = batch;
this.increaseBuffer = increaseBuffer;
this.decreaseBuffer = decreaseBuffer;
// Validate objects state
validate();
this.creator = creator;
this.destructor = destructor;
// Create the minimum amount of objects
Task.Run(() => createNewObjects(min));
}
/// <summary>
/// Creates a managed pool<para/>
/// Will automatically spawn <paramref name="min"/> items<para/>
@ -83,7 +54,7 @@ namespace eu.railduction.netcore.dll.ManagedPool
/// <param name="destructor">Method to handle item-deleting/killing</param>
/// <param name="healthChecker">Method to handle item-health-checking (useful for unstable items)
/// <para/>While running the health-check, the Thread will be blocked!</param>
public Pool(int min, int max, int batch, int increaseBuffer, int decreaseBuffer, Func<T> creator, Action<T> destructor, Func<T, bool> healthChecker)
public Pool(int min, int max, int batch, int increaseBuffer, int decreaseBuffer, Func<T> creator, Action<T> destructor = null, Func<T, bool> healthChecker = null)
{
this.min = min;
this.max = max;
@ -109,6 +80,7 @@ namespace eu.railduction.netcore.dll.ManagedPool
if (batch <= 0) throw new InvalidOperationException($"Invalid parameter batch='{batch}'. Must be at least '1'!");
if (increaseBuffer <= 0) throw new InvalidOperationException($"Invalid parameter increaseBuffer='{increaseBuffer}'. Must be at least '1'!");
if (decreaseBuffer <= 0) throw new InvalidOperationException($"Invalid parameter decreaseBuffer='{decreaseBuffer}'. Must be at least '1'!");
if(creator == null) throw new InvalidOperationException($"Invalid parameter creator. Cannot be null!");
}
/// <summary>
@ -133,7 +105,11 @@ namespace eu.railduction.netcore.dll.ManagedPool
if (popped)
{
if (healthChecker(item)) // Check for item-health
// Only run healthChecker if its set
bool healthy = true;
if (healthChecker != null) healthy = healthChecker(item);
if (healthy) // Check for item-health
{
return item; // Item is healthy, return it
}
@ -218,8 +194,8 @@ namespace eu.railduction.netcore.dll.ManagedPool
{
// decrease counter
current--;
// Destruct it
destructor(item);
// Destruct it when we have a descructor (otherwise the garbage-collector will take care)
if(destructor != null) destructor(item);
}
/// <summary>

Loading…
Cancel
Save