public static TResult SelectConstantByCount <T, TResult>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [CanBeNull] TResult noneConstant = default,
            [CanBeNull] TResult oneConstant  = default,
            [CanBeNull] TResult manyConstant = default)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }

            var list = source.ToList();

            if (!list.Any())
            {
                return(noneConstant);
            }

            if (list.Count == 1)
            {
                return(oneConstant);
            }

            return(manyConstant);
        }
示例#2
0
        public static bool All <T>(
            [InstantHandle, NotNull] this IEnumerable <T> source,
            [InstantHandle, NotNull] Func <T, int, bool> predicate)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            if (predicate == null)
            {
                throw Exceptions.ArgumentNull(nameof(predicate));
            }

            var i = 0;

            foreach (var elem in source)
            {
                var passed = predicate.Invoke(elem, i);
                if (!passed)
                {
                    return(false);
                }
                i++;
            }

            return(true);
        }
示例#3
0
        public static IEnumerable <T> Tap <T>(
            [NotNull] this IEnumerable <T> source,
            [NotNull, InstantHandle] Action <T, int> routine,
            [CanBeNull, InstantHandle] Func <T, int, bool> predicate = null) where T : class
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            if (routine == null)
            {
                throw Exceptions.ArgumentNull(nameof(routine));
            }

            var i = 0;

            foreach (var element in source)
            {
                if (predicate == null || predicate.Invoke(element, i))
                {
                    routine.Invoke(element, i);
                }

                yield return(element);

                i++;
            }
        }
示例#4
0
        public static TResult SelectByCount <T, TResult>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [CanBeNull, InstantHandle] Func <TResult> none   = null,
            [CanBeNull, InstantHandle] Func <T, TResult> one = null,
            [CanBeNull, InstantHandle] Func <ICollection <T>, TResult> many = null)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            none = none ?? (() => default);
            one  = one ?? (_ => default);
            many = many ?? (_ => default);

            var list = source.ToList();

            if (!list.Any())
            {
                return(none.Invoke());
            }

            if (list.Count == 1)
            {
                return(one.Invoke(list.Single()));
            }

            return(many.Invoke(list));
        }
示例#5
0
        public static bool Any <T>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [NotNull, InstantHandle] Func <T, int, bool> predicate)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            if (predicate == null)
            {
                throw Exceptions.ArgumentNull(nameof(predicate));
            }

            var i = 0;

            foreach (var item in source)
            {
                var result = predicate.Invoke(item, i);
                if (result)
                {
                    return(true);
                }

                i++;
            }

            return(false);
        }
示例#6
0
        public static void HandleByCount <T>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [CanBeNull, InstantHandle] Action noItemsHandler        = null,
            [CanBeNull, InstantHandle] Action <T> singleItemHandler = null,
            [CanBeNull, InstantHandle] Action <ICollection <T> > manyItemsHandler = null)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }

            var list = source.ToList();

            if (!list.Any())
            {
                noItemsHandler?.Invoke();
            }
            else if (list.Count == 1)
            {
                singleItemHandler?.Invoke(list.Single());
            }
            else
            {
                manyItemsHandler?.Invoke(list);
            }
        }
示例#7
0
        public static T Random <T>([NotNull, InstantHandle] this IEnumerable <T> source)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            var result = source.OrderBy(_ => Guid.NewGuid()).FirstOrDefault();

            return(result);
        }
        public static bool IsEqualityOperator([NotNull] this MethodInfo methodInfo)
        {
            if (methodInfo == null)
            {
                throw Exceptions.ArgumentNull(nameof(methodInfo));
            }

            var result = methodInfo.IsSpecialName && methodInfo.Name.Equals(EqualityOperatorName);

            return(result);
        }
        public static IReadOnlyCollection <T> ToReadOnlyCollection <T>([NotNull, InstantHandle] this IEnumerable <T> source)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            var list   = source.ToList();
            var result = new ReadOnlyCollection <T>(list);

            return(result);
        }
示例#10
0
        internal T this[int index]
        {
            get
            {
                if (index >= mCount)
                {
                    throw Exceptions.ArgumentNull(Parameter.Index);
                }

                return(mSource[index]);
            }
        }
