using System; using System.Collections.Generic; using System.Text; namespace ScrewTurn.Wiki.SearchEngine { /// /// Contains a collection of SearchResults, without duplicates. /// /// Instance and static members are not thread-safe. public class SearchResultCollection : ICollection { /// /// Contains the collection items. /// protected List items; /// /// Initializes a new instance of the Class. /// public SearchResultCollection() : this(10) { } /// /// Initializes a new instance of the Class. /// /// The initial capacity of the collection. /// If is less than or equal to zero. public SearchResultCollection(int capacity) { if(capacity <= 0) throw new ArgumentOutOfRangeException("Invalid capacity", "capacity"); items = new List(capacity); } /// /// Adds an item to the collection. /// /// The item to add. /// If is null. /// If is laredy present in the collection. public void Add(SearchResult item) { if(item == null) throw new ArgumentNullException("item"); foreach(SearchResult r in items) { if(r.Document == item.Document) throw new ArgumentException("Item is already present in the collection", "item"); } items.Add(item); } /// /// Clears the collection. /// public void Clear() { items.Clear(); } /// /// Determines whether or not the collection contains an item. /// /// The item to check for. /// true if the collection contains item, false otherwise. public bool Contains(SearchResult item) { if(item == null) return false; else return items.Contains(item); } /// /// Retrieves the search result for a document (looking at document names). /// /// The document. /// The SearchResult object, if any, null otherwise. /// If is null. public SearchResult GetSearchResult(IDocument document) { if(document == null) throw new ArgumentNullException("document"); foreach(SearchResult r in items) { if(r.Document.Name == document.Name) return r; } return null; } /// /// Copies the collection to an array. /// /// The destination array. /// The zero-based array index at which the copy begins. /// If is outside the bounds of . public void CopyTo(SearchResult[] array, int arrayIndex) { if(array == null) throw new ArgumentNullException("array"); if(arrayIndex < 0 || arrayIndex > array.Length - 1) throw new ArgumentOutOfRangeException("arrayIndex", "Index should be greater than or equal to zero and less than the number of items in the array"); if(array.Length - arrayIndex < items.Count) throw new ArgumentOutOfRangeException("arrayIndex", "Not enough space for copying the items starting at the specified index"); items.CopyTo(array, arrayIndex); } /// /// Gets the total number of items in the collection. /// public int Count { get { return items.Count; } } /// /// Gets the capacity of the collection. /// public int Capacity { get { return items.Capacity; } } /// /// Gets a value indicating whether the collection is read-only. /// public bool IsReadOnly { get { return false; } } /// /// Removes an item from the collection. /// /// The item to remove. /// true if the item is removed, false otherwise. /// If is null. public bool Remove(SearchResult item) { if(item == null) throw new ArgumentNullException("item"); return items.Remove(item); } /// /// Returns an enumerator that iterates through the items in the collection. /// /// The enumerator. public IEnumerator GetEnumerator() { return items.GetEnumerator(); } /// /// Returns an enumerator that iterates through the items in the collection. /// /// The enumerator. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } /// /// Gets an item from the collection. /// /// The index of the item. /// The item. /// If is outside the bounds of the collection. public SearchResult this[int index] { get { if(index < 0 || index > items.Count - 1) throw new IndexOutOfRangeException("Index should be greater than or equal to zero and less than the number of items in the collection"); return items[index]; } } } }