#region Apache Notice /***************************************************************************** * $Header: $ * $Revision$ * $Date$ * * iBATIS.NET Data Mapper * Copyright (C) 2004 - Gilles Bayon * * * 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 #region Imports using System; using System.Collections; #endregion namespace IBatisNet.Common.Pagination { /// /// Summary description for PaginatedArrayList. /// public class PaginatedArrayList: IPaginatedList { #region Fields private static ArrayList _emptyList = new ArrayList(); private int _pageSize = 0; private int _pageIndex = 0; private IList _list = null; private IList _page = null; #endregion #region Properties /// /// /// public bool IsEmpty { get { return (_page.Count == 0); } } #endregion #region Constructor (s) / Destructor /// /// /// /// public PaginatedArrayList(int pageSize) { _pageSize = pageSize; _pageIndex = 0; _list = new ArrayList(); Repaginate(); } /// /// /// /// /// public PaginatedArrayList(int initialCapacity, int pageSize) { _pageSize = pageSize; _pageIndex = 0; _list = new ArrayList(initialCapacity); Repaginate(); } /// /// /// /// /// public PaginatedArrayList(ICollection c, int pageSize) { _pageSize = pageSize; _pageIndex = 0; _list = new ArrayList(c); Repaginate(); } #endregion #region Methods /// /// /// private void Repaginate() { if (_list.Count==0) { _page = _emptyList; } else { int start = _pageIndex * _pageSize; int end = start + _pageSize - 1; if (end >= _list.Count) { end = _list.Count - 1; } if (start >= _list.Count) { _pageIndex = 0; Repaginate(); } else if (start < 0) { _pageIndex = _list.Count / _pageSize; if (_list.Count % _pageSize == 0) { _pageIndex--; } Repaginate(); } else { _page = SubList(_list, start, end + 1); } } } /// /// Provides a view of the IList pramaeter /// from the specified position /// to the specified position . /// /// The IList elements. /// Starting position for the view of elements. /// Ending position for the view of elements. /// A view of list. /// /// /// The list that is returned is just a view, it is still backed /// by the orignal list. Any changes you make to it will be /// reflected in the orignal list. /// private IList SubList(IList list, int fromIndex, int toIndex) { return ((ArrayList)list).GetRange(fromIndex, toIndex-fromIndex); } #endregion #region IPaginatedList Members /// /// /// public int PageSize { get { return _pageSize; } } /// /// /// public bool IsFirstPage { get { return (_pageIndex == 0); } } /// /// /// public bool IsMiddlePage { get { return !(this.IsFirstPage || this.IsLastPage); } } /// /// /// public bool IsLastPage { get { return _list.Count - ((_pageIndex + 1) * _pageSize) < 1; } } /// /// /// public bool IsNextPageAvailable { get { return !this.IsLastPage; } } /// /// /// public bool IsPreviousPageAvailable { get { return !this.IsFirstPage; } } /// /// /// /// public bool NextPage() { if (this.IsNextPageAvailable) { _pageIndex++; Repaginate(); return true; } else { return false; } } /// /// /// /// public bool PreviousPage() { if (this.IsPreviousPageAvailable) { _pageIndex--; Repaginate(); return true; } else { return false; } } /// /// /// /// public void GotoPage(int pageIndex) { _pageIndex = pageIndex; Repaginate(); } /// /// /// public int PageIndex { get { return _pageIndex; } } #endregion #region IList Members /// /// /// public bool IsReadOnly { get { return _list.IsReadOnly; } } /// /// /// public object this[int index] { get { return _page[index]; } set { _list[index] = value; Repaginate(); } } /// /// /// /// public void RemoveAt(int index) { _list.RemoveAt(index); Repaginate(); } /// /// /// /// /// public void Insert(int index, object value) { _list.Insert(index, value); Repaginate(); } /// /// /// /// public void Remove(object value) { _list.Remove(value); Repaginate(); } /// /// /// /// /// public bool Contains(object value) { return _page.Contains(value); } /// /// /// public void Clear() { _list.Clear(); Repaginate(); } /// /// /// /// /// public int IndexOf(object value) { return _page.IndexOf(value); } /// /// /// /// /// public int Add(object value) { int i = _list.Add(value); Repaginate(); return i; } /// /// /// public bool IsFixedSize { get { return _list.IsFixedSize; } } #endregion #region ICollection Members /// /// /// public bool IsSynchronized { get { return _page.IsSynchronized; } } /// /// /// public int Count { get { return _page.Count; } } /// /// /// /// /// public void CopyTo(Array array, int index) { _page.CopyTo(array, index); } /// /// /// public object SyncRoot { get { return _page.SyncRoot; } } #endregion #region IEnumerable Members /// /// /// /// public IEnumerator GetEnumerator() { return _page.GetEnumerator(); } #endregion #region IEnumerator Members /// /// Sets the enumerator to its initial position, /// which is before the first element in the collection. /// public void Reset() { _page.GetEnumerator().Reset(); } /// /// Gets the current element in the page. /// public object Current { get { return _page.GetEnumerator().Current; } } /// /// Advances the enumerator to the next element of the collection. /// /// /// true if the enumerator was successfully advanced to the next element; /// false if the enumerator has passed the end of the collection. /// public bool MoveNext() { return _page.GetEnumerator().MoveNext(); } #endregion } }