From ea7945b75a5cb541597d486fa92a05425857983b Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Wed, 13 Jun 2018 03:02:17 +0300 Subject: [PATCH] Added ReplaceOne extension. --- .../Collections/Generic/AbpListExtensions.cs | 37 ++++++++++++++++ .../Generic/AbpListExtensions_Tests.cs | 42 +++++++++++++++---- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs b/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs index 2cc73c2117..171a6ac237 100644 --- a/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs +++ b/src/Volo.Abp.Core/System/Collections/Generic/AbpListExtensions.cs @@ -80,6 +80,43 @@ namespace System.Collections.Generic } } + public static void ReplaceOne(this IList source, Predicate selector, T item) + { + for (int i = 0; i < source.Count; i++) + { + if (selector(source[i])) + { + source[i] = item; + return; + } + } + } + + public static void ReplaceOne(this IList source, Predicate selector, Func itemFactory) + { + for (int i = 0; i < source.Count; i++) + { + var item = source[i]; + if (selector(item)) + { + source[i] = itemFactory(item); + return; + } + } + } + + public static void ReplaceOne(this IList source, T item, T replaceWith) + { + for (int i = 0; i < source.Count; i++) + { + if (Comparer.Default.Compare(source[i], item) == 0) + { + source[i] = replaceWith; + return; + } + } + } + public static void MoveItem(this List source, Predicate selector, int targetIndex) { if (!targetIndex.IsBetween(0, source.Count - 1)) diff --git a/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs b/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs index ceacd5f298..cc65099a77 100644 --- a/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs +++ b/test/Volo.Abp.Core.Tests/System/Collections/Generic/AbpListExtensions_Tests.cs @@ -71,10 +71,6 @@ namespace System.Collections.Generic { var list = Enumerable.Range(1, 3).ToList(); - list[0].ShouldBe(1); - list[1].ShouldBe(2); - list[2].ShouldBe(3); - list.ReplaceWhile(i => i >= 2, 42); list[0].ShouldBe(1); @@ -87,15 +83,47 @@ namespace System.Collections.Generic { var list = Enumerable.Range(1, 3).ToList(); + list.ReplaceWhile(i => i >= 2, i => i + 1); + list[0].ShouldBe(1); - list[1].ShouldBe(2); + list[1].ShouldBe(3); + list[2].ShouldBe(4); + } + + [Fact] + public void ReplaceOne_WithValue() + { + var list = Enumerable.Range(1, 3).ToList(); + + list.ReplaceOne(i => i >= 2, 42); + + list[0].ShouldBe(1); + list[1].ShouldBe(42); list[2].ShouldBe(3); + } - list.ReplaceWhile(i => i >= 2, i => i + 1); + [Fact] + public void ReplaceOne_WithFactory() + { + var list = Enumerable.Range(1, 3).ToList(); + + list.ReplaceOne(i => i >= 2, i => i + 1); list[0].ShouldBe(1); list[1].ShouldBe(3); - list[2].ShouldBe(4); + list[2].ShouldBe(3); + } + + [Fact] + public void ReplaceOne_With_Item() + { + var list = Enumerable.Range(1, 3).ToList(); + + list.ReplaceOne(2, 42); + + list[0].ShouldBe(1); + list[1].ShouldBe(42); + list[2].ShouldBe(3); } } }