#region Apache License 2.0
/*
Nuclex .NET Framework
Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion // Apache License 2.0
using System;
using System.Collections;
using System.Collections.Generic;
#if !NO_SETS
namespace Nuclex.Support.Collections {
/// Wraps a set and prevents it from being modified
/// Type of items to manage in the set
public class ReadOnlySet : ISet, ICollection {
///
/// Initializes a new observable set forwarding operations to the specified set
///
/// Set operations will be forwarded to
public ReadOnlySet(ISet set) {
this.set = set;
}
///
/// Determines whether the current set is a proper (strict) subset of a collection
///
/// Collection against which the set will be tested
/// True if the set is a proper subset of the specified collection
public bool IsProperSubsetOf(IEnumerable other) {
return this.set.IsProperSubsetOf(other);
}
///
/// Determines whether the current set is a proper (strict) superset of a collection
///
/// Collection against which the set will be tested
/// True if the set is a proper superset of the specified collection
public bool IsProperSupersetOf(IEnumerable other) {
return this.set.IsProperSupersetOf(other);
}
/// Determines whether the current set is a subset of a collection
/// Collection against which the set will be tested
/// True if the set is a subset of the specified collection
public bool IsSubsetOf(IEnumerable other) {
return this.set.IsSubsetOf(other);
}
/// Determines whether the current set is a superset of a collection
/// Collection against which the set will be tested
/// True if the set is a superset of the specified collection
public bool IsSupersetOf(IEnumerable other) {
return this.set.IsSupersetOf(other);
}
///
/// Determines if the set shares at least one common element with the collection
///
/// Collection the set will be tested against
///
/// True if the set shares at least one common element with the collection
///
public bool Overlaps(IEnumerable other) {
return this.set.Overlaps(other);
}
///
/// Determines whether the set contains the same elements as the specified collection
///
/// Collection the set will be tested against
/// True if the set contains the same elements as the collection
public bool SetEquals(IEnumerable other) {
return this.set.SetEquals(other);
}
/// Determines whether the set contains the specified item
/// Item the set will be tested for
/// True if the set contains the specified item
public bool Contains(TItem item) {
return this.set.Contains(item);
}
/// Copies the contents of the set into an array
/// Array the set's contents will be copied to
///
/// Index in the array the first copied element will be written to
///
public void CopyTo(TItem[] array, int arrayIndex) {
this.set.CopyTo(array, arrayIndex);
}
/// Counts the number of items contained in the set
public int Count {
get { return this.set.Count; }
}
/// Determines whether the set is readonly
public bool IsReadOnly {
get { return true; }
}
/// Creates an enumerator for the set's contents
/// A new enumerator for the sets contents
public IEnumerator GetEnumerator() {
return this.set.GetEnumerator();
}
/// Creates an enumerator for the set's contents
/// A new enumerator for the sets contents
IEnumerator IEnumerable.GetEnumerator() {
return this.set.GetEnumerator();
}
///
/// Modifies the current set so that it contains only elements that are present either
/// in the current set or in the specified collection, but not both
///
/// Collection the set will be excepted with
void ISet.SymmetricExceptWith(IEnumerable other) {
throw new NotSupportedException(
"Excepting is not supported by the read-only set"
);
}
///
/// Modifies the current set so that it contains all elements that are present in both
/// the current set and in the specified collection
///
/// Collection an union will be built with
void ISet.UnionWith(IEnumerable other) {
throw new NotSupportedException(
"Unioning is not supported by the read-only set"
);
}
/// Removes all items from the set
public void Clear() {
throw new NotSupportedException(
"Clearing is not supported by the read-only set"
);
}
/// Removes an item from the set
/// Item that will be removed from the set
///
/// True if the item was contained in the set and is now removed
///
bool ICollection.Remove(TItem item) {
throw new NotSupportedException(
"Removing items is not supported by the read-only set"
);
}
/// Adds an item to the set
/// Item that will be added to the set
///
/// True if the element was added, false if it was already contained in the set
///
bool ISet.Add(TItem item) {
throw new NotSupportedException(
"Adding items is not supported by the read-only set"
);
}
/// Removes all elements that are contained in the collection
/// Collection whose elements will be removed from this set
void ISet.ExceptWith(IEnumerable other) {
throw new NotSupportedException(
"Excepting items is not supported by the read-only set"
);
}
///
/// Only keeps those elements in this set that are contained in the collection
///
/// Other set this set will be filtered by
void ISet.IntersectWith(IEnumerable other) {
throw new NotSupportedException(
"Intersecting items is not supported by the read-only set"
);
}
/// Adds an item to the set
/// Item that will be added to the set
void ICollection.Add(TItem item) {
throw new NotSupportedException(
"Adding is not supported by the read-only set"
);
}
/// The set being wrapped
private ISet set;
}
} // namespace Nuclex.Support.Collections
#endif // !NO_SETS