public static IEnumerable <IEnumerable <T> > InBatchesOf <T>(this IEnumerable <T> items, uint batchSize)
        {
            Guard.AgainstArgument <ArgumentOutOfRangeException>("batchSize", batchSize == 0, Exceptions.EnumerableExtensions_NonZeroBatch);

            T[] bucket = null;
            var count  = 0;

            foreach (var item in items)
            {
                if (bucket == null)
                {
                    bucket = new T[batchSize];
                }

                bucket[count++] = item;

                if (count != batchSize)
                {
                    continue;
                }

                // performance hack for array elements
                yield return(bucket.Select(x => x));

                bucket = null;
                count  = 0;
            }

            // Return the last bucket with all remaining elements
            if (bucket != null && count > 0)
            {
                yield return(bucket.Take(count));
            }
        }
        /// <summary>
        /// Creates an instance of <see cref="Money"/> witht the total value of an collection of moneys.
        /// </summary>
        /// <remarks></remarks>
        /// <param name="moneys">A not null and not empty collection of moneys.</param>
        /// <returns>An <see cref="Money"/> instance which <see cref="Amount"/> is the sum of all amounts of the moneys in the collection,
        /// and <see cref="Currency"/> the same as all the moneys in the collection.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="moneys"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="moneys"/> is empty.</exception>
        /// <exception cref="DifferentCurrencyException">If any of the currencies of <paramref name="moneys"/> differ.</exception>
        public static Money Total(IEnumerable <Money> moneys)
        {
            Guard.AgainstNullArgument(nameof(moneys), moneys);
            Guard.AgainstArgument(nameof(moneys), !moneys.Any(), "The collection of moneys cannot be empty.");

            return(moneys.Aggregate((a, b) => a + b));
        }
Beispiel #3
0
        public void GenericAgainstArgument_TrueConditionFormatMessageNoArguments_ExceptionMustProvideArguments()
        {
            string message       = "message{0}";
            bool   trueCondition = 3 > 2;

            Assert.That(() => Guard.AgainstArgument <ArgumentException>(string.Empty, trueCondition, message),
                        Throws.InstanceOf <FormatException>());
        }
Beispiel #4
0
        // asserts that the allocated amount can be allocated from the allocatable amount
        private void assertSensibleAllocation(string paramName)
        {
            // absolute values is used to simplify the support the case of debt allocation
            var allocatable = _allocatable.Abs();
            var allocated   = _totalAllocated.Abs();

            Guard.AgainstArgument(paramName, allocated > allocatable, "One cannot allocate more than the allocatable amount.");
        }
Beispiel #5
0
        public void GenericAgainstArgument_TrueConditionNoFormatMessage_ExceptionWithMessageAndParam()
        {
            string message = "message", param = "param";
            bool   trueCondition = 3 > 2;

            Assert.That(() => Guard.AgainstArgument <ArgumentNullException>(param, trueCondition, message),
                        Throws.InstanceOf <ArgumentNullException>().With.Message.StartWith(message).And
                        .With.Property("ParamName").EqualTo(param));
        }
Beispiel #6
0
        public void AgainstArgumentNoMessage_TrueCondition_ExceptionWithDefaultMessageAndParam()
        {
            string param         = "param";
            bool   trueCondition = 3 > 2;

            Assert.That(() => Guard.AgainstArgument(param, trueCondition), Throws.ArgumentException
                        .With.Message.EqualTo(new ArgumentException(string.Empty, param).Message).And
                        .With.Property("ParamName").EqualTo(param));
        }
Beispiel #7
0
        public void AgainstArgument_TrueConditionFormatMessageArguments_FormattedMessageException()
        {
            string message = "message{0}", argument = "argument", param = "param";
            bool   trueCondition = 3 > 2;

            Assert.That(() => Guard.AgainstArgument(param, trueCondition, message, argument), Throws.ArgumentException
                        .With.Message.StartsWith(string.Format(message, argument)).And
                        .With.Property("ParamName").EqualTo(param));
        }
Beispiel #8
0
        /// <summary>
        /// Initializes an instance of <see cref="ExchangeRate"/> with the provided information.
        /// </summary>
        /// <param name="from">Base currency, the currency from which the conversion is performed.</param>
        /// <param name="to">Quote currency, the currency which the conversion is performed to.</param>
        /// <param name="rate">A non-negative <see cref="decimal"/> instance representing the relative vaue of <paramref name="from"/> against <paramref name="to"/>.</param>
        /// <example>{from}= EUR, {to}= USD, {rate}=1.2500, represented as "EUR/USD 1.2500" means that one euro is exchanged for 1.2500 US dollars.</example>
        /// <exception cref="InvalidEnumArgumentException"><paramref name="from"/> or <paramref name="to"/> are undefined currencies.</exception>
        /// <exception cref="ArgumentException"><paramref name="rate"/> is negative.</exception>
        public ExchangeRate(CurrencyIsoCode from, CurrencyIsoCode to, decimal rate)
        {
            Guard.AgainstArgument("rate", rate < 0, "Non-negative");
            Enumeration.AssertDefined(from);
            Enumeration.AssertDefined(to);

            From = from;
            To   = to;
            Rate = rate;
        }
