Added ObservableSet and ReadOnlySet wrappers (no unit tests yet)

git-svn-id: file:///srv/devel/repo-conversion/nusu@256 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-01 13:51:04 +00:00
parent 5e4de7f027
commit 2d04ad7b49
7 changed files with 508 additions and 250 deletions

View File

@ -34,9 +34,9 @@ namespace Nuclex.Support.Collections {
ICollection<TItem>, ICollection<TItem>,
ICollection, ICollection,
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged, INotifyCollectionChanged,
#endif #endif
IObservableCollection<TItem> { IObservableCollection<TItem> {
/// <summary>Raised when an item has been added to the collection</summary> /// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded; public event EventHandler<ItemEventArgs<TItem>> ItemAdded;

View File

@ -36,15 +36,15 @@ namespace Nuclex.Support.Collections {
#endif #endif
public class ObservableDictionary<TKey, TValue> : public class ObservableDictionary<TKey, TValue> :
#if !NO_SERIALIZATION #if !NO_SERIALIZATION
ISerializable, ISerializable,
IDeserializationCallback, IDeserializationCallback,
#endif #endif
IDictionary<TKey, TValue>, IDictionary<TKey, TValue>,
IDictionary, IDictionary,
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged, INotifyCollectionChanged,
#endif #endif
IObservableCollection<KeyValuePair<TKey, TValue>> { IObservableCollection<KeyValuePair<TKey, TValue>> {
#if !NO_SERIALIZATION #if !NO_SERIALIZATION
#region class SerializedDictionary #region class SerializedDictionary
@ -300,15 +300,16 @@ namespace Nuclex.Support.Collections {
/// <summary>Fires the 'Clearing' event</summary> /// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() { protected virtual void OnClearing() {
if(Clearing != null) if(Clearing != null) {
Clearing(this, EventArgs.Empty); Clearing(this, EventArgs.Empty);
}
} }
/// <summary>Fires the 'Cleared' event</summary> /// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() { protected virtual void OnCleared() {
if(Cleared != null) if(Cleared != null) {
Cleared(this, EventArgs.Empty); Cleared(this, EventArgs.Empty);
}
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) { if(CollectionChanged != null) {
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs); CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);

View File

@ -35,9 +35,9 @@ namespace Nuclex.Support.Collections {
IList, IList,
ICollection, ICollection,
#if !NO_SPECIALIZED_COLLECTIONS #if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged, INotifyCollectionChanged,
#endif #endif
IObservableCollection<TItem> { IObservableCollection<TItem> {
/// <summary>Raised when an item has been added to the collection</summary> /// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded; public event EventHandler<ItemEventArgs<TItem>> ItemAdded;

View File

@ -21,13 +21,291 @@ License along with this library
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Collections;
#if !NO_SPECIALIZED_COLLECTIONS
using System.Collections.Specialized;
#endif
namespace Nuclex.Support.Collections { namespace Nuclex.Support.Collections {
#if false /// <summary>Set which fires events when items are removed or added to it</summary>
public class ObservableSet<TItem> { /// <typeparam name="TItem">Type of items to manage in the set</typeparam>
} public class ObservableSet<TItem> :
ISet<TItem>,
ICollection<TItem>,
#if !NO_SPECIALIZED_COLLECTIONS
INotifyCollectionChanged,
#endif
IObservableCollection<TItem> {
/// <summary>Raised when an item has been added to the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemAdded;
/// <summary>Raised when an item is removed from the collection</summary>
public event EventHandler<ItemEventArgs<TItem>> ItemRemoved;
/// <summary>Raised when an item is replaced in the collection</summary>
public event EventHandler<ItemReplaceEventArgs<TItem>> ItemReplaced {
add { }
remove { }
}
/// <summary>Raised when the collection is about to be cleared</summary>
/// <remarks>
/// This could be covered by calling ItemRemoved for each item currently
/// contained in the collection, but it is often simpler and more efficient
/// to process the clearing of the entire collection as a special operation.
/// </remarks>
public event EventHandler Clearing;
/// <summary>Raised when the collection has been cleared</summary>
public event EventHandler Cleared;
#if !NO_SPECIALIZED_COLLECTIONS
/// <summary>Called when the collection has changed</summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endif #endif
/// <summary>Initializes a new observable set based on a hashed set</summary>
public ObservableSet() : this(new HashSet<TItem>()) { }
/// <summary>
/// Initializes a new observable set forwarding operations to the specified set
/// </summary>
/// <param name="set">Set operations will be forwarded to</param>
public ObservableSet(ISet<TItem> set) {
this.set = set;
}
/// <summary>Adds an item to the set</summary>
/// <param name="item">Item that will be added to the set</param>
/// <returns>
/// True if the element was added, false if it was already contained in the set
/// </returns>
public bool Add(TItem item) {
return this.set.Add(item);
}
/// <summary>Removes all elements that are contained in the collection</summary>
/// <param name="other">Collection whose elements will be removed from this set</param>
public void ExceptWith(IEnumerable<TItem> other) {
if(other == this) {
Clear();
return;
}
foreach(TItem item in other) {
if(this.set.Remove(item)) {
OnRemoved(item);
}
}
}
/// <summary>
/// Only keeps those elements in this set that are contained in the collection
/// </summary>
/// <param name="other">Other set this set will be filtered by</param>
public void IntersectWith(IEnumerable<TItem> other) {
foreach(TItem item in other) {
if(!other.Contains(item)) {
this.set.Remove(item);
OnRemoved(item);
}
}
}
/// <summary>
/// Determines whether the current set is a proper (strict) subset of a collection
/// </summary>
/// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the set is a proper subset of the specified collection</returns>
public bool IsProperSubsetOf(IEnumerable<TItem> other) {
return this.set.IsProperSubsetOf(other);
}
/// <summary>
/// Determines whether the current set is a proper (strict) superset of a collection
/// </summary>
/// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the set is a proper superset of the specified collection</returns>
public bool IsProperSupersetOf(IEnumerable<TItem> other) {
return this.set.IsProperSupersetOf(other);
}
/// <summary>Determines whether the current set is a subset of a collection</summary>
/// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the set is a subset of the specified collection</returns>
public bool IsSubsetOf(IEnumerable<TItem> other) {
return this.set.IsSubsetOf(other);
}
/// <summary>Determines whether the current set is a superset of a collection</summary>
/// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the set is a superset of the specified collection</returns>
public bool IsSupersetOf(IEnumerable<TItem> other) {
return this.set.IsSupersetOf(other);
}
/// <summary>
/// Determines if the set shares at least one common element with the collection
/// </summary>
/// <param name="other">Collection the set will be tested against</param>
/// <returns>
/// True if the set shares at least one common element with the collection
/// </returns>
public bool Overlaps(IEnumerable<TItem> other) {
return this.set.Overlaps(other);
}
/// <summary>
/// Determines whether the set contains the same elements as the specified collection
/// </summary>
/// <param name="other">Collection the set will be tested against</param>
/// <returns>True if the set contains the same elements as the collection</returns>
public bool SetEquals(IEnumerable<TItem> other) {
return this.set.SetEquals(other);
}
/// <summary>
/// Modifies the current set so that it contains only elements that are present either
/// in the current set or in the specified collection, but not both
/// </summary>
/// <param name="other">Collection the set will be excepted with</param>
public void SymmetricExceptWith(IEnumerable<TItem> other) {
foreach(TItem item in other) {
if(this.set.Remove(item)) {
OnRemoved(item);
} else {
this.Add(item);
OnAdded(item);
}
}
}
/// <summary>
/// Modifies the current set so that it contains all elements that are present in both
/// the current set and in the specified collection
/// </summary>
/// <param name="other">Collection an union will be built with</param>
public void UnionWith(IEnumerable<TItem> other) {
foreach(TItem item in other) {
if(this.set.Add(item)) {
OnAdded(item);
}
}
}
/// <summary>Removes all items from the set</summary>
public void Clear() {
OnClearing();
this.set.Clear();
OnCleared();
}
/// <summary>Determines whether the set contains the specified item</summary>
/// <param name="item">Item the set will be tested for</param>
/// <returns>True if the set contains the specified item</returns>
public bool Contains(TItem item) {
return this.set.Contains(item);
}
/// <summary>Copies the contents of the set into an array</summary>
/// <param name="array">Array the set's contents will be copied to</param>
/// <param name="arrayIndex">
/// Index in the array the first copied element will be written to
/// </param>
public void CopyTo(TItem[] array, int arrayIndex) {
this.set.CopyTo(array, arrayIndex);
}
/// <summary>Counts the number of items contained in the set</summary>
public int Count {
get { return this.set.Count; }
}
/// <summary>Determines whether the set is readonly</summary>
public bool IsReadOnly {
get { return this.set.IsReadOnly; }
}
/// <summary>Removes an item from the set</summary>
/// <param name="item">Item that will be removed from the set</param>
/// <returns>
/// True if the item was contained in the set and is now removed
/// </returns>
public bool Remove(TItem item) {
return this.set.Remove(item);
}
/// <summary>Creates an enumerator for the set's contents</summary>
/// <returns>A new enumerator for the sets contents</returns>
public IEnumerator<TItem> GetEnumerator() {
return this.set.GetEnumerator();
}
/// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(TItem item) {
if(ItemAdded != null) {
ItemAdded(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)
);
}
#endif
}
/// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(TItem item) {
if(ItemRemoved != null) {
ItemRemoved(this, new ItemEventArgs<TItem>(item));
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)
);
}
#endif
}
/// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() {
if(Clearing != null) {
Clearing(this, EventArgs.Empty);
}
}
/// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() {
if(Cleared != null) {
Cleared(this, EventArgs.Empty);
}
#if !NO_SPECIALIZED_COLLECTIONS
if(CollectionChanged != null) {
CollectionChanged(this, Constants.NotifyCollectionResetEventArgs);
}
#endif
}
/// <summary>Adds an item to the set</summary>
/// <param name="item">Item that will be added to the set</param>
void ICollection<TItem>.Add(TItem item) {
this.set.Add(item);
}
/// <summary>Creates an enumerator for the set's contents</summary>
/// <returns>A new enumerator for the sets contents</returns>
IEnumerator IEnumerable.GetEnumerator() {
return this.set.GetEnumerator();
}
/// <summary>The set being wrapped</summary>
private ISet<TItem> set;
}
} // namespace Nuclex.Support.Collections } // namespace Nuclex.Support.Collections

View File

@ -24,8 +24,8 @@ using System.Collections.Generic;
namespace Nuclex.Support.Collections { namespace Nuclex.Support.Collections {
/// <summary>Wraps a List and prevents users from modifying it</summary> /// <summary>Wraps a list and prevents users from modifying it</summary>
/// <typeparam name="TItem">Type of items to manage in the List</typeparam> /// <typeparam name="TItem">Type of items to manage in the set</typeparam>
public class ReadOnlyList<TItem> : IList<TItem>, IList { public class ReadOnlyList<TItem> : IList<TItem>, IList {
/// <summary>Initializes a new read-only List wrapper</summary> /// <summary>Initializes a new read-only List wrapper</summary>
@ -64,30 +64,30 @@ namespace Nuclex.Support.Collections {
this.typedList.CopyTo(array, arrayIndex); this.typedList.CopyTo(array, arrayIndex);
} }
/// <summary>The number of items current contained in the List</summary> /// <summary>The number of items current contained in the list</summary>
public int Count { public int Count {
get { return this.typedList.Count; } get { return this.typedList.Count; }
} }
/// <summary>Whether the List is write-protected</summary> /// <summary>Whether the list is write-protected</summary>
public bool IsReadOnly { public bool IsReadOnly {
get { return true; } get { return true; }
} }
/// <summary>Returns a new enumerator over the contents of the List</summary> /// <summary>Returns a new enumerator over the contents of the list</summary>
/// <returns>The new List contents enumerator</returns> /// <returns>The new list content enumerator</returns>
public IEnumerator<TItem> GetEnumerator() { public IEnumerator<TItem> GetEnumerator() {
return this.typedList.GetEnumerator(); return this.typedList.GetEnumerator();
} }
#region IList<> implementation #region IList<> implementation
/// <summary>Inserts an item into the List</summary> /// <summary>Inserts an item into the list</summary>
/// <param name="index">Zero-based index before which the item will be inserted</param> /// <param name="index">Zero-based index before which the item will be inserted</param>
/// <param name="item">Item that will be inserted into the List</param> /// <param name="item">Item that will be inserted into the list</param>
void IList<TItem>.Insert(int index, TItem item) { void IList<TItem>.Insert(int index, TItem item) {
throw new NotSupportedException( throw new NotSupportedException(
"Inserting items is not supported by the read-only List" "Inserting items is not supported by the read-only list"
); );
} }
@ -95,17 +95,17 @@ namespace Nuclex.Support.Collections {
/// <param name="index">Zero-based index of the item that will be removed</param> /// <param name="index">Zero-based index of the item that will be removed</param>
void IList<TItem>.RemoveAt(int index) { void IList<TItem>.RemoveAt(int index) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Removing items is not supported by the read-only list"
); );
} }
/// <summary>Accesses the List item with the specified index</summary> /// <summary>Accesses the list item with the specified index</summary>
/// <param name="index">Zero-based index of the List item that will be accessed</param> /// <param name="index">Zero-based index of the list item that will be accessed</param>
TItem IList<TItem>.this[int index] { TItem IList<TItem>.this[int index] {
get { return this.typedList[index]; } get { return this.typedList[index]; }
set { set {
throw new NotSupportedException( throw new NotSupportedException(
"Assigning items is not supported by the read-only List" "Assigning items is not supported by the read-only list"
); );
} }
} }
@ -114,27 +114,27 @@ namespace Nuclex.Support.Collections {
#region ICollection<> implementation #region ICollection<> implementation
/// <summary>Adds an item to the end of the List</summary> /// <summary>Adds an item to the end of the list</summary>
/// <param name="item">Item that will be added to the List</param> /// <param name="item">Item that will be added to the list</param>
void ICollection<TItem>.Add(TItem item) { void ICollection<TItem>.Add(TItem item) {
throw new NotSupportedException( throw new NotSupportedException(
"Adding items is not supported by the read-only List" "Adding items is not supported by the read-only list"
); );
} }
/// <summary>Removes all items from the List</summary> /// <summary>Removes all items from the List</summary>
void ICollection<TItem>.Clear() { void ICollection<TItem>.Clear() {
throw new NotSupportedException( throw new NotSupportedException(
"Clearing is not supported by the read-only List" "Clearing is not supported by the read-only list"
); );
} }
/// <summary>Removes the specified item from the List</summary> /// <summary>Removes the specified item from the list</summary>
/// <param name="item">Item that will be removed from the List</param> /// <param name="item">Item that will be removed from the list</param>
/// <returns>True of the specified item was found in the List and removed</returns> /// <returns>True of the specified item was found in the list and removed</returns>
bool ICollection<TItem>.Remove(TItem item) { bool ICollection<TItem>.Remove(TItem item) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Removing items is not supported by the read-only list"
); );
} }
@ -142,8 +142,8 @@ namespace Nuclex.Support.Collections {
#region IEnumerable implementation #region IEnumerable implementation
/// <summary>Returns a new enumerator over the contents of the List</summary> /// <summary>Returns a new enumerator over the contents of the list</summary>
/// <returns>The new List contents enumerator</returns> /// <returns>The new list content enumerator</returns>
IEnumerator IEnumerable.GetEnumerator() { IEnumerator IEnumerable.GetEnumerator() {
return this.objectList.GetEnumerator(); return this.objectList.GetEnumerator();
} }
@ -152,55 +152,55 @@ namespace Nuclex.Support.Collections {
#region IList implementation #region IList implementation
/// <summary>Removes all items from the List</summary> /// <summary>Removes all items from the list</summary>
void IList.Clear() { void IList.Clear() {
throw new NotSupportedException( throw new NotSupportedException(
"Clearing is not supported by the read-only List" "Clearing is not supported by the read-only list"
); );
} }
/// <summary>Adds an item to the end of the List</summary> /// <summary>Adds an item to the end of the list</summary>
/// <param name="value">Item that will be added to the List</param> /// <param name="value">Item that will be added to the list</param>
int IList.Add(object value) { int IList.Add(object value) {
throw new NotSupportedException( throw new NotSupportedException(
"Adding items is not supported by the read-only List" "Adding items is not supported by the read-only list"
); );
} }
/// <summary>Determines whether the List contains the specified item</summary> /// <summary>Determines whether the List contains the specified item</summary>
/// <param name="value">Item that will be checked for</param> /// <param name="value">Item that will be checked for</param>
/// <returns>True if the specified item is contained in the List</returns> /// <returns>True if the specified item is contained in the list</returns>
bool IList.Contains(object value) { bool IList.Contains(object value) {
return this.objectList.Contains(value); return this.objectList.Contains(value);
} }
/// <summary>Retrieves the index of an item within the List</summary> /// <summary>Retrieves the index of an item within the list</summary>
/// <param name="value">Item whose index will be returned</param> /// <param name="value">Item whose index will be returned</param>
/// <returns>The zero-based index of the specified item in the List</returns> /// <returns>The zero-based index of the specified item in the list</returns>
int IList.IndexOf(object value) { int IList.IndexOf(object value) {
return this.objectList.IndexOf(value); return this.objectList.IndexOf(value);
} }
/// <summary>Inserts an item into the List</summary> /// <summary>Inserts an item into the list</summary>
/// <param name="index">Zero-based index before which the item will be inserted</param> /// <param name="index">Zero-based index before which the item will be inserted</param>
/// <param name="value">Item that will be inserted into the List</param> /// <param name="value">Item that will be inserted into the list</param>
void IList.Insert(int index, object value) { void IList.Insert(int index, object value) {
throw new NotSupportedException( throw new NotSupportedException(
"Inserting items is not supported by the read-only List" "Inserting items is not supported by the read-only list"
); );
} }
/// <summary>Whether the size of the List is fixed</summary> /// <summary>Whether the size of the list is fixed</summary>
bool IList.IsFixedSize { bool IList.IsFixedSize {
get { return this.objectList.IsFixedSize; } get { return this.objectList.IsFixedSize; }
} }
/// <summary>Removes the specified item from the List</summary> /// <summary>Removes the specified item from the list</summary>
/// <param name="value">Item that will be removed from the List</param> /// <param name="value">Item that will be removed from the list</param>
/// <returns>True of the specified item was found in the List and removed</returns> /// <returns>True of the specified item was found in the list and removed</returns>
void IList.Remove(object value) { void IList.Remove(object value) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Removing items is not supported by the read-only list"
); );
} }
@ -208,17 +208,17 @@ namespace Nuclex.Support.Collections {
/// <param name="index">Zero-based index of the item that will be removed</param> /// <param name="index">Zero-based index of the item that will be removed</param>
void IList.RemoveAt(int index) { void IList.RemoveAt(int index) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Removing items is not supported by the read-only list"
); );
} }
/// <summary>Accesses the List item with the specified index</summary> /// <summary>Accesses the list item with the specified index</summary>
/// <param name="index">Zero-based index of the List item that will be accessed</param> /// <param name="index">Zero-based index of the list item that will be accessed</param>
object IList.this[int index] { object IList.this[int index] {
get { return this.objectList[index]; } get { return this.objectList[index]; }
set { set {
throw new NotSupportedException( throw new NotSupportedException(
"Assigning items is not supported by the read-only List" "Assigning items is not supported by the read-only list"
); );
} }
} }
@ -227,8 +227,8 @@ namespace Nuclex.Support.Collections {
#region ICollection implementation #region ICollection implementation
/// <summary>Copies the contents of the List into an array</summary> /// <summary>Copies the contents of the list into an array</summary>
/// <param name="array">Array the List will be copied into</param> /// <param name="array">Array the list will be copied into</param>
/// <param name="index"> /// <param name="index">
/// Starting index at which to begin filling the destination array /// Starting index at which to begin filling the destination array
/// </param> /// </param>
@ -236,21 +236,21 @@ namespace Nuclex.Support.Collections {
this.objectList.CopyTo(array, index); this.objectList.CopyTo(array, index);
} }
/// <summary>Whether the List is synchronized for multi-threaded usage</summary> /// <summary>Whether the list is synchronized for multi-threaded usage</summary>
bool ICollection.IsSynchronized { bool ICollection.IsSynchronized {
get { return this.objectList.IsSynchronized; } get { return this.objectList.IsSynchronized; }
} }
/// <summary>Synchronization root on which the List locks</summary> /// <summary>Synchronization root on which the list locks</summary>
object ICollection.SyncRoot { object ICollection.SyncRoot {
get { return this.objectList.SyncRoot; } get { return this.objectList.SyncRoot; }
} }
#endregion #endregion
/// <summary>The wrapped List under its type-safe interface</summary> /// <summary>The wrapped list under its type-safe interface</summary>
private IList<TItem> typedList; private IList<TItem> typedList;
/// <summary>The wrapped List under its object interface</summary> /// <summary>The wrapped list under its object interface</summary>
private IList objectList; private IList objectList;
} }

