//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
///
/// Encapsulates the collection of within a control.
///
public sealed class SelectedDatesCollection : ICollection {
private ArrayList dateList;
///
/// Initializes a new instance of the class
/// with the specified date list.
///
public SelectedDatesCollection(ArrayList dateList) {
this.dateList = dateList;
}
///
/// Gets the item count of the collection.
///
public int Count {
get {
return dateList.Count;
}
}
///
/// Gets a referenced by the specified ordinal index value in the collection.
///
public DateTime this[int index] {
get {
return(DateTime) dateList[index];
}
}
///
/// Adds the specified to the end of the collection.
///
public void Add(DateTime date) {
int index;
if (!FindIndex(date.Date, out index)) {
dateList.Insert(index, date.Date);
}
}
///
/// Removes all controls from the collection.
///
public void Clear() {
dateList.Clear();
}
///
/// Returns a value indicating whether the collection contains the specified
/// date.
///
public bool Contains(DateTime date) {
int index;
return FindIndex(date.Date, out index);
}
///
///
private bool FindIndex(DateTime date, out int index) {
int n = Count;
int Min = 0;
int Max = n;
while (Min < Max) {
index = (Min + Max ) / 2;
if (date == this[index]) {
return true;
}
if (date < this[index]) {
Max = index;
}
else {
Min = index + 1;
}
}
index = Min;
return false;
}
///
/// Returns an enumerator of all controls within the collection.
///
public IEnumerator GetEnumerator() {
return dateList.GetEnumerator();
}
///
/// Copies contents from the collection to a specified with a
/// specified starting index.
///
public void CopyTo(Array array, int index) {
for (IEnumerator e = this.GetEnumerator(); e.MoveNext();)
array.SetValue(e.Current, index++);
}
///
/// Gets the object that can be used to synchronize access to the collection. In
/// this case, it is the collection itself.
///
public Object SyncRoot {
get { return this;}
}
///
/// Gets a value indicating whether the collection is read-only.
///
public bool IsReadOnly {
get { return false;}
}
///
/// Gets a value indicating whether access to the collection is synchronized
/// (thread-safe).
///
public bool IsSynchronized {
get { return false;}
}
///
/// Removes the specified date from the collection.
///
public void Remove(DateTime date) {
int index;
if (FindIndex(date.Date, out index)) {
dateList.RemoveAt(index);
}
}
///
/// Sets the contents of the to span
/// across the specified date range.
///
public void SelectRange(DateTime fromDate, DateTime toDate) {
dateList.Clear();
if (fromDate <= toDate) {
// The while loop below is safe that it is not attempting to add
// day beyond the last supported date because toDate can happen
// to be the last supported date.
dateList.Add(fromDate);
DateTime date = fromDate;
while (date < toDate) {
date = date.AddDays(1);
dateList.Add(date);
}
}
}
}
}