Пример #1
0
        public static Operation Create <T>(this Operation operation, Future <T> future, TimeSpan timeout)
        {
            ArgumentAssert.IsNotNull(operation, "operation");
            ArgumentAssert.IsNotNull(future, "future");

            return(operation.Create((Action <Action>)(doneAction => future.ReactWith(value => doneAction(), e => doneAction())), timeout));
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="KeyedLazyInitializer&lt;TKey, TValue&gt;"/> class.
        /// </summary>
        /// <param name="valueFactory">The value factory that will produce a new value given a key.</param>
        /// <param name="comparer">The equality comparer that will be used to compare keys.</param>
        public KeyedLazyInitializer(Func <TKey, TValue> valueFactory, IEqualityComparer <TKey> comparer)
        {
            ArgumentAssert.IsNotNull(valueFactory, "valueFactory");

            _valueFactory = valueFactory;
            _comparer     = comparer ?? EqualityComparer <TKey> .Default;
        }
Пример #3
0
        /// <summary>
        /// Converts the <see cref="IEnumerable{T}"/> into a comma-separated string value.
        /// Warning - this method does not escape special characters.
        /// </summary>
        /// <typeparam name="T">The element type.</typeparam>
        /// <param name="values">The values.</param>
        /// <param name="toString">The function that converts elements into strings.</param>
        /// <returns>A comma-separated string value.</returns>
        /// <exception cref="ArgumentNullException">
        ///   <para><paramref name="values"/> is <see langword="null"/>.</para>
        ///   <para>- or -</para>
        ///   <para><paramref name="toString"/> is <see langword="null"/>.</para>
        /// </exception>
        public static string ToCsvString <T>(this IEnumerable <T> values, Func <T, string> toString)
        {
            ArgumentAssert.IsNotNull(values, "values");
            ArgumentAssert.IsNotNull(toString, "toString");

            using (var e = values.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return(string.Empty);
                }

                var value = toString(e.Current);

                if (!e.MoveNext())
                {
                    return(value);
                }

                var builder = new StringBuilder(value);

                do
                {
                    builder.Append(',');
                    builder.Append(toString(e.Current));
                }while (e.MoveNext());

                return(builder.ToString());
            }
        }
Пример #4
0
        /// <summary>
        /// Iterates through the collection of futures asynchronously by reacting to each one before moving to the next.
        /// This operation does not bock unless the supplied iterator pattern blocks.
        /// </summary>
        /// <param name="futures">The futures.</param>
        /// <param name="stopOnFutureErrors"><see langword="true"/> to complete with an exception if a yielded future fails; <see langword="false"/> to continue execution.</param>
        /// <returns>A future that completes after sequentially waiting for completion on each one or when it encounters an exception.</returns>
        public static Future ExecuteSequentially(this IEnumerable <Future> futures, bool stopOnFutureErrors = true)
        {
            ArgumentAssert.IsNotNull(futures, "futures");

            var iterator = new SequentialFutureIterator(futures, stopOnFutureErrors);

            iterator.Begin();
            return(iterator.Result);
        }
Пример #5
0
        /// <summary>
        /// Instructs the future to react with the specified action when the future is canceled.
        /// </summary>
        /// <param name="future">The future to subscribe to.</param>
        /// <param name="onCancel">The on error reaction.</param>
        /// <returns>This future.</returns>
        public static IFuture OnCancel(this IFuture future, Action onCancel)
        {
            ArgumentAssert.IsNotNull(future, "future");
            ArgumentAssert.IsNotNull(onCancel, "onCancel");

            return(future.OnComplete(() => { if (future.Status == FutureStatus.Canceled)
                                             {
                                                 onCancel();
                                             }
                                     }));
        }
