#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2014 Nuclex Development Labs
This library is free software; you can redistribute it and/or
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Nuclex.Support.Collections {
/// Wraps a Collection and prevents users from modifying it
/// Type of items to manage in the Collection
public class ReadOnlyCollection :
ICollection,
ICollection {
/// Initializes a new read-only Collection wrapper
/// Collection that will be wrapped
public ReadOnlyCollection(ICollection collection) {
this.typedCollection = collection;
this.objectCollection = (collection as ICollection);
}
/// Determines whether the List contains the specified item
/// Item that will be checked for
/// True if the specified item is contained in the List
public bool Contains(TItem item) {
return this.typedCollection.Contains(item);
}
/// Copies the contents of the List into an array
/// Array the List will be copied into
///
/// Starting index at which to begin filling the destination array
///
public void CopyTo(TItem[] array, int arrayIndex) {
this.typedCollection.CopyTo(array, arrayIndex);
}
/// The number of items current contained in the List
public int Count {
get { return this.typedCollection.Count; }
}
/// Whether the List is write-protected
public bool IsReadOnly {
get { return true; }
}
/// Returns a new enumerator over the contents of the List
/// The new List contents enumerator
public IEnumerator GetEnumerator() {
return this.typedCollection.GetEnumerator();
}
#region ICollection<> implementation
/// Adds an item to the end of the List
/// Item that will be added to the List
void ICollection.Add(TItem item) {
throw new NotSupportedException(
"Adding items is not supported by the read-only List"
);
}
/// Removes all items from the List
void ICollection.Clear() {
throw new NotSupportedException(
"Clearing is not supported by the read-only List"
);
}
/// Removes the specified item from the List
/// Item that will be removed from the List
/// True of the specified item was found in the List and removed
bool ICollection.Remove(TItem item) {
throw new NotSupportedException(
"Removing items is not supported by the read-only List"
);
}
#endregion
#region IEnumerable implementation
/// Returns a new enumerator over the contents of the List
/// The new List contents enumerator
IEnumerator IEnumerable.GetEnumerator() {
return this.objectCollection.GetEnumerator();
}
#endregion
#region ICollection implementation
/// Copies the contents of the List into an array
/// Array the List will be copied into
///
/// Starting index at which to begin filling the destination array
///
void ICollection.CopyTo(Array array, int index) {
this.objectCollection.CopyTo(array, index);
}
/// Whether the List is synchronized for multi-threaded usage
bool ICollection.IsSynchronized {
get { return this.objectCollection.IsSynchronized; }
}
/// Synchronization root on which the List locks
object ICollection.SyncRoot {
get { return this.objectCollection.SyncRoot; }
}
#endregion
/// The wrapped Collection under its type-safe interface
private ICollection typedCollection;
/// The wrapped Collection under its object interface
private ICollection objectCollection;
}
} // namespace Nuclex.Support.Collections