public bool Equals(CollectionNotification <T> other)
 {
     return(!object.ReferenceEquals(other, null) &&
            other.Kind == Kind &&
            (!HasValue || EqualityComparer <T> .Default.Equals(other.Value, Value)) &&
            (Kind != CollectionNotificationKind.OnReplaced || EqualityComparer <T> .Default.Equals(other.ReplacedValue, ReplacedValue)) &&
            (Kind != CollectionNotificationKind.Exists || other.ExistingValues.SequenceEqual(ExistingValues)));
 }
Пример #2
0
        /// <summary>
        /// Converts a <see cref="CollectionModification{T}"/> to a list of <see cref="CollectionNotification{T}"/>.
        /// </summary>
        /// <typeparam name="T">The object that provides modification information.</typeparam>
        /// <param name="modification">The <see cref="CollectionModification{T}"/> to be converted.</param>
        /// <returns>A list of <see cref="CollectionNotification{T}"/> containing
        /// <see cref="CollectionNotificationKind.OnAdded"/> when the specified <paramref name="modification"/> is <see cref="CollectionModificationKind.Add"/>,
        /// <see cref="CollectionNotificationKind.OnRemoved"/> when the specified <paramref name="modification"/> is <see cref="CollectionModificationKind.Remove"/>, or
        /// <see cref="CollectionNotificationKind.OnCleared"/> when the specified <paramref name="modification"/> is <see cref="CollectionModificationKind.Clear"/>.</returns>
        public static IList <CollectionNotification <T> > ToNotifications <T>(this CollectionModification <T> modification)
        {
            Contract.Requires(modification != null);
            Contract.Ensures(Contract.Result <IList <CollectionNotification <T> > >() != null);
            Contract.Ensures(Contract.Result <IList <CollectionNotification <T> > >().IsReadOnly);

            var list = new List <CollectionNotification <T> >();

            IList <T> values;

            switch (modification.Kind)
            {
            case CollectionModificationKind.Add:
                values = modification.Values;

                for (int i = 0; i < values.Count; i++)
                {
                    list.Add(CollectionNotification.CreateOnAdded <T>(values[i]));
                }
                break;

            case CollectionModificationKind.Remove:
                values = modification.Values;

                for (int i = 0; i < values.Count; i++)
                {
                    list.Add(CollectionNotification.CreateOnRemoved <T>(values[i]));
                }
                break;

            case CollectionModificationKind.Clear:
                list.Add(CollectionNotification.CreateOnCleared <T>());
                break;
            }

            IList <CollectionNotification <T> > result = list.AsReadOnly();

            Contract.Assume(result.IsReadOnly);

            return(result);
        }
        /// <summary>
        /// Converts <see cref="INotifyCollectionChanged.CollectionChanged"/> events into an observable sequence of <see cref="CollectionNotification{T}"/>.
        /// </summary>
        /// <typeparam name="T">The object that provides notification information.</typeparam>
        /// <param name="source">An implementation of <see cref="INotifyCollectionChanged"/> that raises events when a collection changes.</param>
        /// <remarks>
        /// An <see cref="NotifyCollectionChangedAction.Add"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnAdded"/> notifications.
        /// A <see cref="NotifyCollectionChangedAction.Remove"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnRemoved"/> notifications.
        /// A <see cref="NotifyCollectionChangedAction.Move"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnReplaced"/> notifications
        /// where <see cref="CollectionNotification{T}.Value"/> and <see cref="CollectionNotification{T}.ReplacedValue"/> refer to the same value.
        /// A <see cref="NotifyCollectionChangedAction.Replace"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnReplaced"/> notifications.
        /// A <see cref="NotifyCollectionChangedAction.Reset"/> event is projected into a single <see cref="CollectionNotificationKind.OnCleared"/> notification.
        /// </remarks>
        /// <returns>An observable sequence of <see cref="CollectionNotification{T}"/> objects corresponding to raised events.</returns>
#else
        /// <summary>
        /// Converts <see cref="INotifyCollectionChanged.CollectionChanged"/> events into an observable sequence of <see cref="CollectionNotification{T}"/>.
        /// </summary>
        /// <typeparam name="T">The object that provides notification information.</typeparam>
        /// <param name="source">An implementation of <see cref="INotifyCollectionChanged"/> that raises events when a collection changes.</param>
        /// <remarks>
        /// An <see cref="NotifyCollectionChangedAction.Add"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnAdded"/> notifications.
        /// A <see cref="NotifyCollectionChangedAction.Remove"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnRemoved"/> notifications.
        /// A <see cref="NotifyCollectionChangedAction.Replace"/> event is projected into zero or more <see cref="CollectionNotificationKind.OnReplaced"/> notifications.
        /// A <see cref="NotifyCollectionChangedAction.Reset"/> event is projected into a single <see cref="CollectionNotificationKind.OnCleared"/> notification.
        /// </remarks>
        /// <returns>An observable sequence of <see cref="CollectionNotification{T}"/> objects corresponding to raised events.</returns>
