diff --git a/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs b/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs index 440d086f52ea48277b5a112e0864c22515c5a992..17c2e2d4c0fb2aae0a2c7aac7751c9a2e7efc9c3 100644 --- a/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs +++ b/VectoCore/VectoCore/InputData/FileIO/JSON/JSONInputData.cs @@ -794,10 +794,7 @@ namespace TUGraz.VectoCore.InputData.FileIO.JSON } for (var i = 0; i < component.Count; i++) { - if (!_componentDigests.ContainsKey(component.Entry)) { - _componentDigests[component.Entry] = new List<string>(); - } - _componentDigests[component.Entry].Add( + _componentDigests.GetOrAdd(component.Entry, _=> new List<string>()).Add( XMLManufacturerReportReader.GetComponentDataDigestValue(xmlDoc, component.Entry, i)); } } diff --git a/VectoCore/VectoCore/Utils/DataTableExtensionMethods.cs b/VectoCore/VectoCore/Utils/DataTableExtensionMethods.cs index 3f99fb03af72be15ae21dc9110b373f2778a5015..d07745cc2f39506e86d35ade18690a8a2264d1d9 100644 --- a/VectoCore/VectoCore/Utils/DataTableExtensionMethods.cs +++ b/VectoCore/VectoCore/Utils/DataTableExtensionMethods.cs @@ -29,11 +29,16 @@ * Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology */ +using Ninject.Planning.Bindings.Resolvers; using System; using System.Collections.Generic; using System.Data; using System.Globalization; +using System.Linq; +using System.ServiceModel.Security; +using System.Windows.Forms.DataVisualization.Charting; using TUGraz.VectoCommon.Exceptions; +using TUGraz.VectoCommon.InputData; using TUGraz.VectoCommon.Utils; namespace TUGraz.VectoCore.Utils @@ -51,6 +56,22 @@ namespace TUGraz.VectoCore.Utils return defaultValue; } + public static IEnumerable<TResult> SelectRows<TResult>(this DataTable self, Func<DataRow,TResult> selector) => self.Rows.Cast<DataRow>().Select(selector); + + public static IEnumerable<TResult> SelectRows<TResult>(this DataTable self, Func<DataRow, int, TResult> selector) => self.Rows.Cast<DataRow>().Select(selector); + + public static IEnumerable<DataRow> Where(this DataTable self, Func<DataRow, bool> predicate) => self.Rows.Cast<DataRow>().Where(predicate); + + public static IEnumerable<DataRow> Where(this DataTable self, Func<DataRow, int, bool> predicate) => self.Rows.Cast<DataRow>().Where(predicate); + + public static DataRow First(this DataTable self) => self.Rows.Cast<DataRow>().First(); + + public static DataRow First(this DataTable self, Func<DataRow, bool> predicate) => self.Rows.Cast<DataRow>().First(predicate); + + public static DataRow Last(this DataTable self) => self.Rows.Cast<DataRow>().Last(); + + public static double Sum(this DataTable self, Func<DataRow, double> selector) => self.Rows.Cast<DataRow>().Sum(selector); + public static double ParseDouble(this DataRow row, int columnIndex) { return row.ParseDouble(row.Table.Columns[columnIndex]); diff --git a/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs b/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs index c4949b7be81fa89e4b28578590344a9cc5f9063c..8e65cd7a3d8c25fb3cd45fdcb60c171402e96194 100644 --- a/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs +++ b/VectoCore/VectoCore/Utils/DictionaryExtensionMethods.cs @@ -29,23 +29,52 @@ * Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology */ +using Ninject.Planning.Bindings.Resolvers; using System; using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Windows.Forms; using TUGraz.VectoCommon.Utils; namespace TUGraz.VectoCore.Utils { internal static class DictionaryExtensionMethods { - public static object GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) - { - return dictionary.TryGetValue(key, out var value) ? (object)value : DBNull.Value; - } + public static object GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) => + dictionary.TryGetValue(key, out var value) ? (object)value : DBNull.Value; public static TValue GetValueOrZero<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) - where TValue : SIBase<TValue> + where TValue : SIBase<TValue> => dictionary.TryGetValue(key, out var value) ? value : 0.SI<TValue>(); + + public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, Func<TKey, TValue> defaultConstructor) { - return dictionary.TryGetValue(key, out var value) ? value : 0.SI<TValue>(); + if (self.TryGetValue(key, out var result)) + return result; + + lock (self) { + if (!self.TryGetValue(key, out result)) { + result = defaultConstructor(key); + self.Add(key, result); + } + return result; + } } + + public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key) => + self.TryGetValue(key, out var value) ? value : default; + + public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue defaultValue) => + self.TryGetValue(key, out var value) ? value : defaultValue; + + + /// <summary> + /// Joins the items of the enumerable into a string. + /// </summary> + /// <param name="self"></param> + /// <param name="separator"></param> + /// <returns></returns> + public static string JoinString(this IEnumerable<string> self, string separator = ", ") => string.Join(separator, self ?? Enumerable.Empty<string>()); } } \ No newline at end of file