Beispiel #9
0
        public void GenericAgainstArgumentNoMessage_TrueCondition_ExceptionWithDefaultMessageAndParam()
        {
            string param         = "param";
            bool   trueCondition = 3 > 2;

            Assert.That(() => Guard.AgainstArgument <ArgumentNullException>(param, trueCondition),
                        Throws.InstanceOf <ArgumentNullException>()
                        .With.Message.EqualTo(new ArgumentNullException(param).Message).And
                        .With.Property("ParamName").EqualTo(param));
        }
        public static string Separate(this string ss, uint size, string separator)
        {
            Guard.AgainstArgument <ArgumentOutOfRangeException>("size", size == 0);

            return(ss.Safe(input =>
            {
                // RegEx pattern: (.{1,4})
                string pattern = "(.{{1,{0}}})".FormatWith(size);
                return Regex.Replace(input, pattern, m =>
                                     m.Value + (m.NextMatch().Success ?
                                                separator :
                                                string.Empty));
            }));
        }
Beispiel #11
0
        public static T FindControl <T>(this Control parentControl, string targetControlID)
            where T : Control
        {
            Guard.AgainstNullArgument("parentControl", parentControl);
            Guard.AgainstArgument("targetControlID", targetControlID.IsEmpty());
            Control control = parentControl.FindControl(targetControlID);

            if (control == null)
            {
                string   controlExtensionsNotFoundTemplate = Exceptions.ControlExtensions_NotFoundTemplate;
                string[] strArrays = new string[] { targetControlID, parentControl.ID };
                ExceptionHelper.Throw <ArgumentOutOfRangeException>(controlExtensionsNotFoundTemplate, strArrays);
            }
            return((T)control);
        }
Beispiel #12
0
        public static IEnumerable <T> ToStepped <T>(this IEnumerable <T> enumerable, uint step)
        {
            Guard.AgainstArgument <ArgumentOutOfRangeException>("step", step < 1, "Cannot be negative or zero.", new string[0]);
            using (IEnumerator <T> enumerator = enumerable.EmptyIfNull <T>().GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    yield return(enumerator.Current);

                    uint num = step;
                    while (num > 1 && enumerator.MoveNext())
                    {
                        num--;
                    }
                }
            }
        }
        public static IEnumerable <T> ToStepped <T>(this IEnumerable <T> enumerable, uint step)
        {
            Guard.AgainstArgument <ArgumentOutOfRangeException>("step", step < 1, "Cannot be negative or zero.");

            using (IEnumerator <T> enumerator = enumerable.EmptyIfNull().GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    yield return(enumerator.Current);

                    for (uint i = step; i > 1; i--)
                    {
                        if (!enumerator.MoveNext())
                        {
                            break;
                        }
                    }
                }
            }
        }
Beispiel #14
0
        public static IEnumerable <IEnumerable <T> > InBatchesOf <T>(this IEnumerable <T> items, uint batchSize)
        {
            Guard.AgainstArgument <ArgumentOutOfRangeException>("batchSize", batchSize == 0, Exceptions.EnumerableExtensions_NonZeroBatch, new string[0]);
            int num = 0;

            T[] tArray = new T[batchSize];
            foreach (T t in items)
            {
                T[] tArray1 = tArray;
                int num1    = num;
                int num2    = num1;
                num = num1 + 1;
                tArray1[checked ((int)((long)num2 % (long)batchSize))] = t;
                if ((long)num % (long)batchSize != (long)0)
                {
                    continue;
                }
                yield return(tArray);
            }
            if ((long)num % (long)batchSize > (long)0)
            {
                yield return(tArray.Take <T>((int)(num % batchSize)));
            }
        }
Beispiel #15
0
 public static string Separate(this string ss, uint size, string separator)
 {
     Guard.AgainstArgument <ArgumentOutOfRangeException>("size", size == 0);
     return(ss.Safe <string, string>((string input) => Regex.Replace(input, "(.{{1,{0}}})".FormatWith(new object[] { size }), (System.Text.RegularExpressions.Match m) => string.Concat(m.Value, (m.NextMatch().Success ? separator : string.Empty))), null));
 }
Beispiel #16
0
 public void GenericAgainstArgumentNoMessage_FalseCondition_NoException()
 {
     Assert.That(() => Guard.AgainstArgument <ArgumentNullException>("param", "asd" == null), Throws.Nothing);
 }
Beispiel #17
0
 public void AgainstArgument_FalseCondition_NoException()
 {
     Assert.DoesNotThrow(() => Guard.AgainstArgument("param", "asd" == null, "no Exception"));
 }