Go to the documentation of this file.00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004
00005 namespace N2.Collections
00006 {
00010 public class CountFilter : ItemFilter
00011 {
00012 #region Constructors
00013
00014
00015
00016 public CountFilter(int startIndex, int maxCount)
00017 {
00018 this.startIndex = startIndex;
00019 this.maxCount = maxCount > 0 ? maxCount : int.MaxValue;
00020 }
00021 #endregion
00022
00023 #region Private Members
00024 private int startIndex;
00025 private int maxCount;
00026 private int currentIndex;
00027 #endregion
00028
00029 #region Properties
00030 public int StartIndex
00031 {
00032 get { return startIndex; }
00033 set { startIndex = value; }
00034 }
00035
00036 public int MaxCount
00037 {
00038 get { return maxCount; }
00039 set { maxCount = value; }
00040 }
00041
00042 public int CurrentIndex
00043 {
00044 get { return currentIndex; }
00045 set { currentIndex = value; }
00046 }
00047 #endregion
00048
00049 #region Methods
00050 public override void Filter(IList<ContentItem> items)
00051 {
00052 while (items.Count > maxCount + startIndex)
00053 items.RemoveAt(items.Count - 1);
00054 while (items.Count > maxCount)
00055 items.RemoveAt(0);
00056 }
00057
00059 public override bool Match(ContentItem item)
00060 {
00061 return Match(CurrentIndex++);
00062 }
00063
00064 public virtual bool Match(int itemIndex)
00065 {
00066 return itemIndex >= StartIndex
00067 && (MaxCount == int.MaxValue || itemIndex < (StartIndex + MaxCount));
00068 }
00069
00070 public virtual void Reset()
00071 {
00072 CurrentIndex = 0;
00073 }
00074 #endregion
00075
00076 #region Static Methods
00077 public static void Filter(IList<ContentItem> items, int startIndex, int maxCount)
00078 {
00079 ItemFilter.Filter(items, new CountFilter(startIndex, maxCount));
00080 }
00081 #endregion
00082
00083 #region IDisposable Members
00084
00085 public override void Dispose()
00086 {
00087 Reset();
00088 }
00089
00090 #endregion
00091 }
00092 }