C# PagingHelper class

Date: 2016-03-19
namespace Code
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

    public class PagingInfo
    {
        private int _pageCount = 0;
        private int _pageIndex = 0;
        private int _pageSize = 0;

        private int _pageGroupCount = 0;
        private int _pageGroupIndex = 0;
        private int _pageGroupSize = 0;
        
        private int _itemIndex = 0;
        private int _itemIndexOnPage = 0;
        private int _itemCount = 0;

        private bool _isFirstPage = true;
        private bool _isLastPage = true;

        private bool _isFirstItem = true;
        private bool _isLastItem = true;

        private bool _isFirstItemOnPage = true;
        private bool _isLastItemOnPage = true;

        public PagingInfo(int pageSize, int pageGroupSize, int itemCount)
        {
            _pageSize = pageSize;
            _pageGroupSize = pageGroupSize;
            _itemCount = itemCount;
            Recalculate();
        }

        public static IEnumerable<keyvaluepair<int, t="">> EnumerateThroughPage(PagingInfo pagingInfo, IList list)
        {
            for (int i = pagingInfo.PageFirstItemIndex; i <= pagingInfo.PageLastItemIndex; i += 1)
            {
                yield return new KeyValuePair<int, t="">(i, list[i]);
            }
        }

        public IEnumerable<keyvaluepair<int, t="">> EnumerateThroughPageList(IList list)
        {
            for (int i = this.PageFirstItemIndex; i <= this.PageLastItemIndex; i += 1)
            {
                yield return new KeyValuePair<int, t="">(i, (T)list[i]);
            }
        }

        public IEnumerable<keyvaluepair<int, t="">> EnumerateThroughPage(IList list)
        {
            for (int i = this.PageFirstItemIndex; i <= this.PageLastItemIndex; i += 1)
            {
                yield return new KeyValuePair<int, t="">(i, list[i]);
            }
        }

        public IEnumerable EnumerateThroughPageItemIndex()
        {
            for (int i = this.PageFirstItemIndex; i <= this.PageLastItemIndex; i += 1)
            {
                yield return i;
            }
        }

        public void SetPageIndex(int pageIndex)
        {
            if (!IsPageIndexValid(pageIndex))
            {
                pageIndex = 0;
            }            

            // Set the itemIndex to the first item on that page
            if (_pageCount < 1) { _pageCount = 1; }

            if (_pageSize == 0)
            {
                SetItemIndex(0);
            }
            else
            {
                SetItemIndex(pageIndex * _pageSize);
            }
        }

        public void SetItemIndex(int itemIndex)
        {
            _itemIndex = itemIndex;
            Recalculate();
        }

        public void SetItemCount(int itemCount)
        {
            _itemCount = itemCount;
            Recalculate();
        }

        public void Recalculate()
        {
            // IN
            // Validate
            if (_itemCount < 0) { _itemCount = 0; }
            if (_itemIndex < 0 || _itemIndex > ItemCount) { _itemIndex = 0; }
            if (_pageSize < 0) { _pageSize = 0; }
            if (_pageGroupSize < 0) { _pageGroupSize = 0; }
            

            // OUT
            if (_pageSize == 0)
            {
                _pageCount = 1;    
            }
            else
            {                
                _pageCount = (_itemCount + _pageSize - 1) / _pageSize;
            }

            if (_pageSize == 0)
            {
                _pageIndex = 0;   
            }
            else
            {
                _pageIndex = (_itemIndex / _pageSize);
            }

            if (_pageGroupSize == 0)
            {
                _pageGroupCount = 1;
            }
            else
            {
                _pageGroupCount = (_pageCount + _pageGroupSize - 1) / _pageGroupSize;
            }

            if (_pageGroupSize == 0)
            {
                _pageGroupIndex = 0;
            }
            else
            {
                _pageGroupIndex = (_pageIndex / _pageGroupSize);   
            }

            if (_pageSize == 0)
            {
                _itemIndexOnPage = _itemIndex;
            }
            else
            {
                _itemIndexOnPage = (_itemIndex % _pageSize);
            }

            _isFirstPage = (_pageIndex == 0);
            _isLastPage = (_pageIndex + 1 == _pageCount);
            _isFirstItem = (_itemIndex == 0);
            _isLastItem = (_itemIndex + 1 == _itemCount);
            _isFirstItemOnPage = (_itemIndexOnPage == 0);
            _isLastItemOnPage = (_itemIndexOnPage == _pageSize - 1) || _isLastItem;
        }

        public int GetFirstPageOfGroup(int pageGroupIndex)
        {
            return pageGroupIndex * _pageGroupSize;
        }

        public int GetLastPageOfGroup(int pageGroupIndex)
        {
            return ((pageGroupIndex+1) * _pageGroupSize) - 1;
        }

        public int PageNumber { get { return _pageIndex+1; } }

        public int PageCount { get { return _pageCount; } }
        public int PageIndex { get { return _pageIndex; } }
        public int PageSize { get { return _pageSize; } }        

        public int PageGroupCount { get { return _pageGroupCount; } }
        public int PageGroupIndex { get { return _pageGroupIndex; } }
        public int PageGroupSize { get { return _pageGroupSize; } }

        public bool HasPrevPage { get { return (PageIndex > 0); } }
        public bool HasNextPage { get { return (PageCount > PageIndex+1); } }

        public bool HasPrevPageGroup { get { return (PageGroupIndex > 0); } }
        public bool HasNextPageGroup { get { return (PageGroupCount > PageGroupIndex+1); } }

        public bool IsFirstPage { get { return _isFirstPage; } }
        public bool IsLastPage { get { return _isLastPage; } }

        public int ItemIndex { get { return _itemIndex; } }
        public int ItemIndexOnPage { get { return _itemIndexOnPage; } }
        public int ItemCount { get { return _itemCount; } }

        public bool IsFirstItem { get { return _isFirstItem; } }
        public bool IsLastItem { get { return _isLastItem; } }

        public bool IsFirstItemOnPage { get { return _isFirstItemOnPage; } }
        public bool IsLastItemOnPage { get { return _isLastItemOnPage; } }

        public int PageFirstItemIndex { get { return PageIndex * PageSize; } }
        public int PageLastItemIndex { get { return Math.Min(ItemCount-1, ((PageIndex+1) * PageSize) - 1); } }

        public int PrevPageIndex { get { if (HasPrevPage) { return  _pageIndex - 1; } else { return _pageIndex; } } }
        public int NextPageIndex { get { if (HasNextPage) { return  _pageIndex + 1; } else { return _pageIndex; } } }
        

        public bool IsPageIndexValid(int pageIndex)
        { 
            return (pageIndex >= 0 && PageCount > pageIndex); 
        }
    }    
}
1060cookie-checkC# PagingHelper class