mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
Adding TryLast extension methods
This commit is contained in:
@@ -3,7 +3,6 @@ using ICD.Common.Utils.Extensions;
|
|||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Tests.Extensions
|
namespace ICD.Common.Utils.Tests.Extensions
|
||||||
{
|
{
|
||||||
@@ -62,6 +61,41 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
Assert.AreEqual(true, exists);
|
Assert.AreEqual(true, exists);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase(1)]
|
||||||
|
public void TryLastTest(int expected)
|
||||||
|
{
|
||||||
|
IEnumerable<int> sequence = Enumerable.Empty<int>();
|
||||||
|
int result;
|
||||||
|
bool exists = sequence.TryLast(out result);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, result);
|
||||||
|
Assert.AreEqual(false, exists);
|
||||||
|
|
||||||
|
sequence = new[] { expected };
|
||||||
|
exists = sequence.TryLast(out result);
|
||||||
|
|
||||||
|
Assert.AreEqual(expected, result);
|
||||||
|
Assert.AreEqual(true, exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(0)]
|
||||||
|
[TestCase(1)]
|
||||||
|
public void TryLastPredicateTest(int expected)
|
||||||
|
{
|
||||||
|
IEnumerable<int> sequence = Enumerable.Range(1, 10).Except(expected);
|
||||||
|
int result;
|
||||||
|
bool exists = sequence.TryLast(i => i == expected, out result);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, result);
|
||||||
|
Assert.AreEqual(false, exists);
|
||||||
|
|
||||||
|
sequence = new[] { expected };
|
||||||
|
exists = sequence.TryLast(i => i == expected, out result);
|
||||||
|
|
||||||
|
Assert.AreEqual(expected, result);
|
||||||
|
Assert.AreEqual(true, exists);
|
||||||
|
}
|
||||||
|
|
||||||
[TestCase(0, 0)]
|
[TestCase(0, 0)]
|
||||||
[TestCase(1, 10)]
|
[TestCase(1, 10)]
|
||||||
public void TryElementAtTest(int index, int expected)
|
public void TryElementAtTest(int index, int expected)
|
||||||
|
|||||||
@@ -94,6 +94,55 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if there is at least 1 item in the sequence.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="item">Outputs the last item in the sequence.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool TryLast<T>(this IEnumerable<T> extends, out T item)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
return extends.TryLast(i => true, out item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if there is at least 1 item in the sequence.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="predicate"></param>
|
||||||
|
/// <param name="item">Outputs the last item in the sequence.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool TryLast<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
if (predicate == null)
|
||||||
|
throw new ArgumentNullException("predicate");
|
||||||
|
|
||||||
|
item = default(T);
|
||||||
|
bool output = false;
|
||||||
|
|
||||||
|
using (IEnumerator<T> iterator = extends.GetEnumerator())
|
||||||
|
{
|
||||||
|
while (iterator.MoveNext())
|
||||||
|
{
|
||||||
|
if (!predicate(iterator.Current))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
item = iterator.Current;
|
||||||
|
output = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the true if an element with the given index is in the sequence.
|
/// Returns the true if an element with the given index is in the sequence.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user