diff options
| author | Luke <luke.pulverenti@gmail.com> | 2016-12-18 00:44:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-18 00:44:33 -0500 |
| commit | e7cebb91a73354dc3e0d0b6340c9fbd6511f4406 (patch) | |
| tree | 6f1c368c766c17b7514fe749c0e92e69cd89194a /MediaBrowser.Model/Extensions | |
| parent | 025905a3e4d50b9a2e07fbf4ff0a203af6604ced (diff) | |
| parent | aaa027f3229073e9a40756c3157d41af2a442922 (diff) | |
Merge pull request #2350 from MediaBrowser/beta
Beta
Diffstat (limited to 'MediaBrowser.Model/Extensions')
| -rw-r--r-- | MediaBrowser.Model/Extensions/BoolHelper.cs | 16 | ||||
| -rw-r--r-- | MediaBrowser.Model/Extensions/DoubleHelper.cs | 21 | ||||
| -rw-r--r-- | MediaBrowser.Model/Extensions/FloatHelper.cs | 18 | ||||
| -rw-r--r-- | MediaBrowser.Model/Extensions/IntHelper.cs | 21 | ||||
| -rw-r--r-- | MediaBrowser.Model/Extensions/LinqExtensions.cs | 84 |
5 files changed, 84 insertions, 76 deletions
diff --git a/MediaBrowser.Model/Extensions/BoolHelper.cs b/MediaBrowser.Model/Extensions/BoolHelper.cs deleted file mode 100644 index 5b61f864b0..0000000000 --- a/MediaBrowser.Model/Extensions/BoolHelper.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace MediaBrowser.Model.Extensions -{ - public static class BoolHelper - { - /// <summary> - /// Tries the parse culture invariant. - /// </summary> - /// <param name="s">The s.</param> - /// <param name="result">The result.</param> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> - public static bool TryParseCultureInvariant(string s, out bool result) - { - return bool.TryParse(s, out result); - } - } -}
\ No newline at end of file diff --git a/MediaBrowser.Model/Extensions/DoubleHelper.cs b/MediaBrowser.Model/Extensions/DoubleHelper.cs deleted file mode 100644 index bcaf2d7800..0000000000 --- a/MediaBrowser.Model/Extensions/DoubleHelper.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Globalization; - -namespace MediaBrowser.Model.Extensions -{ - /// <summary> - /// Isolating these helpers allow this entire project to be easily converted to Java - /// </summary> - public static class DoubleHelper - { - /// <summary> - /// Tries the parse culture invariant. - /// </summary> - /// <param name="s">The s.</param> - /// <param name="result">The result.</param> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> - public static bool TryParseCultureInvariant(string s, out double result) - { - return double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result); - } - } -} diff --git a/MediaBrowser.Model/Extensions/FloatHelper.cs b/MediaBrowser.Model/Extensions/FloatHelper.cs deleted file mode 100644 index 171eccf931..0000000000 --- a/MediaBrowser.Model/Extensions/FloatHelper.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Globalization; - -namespace MediaBrowser.Model.Extensions -{ - public static class FloatHelper - { - /// <summary> - /// Tries the parse culture invariant. - /// </summary> - /// <param name="s">The s.</param> - /// <param name="result">The result.</param> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> - public static bool TryParseCultureInvariant(string s, out float result) - { - return float.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result); - } - } -}
\ No newline at end of file diff --git a/MediaBrowser.Model/Extensions/IntHelper.cs b/MediaBrowser.Model/Extensions/IntHelper.cs deleted file mode 100644 index 6c5f26080a..0000000000 --- a/MediaBrowser.Model/Extensions/IntHelper.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Globalization; - -namespace MediaBrowser.Model.Extensions -{ - /// <summary> - /// Isolating these helpers allow this entire project to be easily converted to Java - /// </summary> - public static class IntHelper - { - /// <summary> - /// Tries the parse culture invariant. - /// </summary> - /// <param name="s">The s.</param> - /// <param name="result">The result.</param> - /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> - public static bool TryParseCultureInvariant(string s, out int result) - { - return int.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result); - } - } -} diff --git a/MediaBrowser.Model/Extensions/LinqExtensions.cs b/MediaBrowser.Model/Extensions/LinqExtensions.cs new file mode 100644 index 0000000000..6b2bdb4c77 --- /dev/null +++ b/MediaBrowser.Model/Extensions/LinqExtensions.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Model.Extensions +{ + // MoreLINQ - Extensions to LINQ to Objects + // Copyright (c) 2008 Jonathan Skeet. All rights reserved. + // + // Licensed under the Apache License, Version 2.0 (the "License"); + // you may not use this file except in compliance with the License. + // You may obtain a copy of the License at + // + // http://www.apache.org/licenses/LICENSE-2.0 + // + // Unless required by applicable law or agreed to in writing, software + // distributed under the License is distributed on an "AS IS" BASIS, + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + // See the License for the specific language governing permissions and + // limitations under the License. + + public static class LinqExtensions + { + /// <summary> + /// Returns all distinct elements of the given source, where "distinctness" + /// is determined via a projection and the default equality comparer for the projected type. + /// </summary> + /// <remarks> + /// This operator uses deferred execution and streams the results, although + /// a set of already-seen keys is retained. If a key is seen multiple times, + /// only the first element with that key is returned. + /// </remarks> + /// <typeparam name="TSource">Type of the source sequence</typeparam> + /// <typeparam name="TKey">Type of the projected element</typeparam> + /// <param name="source">Source sequence</param> + /// <param name="keySelector">Projection for determining "distinctness"</param> + /// <returns>A sequence consisting of distinct elements from the source sequence, + /// comparing them by the specified key projection.</returns> + + public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, + Func<TSource, TKey> keySelector) + { + return source.DistinctBy(keySelector, null); + } + + /// <summary> + /// Returns all distinct elements of the given source, where "distinctness" + /// is determined via a projection and the specified comparer for the projected type. + /// </summary> + /// <remarks> + /// This operator uses deferred execution and streams the results, although + /// a set of already-seen keys is retained. If a key is seen multiple times, + /// only the first element with that key is returned. + /// </remarks> + /// <typeparam name="TSource">Type of the source sequence</typeparam> + /// <typeparam name="TKey">Type of the projected element</typeparam> + /// <param name="source">Source sequence</param> + /// <param name="keySelector">Projection for determining "distinctness"</param> + /// <param name="comparer">The equality comparer to use to determine whether or not keys are equal. + /// If null, the default equality comparer for <c>TSource</c> is used.</param> + /// <returns>A sequence consisting of distinct elements from the source sequence, + /// comparing them by the specified key projection.</returns> + + public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, + Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) + { + if (source == null) throw new ArgumentNullException("source"); + if (keySelector == null) throw new ArgumentNullException("keySelector"); + return DistinctByImpl(source, keySelector, comparer); + } + + private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source, + Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) + { + var knownKeys = new HashSet<TKey>(comparer); + foreach (var element in source) + { + if (knownKeys.Add(keySelector(element))) + { + yield return element; + } + } + } + } +} |
