2008-07-17 21:19:41 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2013-04-25 13:11:59 +00:00
|
|
|
|
Copyright (C) 2002-2013 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 {
|
|
|
|
|
|
|
|
|
|
/// <summary>Wraps a Collection and prevents users from modifying it</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
/// <typeparam name="TItem">Type of items to manage in the Collection</typeparam>
|
|
|
|
|
public class ReadOnlyCollection<TItem> :
|
|
|
|
|
ICollection<TItem>,
|
2008-07-17 20:44:40 +00:00
|
|
|
|
ICollection {
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new read-only Collection wrapper</summary>
|
|
|
|
|
/// <param name="collection">Collection that will be wrapped</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public ReadOnlyCollection(ICollection<TItem> collection) {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
this.typedCollection = collection;
|
|
|
|
|
this.objectCollection = (collection as ICollection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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.typedCollection.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.typedCollection.CopyTo(array, arrayIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>The number of items current contained in the List</summary>
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return this.typedCollection.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Whether the List is write-protected</summary>
|
|
|
|
|
public bool IsReadOnly {
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Returns a new enumerator over the contents of the List</summary>
|
|
|
|
|
/// <returns>The new List contents enumerator</returns>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
public IEnumerator<TItem> GetEnumerator() {
|
2008-07-17 20:44:40 +00:00
|
|
|
|
return this.typedCollection.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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>
|
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(
|
|
|
|
|
"Adding items is not supported by the read-only List"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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(
|
|
|
|
|
"Clearing is not supported by the read-only List"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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(
|
|
|
|
|
"Removing items is not supported by the read-only List"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region IEnumerable implementation
|
|
|
|
|
|
|
|
|
|
/// <summary>Returns a new enumerator over the contents of the List</summary>
|
|
|
|
|
/// <returns>The new List contents enumerator</returns>
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() {
|
|
|
|
|
return this.objectCollection.GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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) {
|
2008-11-27 19:02:48 +00:00
|
|
|
|
this.objectCollection.CopyTo(array, index);
|
2008-07-17 20:44:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Whether the List is synchronized for multi-threaded usage</summary>
|
|
|
|
|
bool ICollection.IsSynchronized {
|
2008-11-27 19:02:48 +00:00
|
|
|
|
get { return this.objectCollection.IsSynchronized; }
|
2008-07-17 20:44:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Synchronization root on which the List locks</summary>
|
|
|
|
|
object ICollection.SyncRoot {
|
2008-11-27 19:02:48 +00:00
|
|
|
|
get { return this.objectCollection.SyncRoot; }
|
2008-07-17 20:44:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>The wrapped Collection under its type-safe interface</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
|
private ICollection<TItem> typedCollection;
|
2008-07-17 20:44:40 +00:00
|
|
|
|
/// <summary>The wrapped Collection under its object interface</summary>
|
|
|
|
|
private ICollection objectCollection;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|