4 using System.Collections;
5 using System.Collections.Generic;
7 namespace SiliconStudio.Core.Collections
13 public class ConstrainedList<T> : IList<T>
15 private readonly List<T> innerList =
new List<T>();
17 private readonly
string errorMessage;
21 Constraint = constraint;
22 ThrowException = throwException;
23 this.errorMessage = errorMessage;
28 ThrowException =
true;
34 public bool ThrowException {
get; set; }
39 public Func<ConstrainedList<T>, T,
bool> Constraint {
get; set; }
44 return innerList.GetEnumerator();
48 IEnumerator IEnumerable.GetEnumerator()
50 return GetEnumerator();
54 public void Add(T item)
56 if (CheckConstraint(item))
69 return innerList.Contains(item);
73 public void CopyTo(T[] array,
int arrayIndex)
75 innerList.CopyTo(array, arrayIndex);
81 return innerList.Remove(item);
85 public int Count {
get {
return innerList.Count; } }
88 public bool IsReadOnly {
get {
return false; } }
93 return innerList.IndexOf(item);
97 public void Insert(
int index, T item)
99 if (CheckConstraint(item))
100 innerList.Insert(index, item);
106 innerList.RemoveAt(index);
110 public T
this[
int index] {
get {
return innerList[index]; } set {
if (CheckConstraint(value)) innerList[index] = value; } }
112 private bool CheckConstraint(T item)
115 if (Constraint != null)
117 result = Constraint(
this, item);
118 if (!result && ThrowException)
119 throw new ArgumentException(errorMessage ??
"The given item does not validate the collection constraint.");
void CopyTo(T[] array, int arrayIndex)
IEnumerator< T > GetEnumerator()
ConstrainedList(Func< ConstrainedList< T >, T, bool > constraint=null, bool throwException=true, string errorMessage=null)
void Insert(int index, T item)
Represent a collection associated with a constraint. When an item is added to this collection...