Permalink
Browse files

Make RDC base class readonly

Move all throwing read-only methods into RDC<T> and expose internal
read/write methods to implementing classes.
  • Loading branch information...
1 parent 1c395b5 commit 1ebba6ecc6d8d6be463ce5690119f727cb4112d2 @niik niik committed with paulcbetts Mar 31, 2013
Showing with 136 additions and 76 deletions.
  1. +136 −76 ReactiveUI/ReactiveCollectionMixins.cs
@@ -13,6 +13,136 @@ namespace ReactiveUI
{
public abstract class ReactiveDerivedCollection<TValue> : ReactiveCollection<TValue>, IDisposable
{
+ const string readonlyExceptionMessage = "Derived collections cannot be modified.";
+
+ public override bool IsReadOnly { get { return true; } }
+
+ public override TValue this[int index]
+ {
+ get { return base[index]; }
+ set { throw new InvalidOperationException(readonlyExceptionMessage); }
+ }
+
+ public override void Add(TValue item)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalAdd(TValue item)
+ {
+ base.Add(item);
+ }
+
+ public override void AddRange(IEnumerable<TValue> collection)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalAddRange(IEnumerable<TValue> collection)
+ {
+ base.AddRange(collection);
+ }
+
+ public override void Clear()
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalClear()
+ {
+ base.Clear();
+ }
+
+ public override void Insert(int index, TValue item)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalInsert(int index, TValue item)
+ {
+ base.Insert(index, item);
+ }
+
+ public override void InsertRange(int index, IEnumerable<TValue> collection)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalInsertRange(int index, IEnumerable<TValue> collection)
+ {
+ base.InsertRange(index, collection);
+ }
+
+ public override bool Remove(TValue item)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual bool internalRemove(TValue item)
+ {
+ return base.Remove(item);
+ }
+
+ public override void RemoveAll(IEnumerable<TValue> items)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalRemoveAll(IEnumerable<TValue> items)
+ {
+ base.RemoveAll(items);
+ }
+
+ public override void RemoveAt(int index)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalRemoveAt(int index)
+ {
+ base.RemoveAt(index);
+ }
+
+ public override void RemoveRange(int index, int count)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalRemoveRange(int index, int count)
+ {
+ base.RemoveRange(index, count);
+ }
+
+ public override void Sort(Comparison<TValue> comparison)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalSort(Comparison<TValue> comparison)
+ {
+ base.Sort(comparison);
+ }
+
+ public override void Sort(IComparer<TValue> comparer = null)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalSort(IComparer<TValue> comparer = null)
+ {
+ base.Sort(comparer);
+ }
+
+ public override void Sort(int index, int count, IComparer<TValue> comparer)
+ {
+ throw new InvalidOperationException(readonlyExceptionMessage);
+ }
+
+ protected virtual void internalSort(int index, int count, IComparer<TValue> comparer)
+ {
+ base.Sort(index, count, comparer);
+ }
+
public void Dispose()
{
this.Dispose(true);
@@ -23,8 +153,6 @@ public void Dispose()
public sealed class ReactiveDerivedCollection<TSource, TValue> : ReactiveDerivedCollection<TValue>, IDisposable
{
- const string readonlyExceptionMessage = "Derived collections cannot be modified.";
-
IEnumerable<TSource> source;
Func<TSource, TValue> selector;
Func<TSource, bool> filter;
@@ -35,8 +163,6 @@ public sealed class ReactiveDerivedCollection<TSource, TValue> : ReactiveDerived
List<int> indexToSourceIndexMap;
CompositeDisposable inner;
- public override bool IsReadOnly { get { return true; } }
-
public ReactiveDerivedCollection(
IEnumerable<TSource> source,
Func<TSource, TValue> selector,
@@ -167,7 +293,7 @@ private bool canItemStayAtPosition(TValue item, int currentIndex)
private void internalReplace(int destinationIndex, TValue newItem)
{
- base[destinationIndex] = newItem;
+ base.SetItem(destinationIndex, newItem);
}
/// <summary>
@@ -264,90 +390,24 @@ public override void Reset()
}
}
- private void internalClear()
+ protected override void internalClear()
{
indexToSourceIndexMap.Clear();
- base.Clear();
+ base.internalClear();
}
private void internalInsertAndMap(int sourceIndex, TValue value)
{
int destinationIndex = positionForNewItem(sourceIndex, value);
indexToSourceIndexMap.Insert(destinationIndex, sourceIndex);
- base.Insert(destinationIndex, value);
+ base.internalInsert(destinationIndex, value);
}
- private void internalRemoveAt(int destinationIndex)
+ protected override void internalRemoveAt(int destinationIndex)
{
indexToSourceIndexMap.RemoveAt(destinationIndex);
- base.RemoveAt(destinationIndex);
- }
-
- public override int Add(object value)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void AddRange(IEnumerable<TValue> collection)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void Clear()
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void Insert(int index, TValue item)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void InsertRange(int index, IEnumerable<TValue> collection)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override bool Remove(TValue item)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void RemoveAll(IEnumerable<TValue> items)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void RemoveAt(int index)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void RemoveRange(int index, int count)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void Sort(Comparison<TValue> comparison)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void Sort(IComparer<TValue> comparer = null)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override void Sort(int index, int count, IComparer<TValue> comparer)
- {
- throw new InvalidOperationException(readonlyExceptionMessage);
- }
-
- public override TValue this[int index]
- {
- get { return base[index]; }
- set { throw new InvalidOperationException(readonlyExceptionMessage); }
+ base.internalRemoveAt(destinationIndex);
}
/// <summary>

0 comments on commit 1ebba6e

Please sign in to comment.