#endif
        public static IObservable <CollectionNotification <T> > AsCollectionNotifications <T>(this INotifyCollectionChanged source)
        {
            Contract.Requires(source != null);
            Contract.Ensures(Contract.Result <IObservable <CollectionNotification <T> > >() != null);

            return(Observable.FromEventPattern <NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
                       eh => source.CollectionChanged += eh,
                       eh => source.CollectionChanged -= eh)
                   .SelectMany(e =>
            {
                var args = e.EventArgs;

                switch (args.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    return EnsureSequence <T>(args.NewItems).Select(CollectionNotification.CreateOnAdded).ToObservable();

                case NotifyCollectionChangedAction.Remove:
                    return EnsureSequence <T>(args.OldItems).Select(CollectionNotification.CreateOnRemoved).ToObservable();

#if !SILVERLIGHT
                case NotifyCollectionChangedAction.Move:
                    return EnsureSequence <T>(args.OldItems).Select(value => CollectionNotification.CreateOnReplaced(value, value)).ToObservable();
#endif
                case NotifyCollectionChangedAction.Replace:
                    return EnsureSequence <T>(args.NewItems)
                    .Zip(
                        EnsureSequence <T>(args.OldItems),
                        (newValue, oldValue) => CollectionNotification.CreateOnReplaced(oldValue, newValue))
                    .ToObservable();

                case NotifyCollectionChangedAction.Reset:
                    return Observable.Return(CollectionNotification.CreateOnCleared <T>());

                default:
                    throw new InvalidOperationException();
                }
            }));
        }
        /// <summary>
        /// Converts a <see cref="CollectionNotification{T}"/> to a list of <see cref="CollectionModification{T}"/>.
        /// </summary>
        /// <typeparam name="T">The object that provides notification information.</typeparam>
        /// <param name="notification">The <see cref="CollectionNotification{T}"/> to be converted.</param>
        /// <returns>A list of <see cref="CollectionModification{T}"/> containing
        /// <see cref="CollectionModificationKind.Add"/> when the specified <paramref name="notification"/> is <see cref="CollectionNotificationKind.Exists"/> or <see cref="CollectionNotificationKind.OnAdded"/>,
        /// <see cref="CollectionModificationKind.Remove"/> followed by <see cref="CollectionModificationKind.Add"/> when the specified <paramref name="notification"/> is <see cref="CollectionNotificationKind.OnReplaced"/>,
        /// <see cref="CollectionModificationKind.Remove"/> when the specified <paramref name="notification"/> is <see cref="CollectionNotificationKind.OnRemoved"/>, or
        /// <see cref="CollectionModificationKind.Clear"/> when the specified <paramref name="notification"/> is <see cref="CollectionNotificationKind.OnCleared"/>.</returns>
        public static IList <CollectionModification <T> > ToModifications <T>(this CollectionNotification <T> notification)
        {
            Contract.Requires(notification != null);
            Contract.Ensures(Contract.Result <IList <CollectionModification <T> > >() != null);
            Contract.Ensures(Contract.Result <IList <CollectionModification <T> > >().IsReadOnly);

            var list = new List <CollectionModification <T> >();

            switch (notification.Kind)
            {
            case CollectionNotificationKind.Exists:
                list.Add(CollectionModification.CreateAdd <T>(notification.ExistingValues));
                break;

            case CollectionNotificationKind.OnAdded:
                list.Add(CollectionModification.CreateAdd <T>(notification.Value));
                break;

            case CollectionNotificationKind.OnRemoved:
                list.Add(CollectionModification.CreateRemove <T>(notification.Value));
                break;

            case CollectionNotificationKind.OnReplaced:
                list.Add(CollectionModification.CreateRemove <T>(notification.ReplacedValue));
                list.Add(CollectionModification.CreateAdd <T>(notification.Value));
                break;

            case CollectionNotificationKind.OnCleared:
                list.Add(CollectionModification.CreateClear <T>());
                break;
            }

            IList <CollectionModification <T> > result = list.AsReadOnly();

            Contract.Assume(result.IsReadOnly);

            return(result);
        }