Exemplo n.º 1
0
        private static FixedSingleResultOpt <TSource> WhereSingleImmediate <TSource>(this IEnumerable <TSource> source)
        {
            const bool usingPredicate = false;

            if (source is IOpt <TSource> sourceOpt)
            {
                return(SingleResultOpt.GetFixedFromOpt(sourceOpt.ToFixed(), usingPredicate));
            }
            else if (source is IList <TSource> sourceList)
            {
                if (sourceList.Count > 1)
                {
                    return(SingleResultOpt.GetFixedMoreThanOne <TSource>(usingPredicate));
                }
                else if (sourceList.Count == 0)
                {
                    return(SingleResultOpt.GetFixedZero <TSource>(usingPredicate));
                }
                else
                {
                    return(SingleResultOpt.GetFixedOne(usingPredicate, sourceList[0]));
                }
            }
            else
            {
                using (var sourceEnumerator = source.GetEnumerator())
                {
                    return(EnumeratorSingleToFixedOpt(sourceEnumerator, usingPredicate));
                }
            }
        }
Exemplo n.º 2
0
        private static FixedSingleResultOpt <TSource> EnumeratorSingleToFixedOpt <TSource>(IEnumerator <TSource> source, bool usingPredicate)
        {
            if (source.MoveNext())
            {
                TSource value = source.Current;

                if (source.MoveNext())
                {
                    // More than one element
                    return(SingleResultOpt.GetFixedMoreThanOne <TSource>(usingPredicate));
                }
                else
                {
                    // Exactly one element
                    return(SingleResultOpt.GetFixedOne(usingPredicate, value));
                }
            }
            else
            {
                // No elements
                return(SingleResultOpt.GetFixedZero <TSource>(usingPredicate));
            }
        }