示例#11
0
        /// <summary>
        /// Throws an exception if the value is incompatible with this option.
        /// </summary>
        /// <param name="value">The value to check.</param>
        public void CheckValue(object value)
        {
            if (value is null)
            {
                throw Exceptions.ArgumentNull(nameof(value));
            }

            if (!ValueType.IsAssignableFrom(value.GetType()))
            {
                throw Exceptions.Argument(nameof(value), "Value is incompatible with type {0}.", ValueType);
            }

            OnCheckValue(value);
        }
        public static object GetDefaultValue([NotNull] this Type type)
        {
            if (type == null)
            {
                throw Exceptions.ArgumentNull(nameof(type));
            }

            var result = typeof(TypeExt)
                         .GetMethod(nameof(GetDefaultValueImpl), BindingFlags.Static | BindingFlags.NonPublic)
                         .MakeGenericMethod(type)
                         .Invoke(null, Array.Empty <object>());

            return(result);
        }
示例#13
0
        public static IEnumerable <T> NotNull <T>(this IEnumerable <T> source)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }

            foreach (var elem in source)
            {
                if (elem != null)
                {
                    yield return(elem);
                }
            }
        }
        [ExcludeFromCodeCoverage] // Can't reach 100% in RELEASE config, but this code is trusted and the alternatives are tested.
        public static IEnumerable <T> ToListWhenDebugging <T>([NotNull] this IEnumerable <T> source)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }

            if (Debugger.IsAttached)
            {
                return(source.ToList());
            }
            else
            {
                return(source);
            }
        }
        public static IOrderedEnumerable <TElement> ThenPrioritize <TElement>(
            [NotNull] this IOrderedEnumerable <TElement> source,
            [NotNull] Func <TElement, bool> predicate)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            if (predicate == null)
            {
                throw Exceptions.ArgumentNull(nameof(predicate));
            }

            var result = source.ThenByDescending(predicate);

            return(result);
        }
        public static IEnumerable <T> ToCircularEnumerable <T>(
            [NotNull, ItemCanBeNull, InstantHandle] this IEnumerable <T> source)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }

            var enumerable = source.ToList();

            while (true)
            {
                foreach (var elem in enumerable)
                {
                    yield return(elem);
                }
            }
        }
示例#17
0
        public static T FirstCoalesce <T>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [NotNull, ItemNotNull, InstantHandle] params Func <T, bool>[] predicates)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            var matches = new List <Tuple <int, T> >();

            foreach (var elem in source)
            {
                var i = 0;
                foreach (var predicate in predicates)
                {
                    if (predicate == null)
                    {
                        throw new ArgumentException("One of the passed predicates was null.").WithSource();
                    }

                    var match = predicate.Invoke(elem);
                    if (match)
                    {
                        if (i == 0)
                        {
                            return(elem);
                        }
                        else
                        {
                            matches.Add(new Tuple <int, T>(i, elem));
                        }
                    }

                    i++;
                }
            }

            var tuple = matches.OrderBy(m => m.Item1).First();

            return(tuple.Item2);
        }
示例#18
0
        public static IEnumerable <T> Flatten <T>(
            [NotNull, InstantHandle] this IEnumerable <T> parents,
            [NotNull, InstantHandle] Func <T, T> getChild)
        {
            if (parents == null)
            {
                throw Exceptions.ArgumentNull(nameof(parents));
            }
            if (getChild == null)
            {
                throw Exceptions.ArgumentNull(nameof(getChild));
            }

            foreach (var parent in parents)
            {
                var flatParent = FlattenImpl(parent, getChild);
                foreach (var child in flatParent)
                {
                    yield return(child);
                }
            }
        }
