2008-07-17 21:19:41 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2012-02-29 16:27:43 +00:00
|
|
|
|
Copyright (C) 2002-2012 Nuclex Development Labs
|
2008-07-17 21:19:41 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2008-07-17 20:44:40 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Wraps a list and prevents users from modifying it</summary>
|
|
|
|
|
/// <typeparam name="TItem">Type of items to manage in the set</typeparam>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public class ReadOnlyList<TItem> : IList<TItem>, IList {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new read-only List wrapper</summary>
|
|
|
|
|
/// <param name="list">List that will be wrapped</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public ReadOnlyList(IList<TItem> list) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
this.typedList = list;
|
|
|
|
|
this.objectList = (list as IList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Retrieves the index of an item within the List</summary>
|
|
|
|
|
/// <param name="item">Item whose index will be returned</param>
|
|
|
|
|
/// <returns>The zero-based index of the specified item in the List</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public int IndexOf(TItem item) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
return this.typedList.IndexOf(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public TItem this[int index] {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
get { return this.typedList[index]; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Determines whether the List contains the specified item</summary>
|
|
|
|
|
/// <param name="item">Item that will be checked for</param>
|
|
|
|
|
/// <returns>True if the specified item is contained in the List</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public bool Contains(TItem item) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
return this.typedList.Contains(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Copies the contents of the List into an array</summary>
|
|
|
|
|
/// <param name="array">Array the List will be copied into</param>
|
|
|
|
|
/// <param name="arrayIndex">
|
|
|
|
|
/// Starting index at which to begin filling the destination array
|
|
|
|
|
/// </param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public void CopyTo(TItem[] array, int arrayIndex) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
this.typedList.CopyTo(array, arrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>The number of items current contained in the list</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
public int Count {
|
|
|
|
|
get { return this.typedList.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Whether the list is write-protected</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
public bool IsReadOnly {
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Returns a new enumerator over the contents of the list</summary>
|
|
|
|
|
/// <returns>The new list content enumerator</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public IEnumerator<TItem> GetEnumerator() {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
return this.typedList.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IList<> implementation
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Inserts an item into the list</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
/// <param name="index">Zero-based index before which the item will be inserted</param>
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <param name="item">Item that will be inserted into the list</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
void IList<TItem>.Insert(int index, TItem item) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Inserting items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes an item from the list</summary>
|
|
|
|
|
/// <param name="index">Zero-based index of the item that will be removed</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
void IList<TItem>.RemoveAt(int index) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Removing items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <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>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
TItem IList<TItem>.this[int index] {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
get { return this.typedList[index]; }
|
|
|
|
|
set {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Assigning items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ICollection<> implementation
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Adds an item to the end of the list</summary>
|
|
|
|
|
/// <param name="item">Item that will be added to the list</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
void ICollection<TItem>.Add(TItem item) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Adding items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes all items from the List</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
void ICollection<TItem>.Clear() {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Clearing is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <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>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
bool ICollection<TItem>.Remove(TItem item) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Removing items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEnumerable implementation
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Returns a new enumerator over the contents of the list</summary>
|
|
|
|
|
/// <returns>The new list content enumerator</returns>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() {
|
|
|
|
|
return this.objectList.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IList implementation
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Removes all items from the list</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
void IList.Clear() {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Clearing is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Adds an item to the end of the list</summary>
|
|
|
|
|
/// <param name="value">Item that will be added to the list</param>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
int IList.Add(object value) {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Adding items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Determines whether the List contains the specified item</summary>
|
|
|
|
|
/// <param name="value">Item that will be checked for</param>
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <returns>True if the specified item is contained in the list</returns>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
bool IList.Contains(object value) {
|
|
|
|
|
return this.objectList.Contains(value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Retrieves the index of an item within the list</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
/// <param name="value">Item whose index will be returned</param>
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <returns>The zero-based index of the specified item in the list</returns>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
int IList.IndexOf(object value) {
|
|
|
|
|
return this.objectList.IndexOf(value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Inserts an item into the list</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
/// <param name="index">Zero-based index before which the item will be inserted</param>
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <param name="value">Item that will be inserted into the list</param>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
void IList.Insert(int index, object value) {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Inserting items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Whether the size of the list is fixed</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
bool IList.IsFixedSize {
|
2008-11-27 19:40:43 +00:00
|
|
|
|
get { return this.objectList.IsFixedSize; }
|
2008-07-17 20:44:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <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>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
void IList.Remove(object value) {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Removing items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Removes an item from the list</summary>
|
|
|
|
|
/// <param name="index">Zero-based index of the item that will be removed</param>
|
|
|
|
|
void IList.RemoveAt(int index) {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Removing items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <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>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
object IList.this[int index] {
|
|
|
|
|
get { return this.objectList[index]; }
|
|
|
|
|
set {
|
|
|
|
|
throw new NotSupportedException(
|
2012-03-01 13:51:04 +00:00
|
|
|
|
"Assigning items is not supported by the read-only list"
|
2008-07-17 20:44:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ICollection implementation
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Copies the contents of the list into an array</summary>
|
|
|
|
|
/// <param name="array">Array the list will be copied into</param>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Whether the list is synchronized for multi-threaded usage</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
bool ICollection.IsSynchronized {
|
|
|
|
|
get { return this.objectList.IsSynchronized; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>Synchronization root on which the list locks</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
object ICollection.SyncRoot {
|
|
|
|
|
get { return this.objectList.SyncRoot; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>The wrapped list under its type-safe interface</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
private IList<TItem> typedList;
|
2012-03-01 13:51:04 +00:00
|
|
|
|
/// <summary>The wrapped list under its object interface</summary>
|
2008-07-17 20:44:40 +00:00
|
|
|
|
private IList objectList;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|