From c7f559f8cefa4c4b90df3bff72290c8bd5b18e01 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 21 Oct 2016 22:08:34 -0400 Subject: make model project portable --- MediaBrowser.Model/Extensions/IntHelper.cs | 21 ------- MediaBrowser.Model/Extensions/LinqExtensions.cs | 84 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 21 deletions(-) delete mode 100644 MediaBrowser.Model/Extensions/IntHelper.cs create mode 100644 MediaBrowser.Model/Extensions/LinqExtensions.cs (limited to 'MediaBrowser.Model/Extensions') 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 -{ - /// - /// Isolating these helpers allow this entire project to be easily converted to Java - /// - public static class IntHelper - { - /// - /// Tries the parse culture invariant. - /// - /// The s. - /// The result. - /// true if XXXX, false otherwise. - 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 + { + /// + /// Returns all distinct elements of the given source, where "distinctness" + /// is determined via a projection and the default equality comparer for the projected type. + /// + /// + /// 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. + /// + /// Type of the source sequence + /// Type of the projected element + /// Source sequence + /// Projection for determining "distinctness" + /// A sequence consisting of distinct elements from the source sequence, + /// comparing them by the specified key projection. + + public static IEnumerable DistinctBy(this IEnumerable source, + Func keySelector) + { + return source.DistinctBy(keySelector, null); + } + + /// + /// Returns all distinct elements of the given source, where "distinctness" + /// is determined via a projection and the specified comparer for the projected type. + /// + /// + /// 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. + /// + /// Type of the source sequence + /// Type of the projected element + /// Source sequence + /// Projection for determining "distinctness" + /// The equality comparer to use to determine whether or not keys are equal. + /// If null, the default equality comparer for TSource is used. + /// A sequence consisting of distinct elements from the source sequence, + /// comparing them by the specified key projection. + + public static IEnumerable DistinctBy(this IEnumerable source, + Func keySelector, IEqualityComparer comparer) + { + if (source == null) throw new ArgumentNullException("source"); + if (keySelector == null) throw new ArgumentNullException("keySelector"); + return DistinctByImpl(source, keySelector, comparer); + } + + private static IEnumerable DistinctByImpl(IEnumerable source, + Func keySelector, IEqualityComparer comparer) + { + var knownKeys = new HashSet(comparer); + foreach (var element in source) + { + if (knownKeys.Add(keySelector(element))) + { + yield return element; + } + } + } + } +} -- cgit v1.2.3