示例#19
0
        public IOrderedEnumerable <TSource> CreateOrderedEnumerable <TKey>(Func <TSource, TKey> keySelector, IComparer <TKey> comparer, bool descending)
        {
            if (keySelector == null)
            {
                throw Exceptions.ArgumentNull(Parameter.KeySelector);
            }

            Func <TSource, CompositeKey <TCompositeKey, TKey> > compositeKeySelector = (x => new CompositeKey <TCompositeKey, TKey>(mKeySelector(x), keySelector(x)));

            IComparer <CompositeKey <TCompositeKey, TKey> > compositeComparer;

            if (descending)
            {
                compositeComparer = new CompositeKey <TCompositeKey, TKey> .Comparer(mComparer, new DescendingComparer <TKey>(comparer));
            }
            else
            {
                compositeComparer = new CompositeKey <TCompositeKey, TKey> .Comparer(mComparer, comparer ?? Comparer <TKey> .Default);
            }

            return(new OrderedEnumerable <TSource, CompositeKey <TCompositeKey, TKey> >(mSource, compositeKeySelector, compositeComparer));
        }
示例#20
0
        public static T Single <T>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [NotNull, InstantHandle] Func <T, bool> predicate,
            [CanBeNull] Exception noneError = null,
            [CanBeNull] Exception manyError = null)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }

            var elems = source.Take(2).ToList();

            if (elems.Count == 0)
            {
                throw noneError ?? Exceptions.ErrorNoMatch();
            }
            if (elems.Count == 2)
            {
                throw manyError ?? Exceptions.ErrorMoreThanOneMatch();
            }

            return(elems.Single());
        }
        public static IEnumerable <T> OrderByDependency <T, TKey>(
            [NotNull, InstantHandle] this IEnumerable <T> source,
            [NotNull] Func <T, TKey> referenceKey,
            [NotNull] Func <T, IEnumerable <TKey> > dependents,
            IEqualityComparer <TKey> keyComparer = null)
        {
            if (source == null)
            {
                throw Exceptions.ArgumentNull(nameof(source));
            }
            if (referenceKey == null)
            {
                throw Exceptions.ArgumentNull(nameof(referenceKey));
            }
            if (dependents == null)
            {
                throw Exceptions.ArgumentNull(nameof(dependents));
            }

            var clone = new List <T>(source);

            if (!clone.Any())
            {
                yield break;
            }

            var dependencyGraph = new Dictionary <T, ICollection <TKey> >();

            // keyComparer ??= EqualityComparer<TKey>.Default;
            keyComparer = keyComparer ?? EqualityComparer <TKey> .Default;

            TKey GetReferenceKey(T instance)
            {
                if (instance == null)
                {
                    throw new ArgumentException("Source contains null elements.", nameof(source));
                }
                return(referenceKey.Invoke(instance));
            }

            do
            {
                var elem = clone[0];
                if (!dependencyGraph.ContainsKey(elem))
                {
                    dependencyGraph[elem] = dependents.Invoke(elem).ToList();
                }

                var dependsOn = dependencyGraph[elem];
                if (!dependsOn.Any())
                {
                    yield return(elem);

                    clone.RemoveAt(0);
                }
                else
                {
                    var stillExisting = clone.Skip(1).Where(c => dependsOn.Contains(GetReferenceKey(c), keyComparer)).ToList();
                    if (stillExisting.Any())
                    {
                        // TODO: Sub-optimal
                        var circular = false;
                        foreach (var lookup in stillExisting)
                        {
                            // if it not yet put in the dependency graph, put it at the back of the list
                            if (!dependencyGraph.ContainsKey(lookup))
                            {
                                break;
                            }

                            var key     = GetReferenceKey(elem);
                            var matches = dependencyGraph.Where(g => g.Value.Contains(key, keyComparer)).ToList();
                            if (matches.Any())
                            {
                                foreach (var match in matches)
                                {
                                    var we       = dependencyGraph[elem];
                                    var matchKey = referenceKey.Invoke(match.Key);
                                    if (we.Contains(matchKey, keyComparer))
                                    {
                                        circular = true;
                                    }
                                }
                            }
                        }

                        if (circular)
                        {
                            throw new InvalidOperationException("Circular order dependencies detected.");
                        }

                        clone.Add(elem);
                        clone.RemoveAt(0);
                    }
                    else
                    {
                        yield return(clone[0]);

                        clone.RemoveAt(0);
                    }
                }
            } while (clone.Any());
        }