View File

@ -1,9 +1,43 @@
using System; #region CPL License
using System.Collections.Generic; /*
using System.Linq; Nuclex Framework
using System.Text; Copyright (C) 2002-2012 Nuclex Development Labs
namespace Nuclex.Support.Source.Collections { This library is free software; you can redistribute it and/or
class ReadOnlySet { modify it under the terms of the IBM Common Public License as
} published by the IBM Corporation; either version 1.0 of the
} License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
#if UNITTEST
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
using NMock;
namespace Nuclex.Support.Collections {
#if false
/// <summary>Unit Test for the read-only set wrapper</summary>
[TestFixture]
internal class ReadOnlySetTest {
}
#endif
} // namespace Nuclex.Support.Collections
#endif // UNITTEST

View File

@ -21,247 +21,192 @@ License along with this library
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization;
namespace Nuclex.Support.Collections { namespace Nuclex.Support.Collections {
#if false /// <summary>Wraps a set and prevents it from being modified</summary>
/// <summary>Wraps a List and prevents users from modifying it</summary> /// <typeparam name="TItem">Type of items to manage in the set</typeparam>
/// <typeparam name="TItem">Type of items to manage in the List</typeparam> public class ReadOnlySet<TItem> : ISet<TItem>, ICollection<TItem> {
#if !NO_SERIALIZATION
[Serializable]
#endif
public class ReadOnlySet<TItem> :
#if !NO_SERIALIZATION
ISerializable,
IDeserializationCallback,
#endif
ISet<TItem>,
ICollection<TItem> {
/// <summary>Initializes a new read-only List wrapper</summary> /// <summary>
/// <param name="list">List that will be wrapped</param> /// Initializes a new observable set forwarding operations to the specified set
public ReadOnlySet(ISet<TItem> list) { /// </summary>
this.typedList = list; /// <param name="set">Set operations will be forwarded to</param>
public ReadOnlySet(ISet<TItem> set) {
this.set = set;
} }
/// <summary>Retrieves the index of an item within the List</summary> /// <summary>
/// <param name="item">Item whose index will be returned</param> /// Determines whether the current set is a proper (strict) subset of a collection
/// <returns>The zero-based index of the specified item in the List</returns> /// </summary>
public int IndexOf(TItem item) { /// <param name="other">Collection against which the set will be tested</param>
return this.typedList.IndexOf(item); /// <returns>True if the set is a proper subset of the specified collection</returns>
public bool IsProperSubsetOf(IEnumerable<TItem> other) {
return this.set.IsProperSubsetOf(other);
} }
/// <summary>Accesses the List item with the specified index</summary> /// <summary>
/// <param name="index">Zero-based index of the List item that will be accessed</param> /// Determines whether the current set is a proper (strict) superset of a collection
public TItem this[int index] { /// </summary>
get { return this.typedList[index]; } /// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the set is a proper superset of the specified collection</returns>
public bool IsProperSupersetOf(IEnumerable<TItem> other) {
return this.set.IsProperSupersetOf(other);
} }
/// <summary>Determines whether the List contains the specified item</summary> /// <summary>Determines whether the current set is a subset of a collection</summary>
/// <param name="item">Item that will be checked for</param> /// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the specified item is contained in the List</returns> /// <returns>True if the set is a subset of the specified collection</returns>
public bool IsSubsetOf(IEnumerable<TItem> other) {
return this.set.IsSubsetOf(other);
}
/// <summary>Determines whether the current set is a superset of a collection</summary>
/// <param name="other">Collection against which the set will be tested</param>
/// <returns>True if the set is a superset of the specified collection</returns>
public bool IsSupersetOf(IEnumerable<TItem> other) {
return this.set.IsSupersetOf(other);
}
/// <summary>
/// Determines if the set shares at least one common element with the collection
/// </summary>
/// <param name="other">Collection the set will be tested against</param>
/// <returns>
/// True if the set shares at least one common element with the collection
/// </returns>
public bool Overlaps(IEnumerable<TItem> other) {
return this.set.Overlaps(other);
}
/// <summary>
/// Determines whether the set contains the same elements as the specified collection
/// </summary>
/// <param name="other">Collection the set will be tested against</param>
/// <returns>True if the set contains the same elements as the collection</returns>
public bool SetEquals(IEnumerable<TItem> other) {
return this.set.SetEquals(other);
}
/// <summary>Determines whether the set contains the specified item</summary>
/// <param name="item">Item the set will be tested for</param>
/// <returns>True if the set contains the specified item</returns>
public bool Contains(TItem item) { public bool Contains(TItem item) {
return this.typedList.Contains(item); return this.set.Contains(item);
} }
/// <summary>Copies the contents of the List into an array</summary> /// <summary>Copies the contents of the set into an array</summary>
/// <param name="array">Array the List will be copied into</param> /// <param name="array">Array the set's contents will be copied to</param>
/// <param name="arrayIndex"> /// <param name="arrayIndex">
/// Starting index at which to begin filling the destination array /// Index in the array the first copied element will be written to
/// </param> /// </param>
public void CopyTo(TItem[] array, int arrayIndex) { public void CopyTo(TItem[] array, int arrayIndex) {
this.typedList.CopyTo(array, arrayIndex); this.set.CopyTo(array, arrayIndex);
} }
/// <summary>The number of items current contained in the List</summary> /// <summary>Counts the number of items contained in the set</summary>
public int Count { public int Count {
get { return this.typedList.Count; } get { return this.set.Count; }
} }
/// <summary>Whether the List is write-protected</summary> /// <summary>Determines whether the set is readonly</summary>
public bool IsReadOnly { public bool IsReadOnly {
get { return true; } get { return true; }
} }
/// <summary>Returns a new enumerator over the contents of the List</summary>
/// <returns>The new List contents enumerator</returns> /// <summary>Creates an enumerator for the set's contents</summary>
/// <returns>A new enumerator for the sets contents</returns>
public IEnumerator<TItem> GetEnumerator() { public IEnumerator<TItem> GetEnumerator() {
return this.typedList.GetEnumerator(); return this.set.GetEnumerator();
} }
#region IList<> implementation /// <summary>Creates an enumerator for the set's contents</summary>
/// <returns>A new enumerator for the sets contents</returns>
IEnumerator IEnumerable.GetEnumerator() {
return this.set.GetEnumerator();
}
/// <summary>Inserts an item into the List</summary> /// <summary>
/// <param name="index">Zero-based index before which the item will be inserted</param> /// Modifies the current set so that it contains only elements that are present either
/// <param name="item">Item that will be inserted into the List</param> /// in the current set or in the specified collection, but not both
void IList<TItem>.Insert(int index, TItem item) { /// </summary>
/// <param name="other">Collection the set will be excepted with</param>
void ISet<TItem>.SymmetricExceptWith(IEnumerable<TItem> other) {
throw new NotSupportedException( throw new NotSupportedException(
"Inserting items is not supported by the read-only List" "Excepting is not supported by the read-only set"
); );
} }
/// <summary>Removes an item from the list</summary> /// <summary>
/// <param name="index">Zero-based index of the item that will be removed</param> /// Modifies the current set so that it contains all elements that are present in both
void IList<TItem>.RemoveAt(int index) { /// the current set and in the specified collection
/// </summary>
/// <param name="other">Collection an union will be built with</param>
void ISet<TItem>.UnionWith(IEnumerable<TItem> other) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Unioning is not supported by the read-only set"
); );
} }
/// <summary>Accesses the List item with the specified index</summary> /// <summary>Removes all items from the set</summary>
/// <param name="index">Zero-based index of the List item that will be accessed</param> public void Clear() {
TItem IList<ItemType>.this[int index] {
get { return this.typedList[index]; }
set {
throw new NotSupportedException(
"Assigning items is not supported by the read-only List"
);
}
}
#endregion
#region ICollection<> implementation
/// <summary>Adds an item to the end of the List</summary>
/// <param name="item">Item that will be added to the List</param>
void ICollection<TItem>.Add(TItem item) {
throw new NotSupportedException( throw new NotSupportedException(
"Adding items is not supported by the read-only List" "Clearing is not supported by the read-only set"
); );
} }
/// <summary>Removes all items from the List</summary> /// <summary>Removes an item from the set</summary>
void ICollection<TItem>.Clear() { /// <param name="item">Item that will be removed from the set</param>
throw new NotSupportedException( /// <returns>
"Clearing is not supported by the read-only List" /// True if the item was contained in the set and is now removed
); /// </returns>
}
/// <summary>Removes the specified item from the List</summary>
/// <param name="item">Item that will be removed from the List</param>
/// <returns>True of the specified item was found in the List and removed</returns>
bool ICollection<TItem>.Remove(TItem item) { bool ICollection<TItem>.Remove(TItem item) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Removing items is not supported by the read-only set"
); );
} }
#endregion /// <summary>Adds an item to the set</summary>
/// <param name="item">Item that will be added to the set</param>
#region IEnumerable implementation /// <returns>
/// True if the element was added, false if it was already contained in the set
/// <summary>Returns a new enumerator over the contents of the List</summary> /// </returns>
/// <returns>The new List contents enumerator</returns> bool ISet<TItem>.Add(TItem item) {
IEnumerator IEnumerable.GetEnumerator() {
return this.objectList.GetEnumerator();
}
#endregion
#region IList implementation
/// <summary>Removes all items from the List</summary>
void IList.Clear() {
throw new NotSupportedException( throw new NotSupportedException(
"Clearing is not supported by the read-only List" "Adding items is not supported by the read-only set"
); );
} }
/// <summary>Adds an item to the end of the List</summary> /// <summary>Removes all elements that are contained in the collection</summary>
/// <param name="value">Item that will be added to the List</param> /// <param name="other">Collection whose elements will be removed from this set</param>
int IList.Add(object value) { void ISet<TItem>.ExceptWith(IEnumerable<TItem> other) {
throw new NotSupportedException( throw new NotSupportedException(
"Adding items is not supported by the read-only List" "Excepting items is not supported by the read-only set"
); );
} }
/// <summary>Determines whether the List contains the specified item</summary> /// <summary>
/// <param name="value">Item that will be checked for</param> /// Only keeps those elements in this set that are contained in the collection
/// <returns>True if the specified item is contained in the List</returns> /// </summary>
bool IList.Contains(object value) { /// <param name="other">Other set this set will be filtered by</param>
return this.objectList.Contains(value); void ISet<TItem>.IntersectWith(IEnumerable<TItem> other) {
}
/// <summary>Retrieves the index of an item within the List</summary>
/// <param name="value">Item whose index will be returned</param>
/// <returns>The zero-based index of the specified item in the List</returns>
int IList.IndexOf(object value) {
return this.objectList.IndexOf(value);
}
/// <summary>Inserts an item into the List</summary>
/// <param name="index">Zero-based index before which the item will be inserted</param>
/// <param name="value">Item that will be inserted into the List</param>
void IList.Insert(int index, object value) {
throw new NotSupportedException( throw new NotSupportedException(
"Inserting items is not supported by the read-only List" "Intersecting items is not supported by the read-only set"
); );
} }
/// <summary>Whether the size of the List is fixed</summary> /// <summary>Adds an item to the set</summary>
bool IList.IsFixedSize { /// <param name="item">Item that will be added to the set</param>
get { return this.objectList.IsFixedSize; } void ICollection<TItem>.Add(TItem item) {
}
/// <summary>Removes the specified item from the List</summary>
/// <param name="value">Item that will be removed from the List</param>
/// <returns>True of the specified item was found in the List and removed</returns>
void IList.Remove(object value) {
throw new NotSupportedException( throw new NotSupportedException(
"Removing items is not supported by the read-only List" "Adding is not supported by the read-only set"
); );
} }
/// <summary>Removes an item from the list</summary> /// <summary>The set being wrapped</summary>
/// <param name="index">Zero-based index of the item that will be removed</param> private ISet<TItem> set;
void IList.RemoveAt(int index) {
throw new NotSupportedException(
"Removing items is not supported by the read-only List"
);
}
/// <summary>Accesses the List item with the specified index</summary>
/// <param name="index">Zero-based index of the List item that will be accessed</param>
object IList.this[int index] {
get { return this.objectList[index]; }
set {
throw new NotSupportedException(
"Assigning items is not supported by the read-only List"
);
}
}
#endregion
#region ICollection implementation
/// <summary>Copies the contents of the List into an array</summary>
/// <param name="array">Array the List will be copied into</param>
/// <param name="index">
/// Starting index at which to begin filling the destination array
/// </param>
void ICollection.CopyTo(Array array, int index) {
this.objectList.CopyTo(array, index);
}
/// <summary>Whether the List is synchronized for multi-threaded usage</summary>
bool ICollection.IsSynchronized {
get { return this.objectList.IsSynchronized; }
}
/// <summary>Synchronization root on which the List locks</summary>
object ICollection.SyncRoot {
get { return this.objectList.SyncRoot; }
}
#endregion
/// <summary>The wrapped List under its type-safe interface</summary>
private ISet<TItem> typedList;
} }
#endif
} // namespace Nuclex.Support.Collections } // namespace Nuclex.Support.Collections