#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 list and prevents users from modifying it /// Type of items to manage in the set public class ReadOnlyList : IList, IList { /// Initializes a new read-only List wrapper /// List that will be wrapped public ReadOnlyList(IList list) { this.typedList = list; this.objectList = (list as IList); } /// Retrieves the index of an item within the List /// Item whose index will be returned /// The zero-based index of the specified item in the List public int IndexOf(TItem item) { return this.typedList.IndexOf(item); } /// Accesses the List item with the specified index /// Zero-based index of the List item that will be accessed public TItem this[int index] { get { return this.typedList[index]; } } /// 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.typedList.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.typedList.CopyTo(array, arrayIndex); } /// The number of items current contained in the list public int Count { get { return this.typedList.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 content enumerator public IEnumerator GetEnumerator() { return this.typedList.GetEnumerator(); } #region IList<> implementation /// Inserts an item into the list /// Zero-based index before which the item will be inserted /// Item that will be inserted into the list void IList.Insert(int index, TItem item) { throw new NotSupportedException( "Inserting items is not supported by the read-only list" ); } /// Removes an item from the list /// Zero-based index of the item that will be removed void IList.RemoveAt(int index) { throw new NotSupportedException( "Removing items is not supported by the read-only list" ); } /// Accesses the list item with the specified index /// Zero-based index of the list item that will be accessed TItem IList.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 /// 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 content enumerator IEnumerator IEnumerable.GetEnumerator() { return this.objectList.GetEnumerator(); } #endregion #region IList implementation /// Removes all items from the list void IList.Clear() { throw new NotSupportedException( "Clearing is not supported by the read-only list" ); } /// Adds an item to the end of the list /// Item that will be added to the list int IList.Add(object value) { throw new NotSupportedException( "Adding items is not supported by the read-only list" ); } /// Determines whether the List contains the specified item /// Item that will be checked for /// True if the specified item is contained in the list bool IList.Contains(object value) { return this.objectList.Contains(value); } /// Retrieves the index of an item within the list /// Item whose index will be returned /// The zero-based index of the specified item in the list int IList.IndexOf(object value) { return this.objectList.IndexOf(value); } /// Inserts an item into the list /// Zero-based index before which the item will be inserted /// Item that will be inserted into the list void IList.Insert(int index, object value) { throw new NotSupportedException( "Inserting items is not supported by the read-only list" ); } /// Whether the size of the list is fixed bool IList.IsFixedSize { get { return this.objectList.IsFixedSize; } } /// 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 void IList.Remove(object value) { throw new NotSupportedException( "Removing items is not supported by the read-only list" ); } /// Removes an item from the list /// Zero-based index of the item that will be removed void IList.RemoveAt(int index) { throw new NotSupportedException( "Removing items is not supported by the read-only list" ); } /// Accesses the list item with the specified index /// Zero-based index of the list item that will be accessed 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 /// 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.objectList.CopyTo(array, index); } /// Whether the list is synchronized for multi-threaded usage bool ICollection.IsSynchronized { get { return this.objectList.IsSynchronized; } } /// Synchronization root on which the list locks object ICollection.SyncRoot { get { return this.objectList.SyncRoot; } } #endregion /// The wrapped list under its type-safe interface private IList typedList; /// The wrapped list under its object interface private IList objectList; } } // namespace Nuclex.Support.Collections