Пример #6
0
        /// <summary>
        /// Instructs the future to react with the specified action when the future completes successfully.
        /// </summary>
        /// <param name="future">The future to subscribe to.</param>
        /// <param name="onSuccess">The on success reaction.</param>
        /// <returns>This future.</returns>
        public static IFuture OnSuccess(this IFuture future, Action onSuccess)
        {
            ArgumentAssert.IsNotNull(future, "future");
            ArgumentAssert.IsNotNull(onSuccess, "onSuccess");

            return(future.OnComplete(() => { if (future.Status == FutureStatus.Success)
                                             {
                                                 onSuccess();
                                             }
                                     }));
        }
Пример #7
0
        /// <summary>
        /// Instructs the future to react with the specified error action.
        /// </summary>
        /// <param name="future">The future to subscribe to.</param>
        /// <param name="onError">The on error reaction.</param>
        /// <returns>This future.</returns>
        public static IFuture OnError(this IFuture future, Action <Exception> onError)
        {
            ArgumentAssert.IsNotNull(future, "future");
            ArgumentAssert.IsNotNull(onError, "onError");

            return(future.OnComplete(() => { if (future.Status == FutureStatus.Failure)
                                             {
                                                 onError(future.Error);
                                             }
                                     }));
        }
Пример #8
0
        /// <summary>
        /// Instructs the future to react with the specified action when the future completes successfully.
        /// </summary>
        /// <param name="future">The future to subscribe to.</param>
        /// <param name="onSuccess">The on success reaction.</param>
        /// <returns>This future.</returns>
        public static IFuture <T> OnSuccess <T>(this IFuture <T> future, Action <T> onSuccess)
        {
            ArgumentAssert.IsNotNull(future, "future");
            ArgumentAssert.IsNotNull(onSuccess, "onSuccess");

            future.OnComplete(() => { if (future.Status == FutureStatus.Success)
                                      {
                                          onSuccess(future.Result);
                                      }
                              });
            return(future);
        }
Пример #9
0
        /// <summary>
        /// Converts the specified bytes into a hexadecimal string value.
        /// </summary>
        /// <param name="bytes">The bytes to convert.</param>
        /// <returns>The hexadecimal representation of <paramref name="bytes"/>.</returns>
        public static string ToHex(this IEnumerable <byte> bytes)
        {
            ArgumentAssert.IsNotNull(bytes, "bytes");
            StringBuilder sb = new StringBuilder();

            foreach (var b in bytes)
            {
                sb.Append(GetHexNibble(b >> 4));
                sb.Append(GetHexNibble(b & 0xF));
            }
            return(sb.ToString());
        }
Пример #10
0
        public static void ForwardTo <T>(this Future <T> source, Future <T> destination)
        {
            ArgumentAssert.IsNotNull(source, "source");
            ArgumentAssert.IsNotNull(destination, "destination");

            if (source.IsComplete)
            {
                source.CopyTo(destination);
                return;
            }

            source.OnComplete(() => source.CopyTo(destination));
        }
Пример #11
0
        /// <summary>
        /// Reads the specified xml attribute as if it were a Boolean string.
        /// Returns <paramref name="defaultValue"/> the value was not present or could not be parsed.
        /// </summary>
        /// <param name="reader">The reader to read from.</param>
        /// <param name="attributeName">The name of the attribute.</param>
        /// <param name="defaultValue">The value to return in the event that the attribute is missing or invalid.</param>
        /// <returns></returns>
        public static bool ReadBooleanAttributeOrDefault(this XmlReader reader, string attributeName, bool defaultValue = false)
        {
            ArgumentAssert.IsNotNull(reader, "reader");
            ArgumentAssert.IsNotNullOrEmpty(attributeName, "attributeName");

            var  val = reader.GetAttribute(attributeName);
            bool result;

            if (!string.IsNullOrEmpty(val) && bool.TryParse(val, out result))
            {
                return(result);
            }
            return(defaultValue);
        }
Пример #12
0
        public void ReactWith(IFutureReaction reaction)
        {
            ArgumentAssert.IsNotNull(reaction, "reaction");

            OnComplete(() =>
            {
                if (HasError)
                {
                    reaction.OnError(Error);
                }
                else
                {
                    reaction.OnCompleted();
                }
            });
        }