// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace CommonLang.Geometry
{
///
/// The collection of the elements and a part of the class.
///
// TODO : [TypeConverter(typeof(ExpandableObjectConverter))]
public class CurveKeyCollection : ICollection
{
#region Private Fields
private readonly List _keys;
#endregion
#region Properties
///
/// Indexer.
///
/// The index of key in this collection.
/// at position.
public CurveKey this[int index]
{
get { return _keys[index]; }
set
{
if (value == null)
throw new ArgumentNullException();
if (index >= _keys.Count)
throw new IndexOutOfRangeException();
if (_keys[index].Position == value.Position)
_keys[index] = value;
else
{
_keys.RemoveAt(index);
_keys.Add(value);
}
}
}
///
/// Returns the count of keys in this collection.
///
public int Count
{
get { return _keys.Count; }
}
///
/// Returns false because it is not a read-only collection.
///
public bool IsReadOnly
{
get { return false; }
}
#endregion
#region Constructors
///
/// Creates a new instance of class.
///
public CurveKeyCollection()
{
_keys = new List();
}
#endregion
IEnumerator IEnumerable.GetEnumerator()
{
return _keys.GetEnumerator();
}
///
/// Adds a key to this collection.
///
/// New key for the collection.
/// Throws if is null.
/// The new key would be added respectively to a position of that key and the position of other keys.
public void Add(CurveKey item)
{
if (item == null)
throw new ArgumentNullException("item");
if (_keys.Count == 0)
{
this._keys.Add(item);
return;
}
for (int i = 0; i < this._keys.Count; i++)
{
if (item.Position < this._keys[i].Position)
{
this._keys.Insert(i, item);
return;
}
}
this._keys.Add(item);
}
///
/// Removes all keys from this collection.
///
public void Clear()
{
_keys.Clear();
}
///
/// Creates a copy of this collection.
///
/// A copy of this collection.
public CurveKeyCollection Clone()
{
CurveKeyCollection ckc = new CurveKeyCollection();
foreach (CurveKey key in this._keys)
ckc.Add(key);
return ckc;
}
///
/// Determines whether this collection contains a specific key.
///
/// The key to locate in this collection.
/// true if the key is found; false otherwise.
public bool Contains(CurveKey item)
{
return _keys.Contains(item);
}
///
/// Copies the keys of this collection to an array, starting at the array index provided.
///
/// Destination array where elements will be copied.
/// The zero-based index in the array to start copying from.
public void CopyTo(CurveKey[] array, int arrayIndex)
{
_keys.CopyTo(array, arrayIndex);
}
///
/// Returns an enumerator that iterates through the collection.
///
/// An enumerator for the .
public IEnumerator GetEnumerator()
{
return _keys.GetEnumerator();
}
///
/// Finds element in the collection and returns its index.
///
/// Element for the search.
/// Index of the element; or -1 if item is not found.
public int IndexOf(CurveKey item)
{
return _keys.IndexOf(item);
}
///
/// Removes element at the specified index.
///
/// The index which element will be removed.
public void RemoveAt(int index)
{
_keys.RemoveAt(index);
}
///
/// Removes specific element.
///
/// The element
/// true if item is successfully removed; false otherwise. This method also returns false if item was not found.
public bool Remove(CurveKey item)
{
return _keys.Remove(item);
}
}
}