From 1ebce8d36c542ad5f2f4bc2d23d0e2bd401e2448 Mon Sep 17 00:00:00 2001 From: Lyndon Date: Tue, 3 Sep 2019 20:12:13 +0200 Subject: [PATCH] Extensionklassen --- .../Extensions/IDictionaryTExtensions.cs | 54 ++++++++++++ ChatController/Extensions/IListTExtensions.cs | 83 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 ChatController/Extensions/IDictionaryTExtensions.cs create mode 100644 ChatController/Extensions/IListTExtensions.cs diff --git a/ChatController/Extensions/IDictionaryTExtensions.cs b/ChatController/Extensions/IDictionaryTExtensions.cs new file mode 100644 index 0000000..b64367e --- /dev/null +++ b/ChatController/Extensions/IDictionaryTExtensions.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using System.Linq; + +namespace ChatController.Extensions +{ + public static class IDictionaryTExtensions + { + public static IDictionary> AddOrUpdateValueInDictionary(this IDictionary> pDictionary, T pKey, List pValue) + { + if (!pDictionary.ContainsKey(pKey)) + { + pDictionary.Add(pKey, pValue); + } + else + { + pDictionary[pKey] = pDictionary[pKey].Union(pValue).ToList(); + } + + return pDictionary; + } + + public static IDictionary> AddOrUpdateValueInDictionary(this IDictionary> pDictionary, T pKey, U pValue) + { + if (!pDictionary.ContainsKey(pKey)) + { + pDictionary.Add(pKey, new List { pValue }); + } + else + { + pDictionary[pKey].AddIfNotIn(pValue); + } + + return pDictionary; + } + + public static void AddAndIgnoreDuplicates(this IDictionary pDictionary, T pKey, U pValue) + { + if (pDictionary.ContainsKey(pKey)) + { + return; + } + + pDictionary.Add(pKey, pValue); + } + + public static void AddAndIgnoreDuplicates(this IDictionary pDictionary, IEnumerable> pDictionary2Add) + { + foreach (var kvp in pDictionary2Add.Where(kvp => !pDictionary.ContainsKey(kvp.Key))) + { + pDictionary.Add(kvp); + } + } + } +} diff --git a/ChatController/Extensions/IListTExtensions.cs b/ChatController/Extensions/IListTExtensions.cs new file mode 100644 index 0000000..49f3955 --- /dev/null +++ b/ChatController/Extensions/IListTExtensions.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace ChatController.Extensions +{ + public static class IListTExtensions + { + public static void AddIfNotIn(this IList pList, T pToAdd) + { + if (!pList.Contains(pToAdd)) + { + pList.Add(pToAdd); + } + } + + public static void AddRangeIfElementsNotIn(this IList pList, IEnumerable pToAdd) + { + foreach (var iItem in pToAdd) + { + pList.AddIfNotIn(iItem); + } + } + + public static void AddRange(this IList pList, IEnumerable pToAdd) + { + foreach (T iItem in pToAdd) + { + pList.Add(iItem); + } + } + + public static bool Exists(this IList pList, Predicate pMatch) + { + foreach (T iItem in pList) + { + if (pMatch(iItem)) + { + return true; + } + } + + return false; + } + + public static void MakeEqualTo(this IList plist, IList otherList) + { + var toRemove = new List(); + + foreach (T t in plist) + { + if (!otherList.Contains(t)) + { + toRemove.Add(t); + } + } + + plist.RemoveRange(toRemove); + + foreach (T t in otherList) + { + if (!plist.Contains(t)) + { + plist.Insert(otherList.IndexOf(t), t); + } + } + } + + public static void RemoveRange(this IList pList, IEnumerable pToRemove) + { + foreach (T iItem in pToRemove.ToList()) + { + pList.Remove(iItem); + } + } + + public static void RemoveRange(this IList pList, Predicate pMatch) + { + List lToDelete = pList.Where(t => pMatch(t)).ToList(); + pList.RemoveRange(lToDelete); + } + } +}