Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PageNumberAndItemNumbers"/> struct.
        /// </summary>
        /// <param name="pageNumber">
        /// Initial value for the <see cref="PageNumber"/> field.
        /// </param>
        /// <param name="pageSize">
        /// The <see cref="PageNumberAndSize.Size"/>
        /// of each page in a "paged" collection.
        /// </param>
        /// <param name="totalItems">
        /// The total number of items in a "paged" collection.
        /// </param>
        /// <param name="isLastPage">
        /// Optional signal for setting the <see cref="LastItemNumber"/>
        /// value, whether to calculate if <paramref name="pageNumber"/>
        /// is the last page or to use this value.
        /// </param>
        internal PageNumberAndItemNumbers(
            int pageNumber, byte pageSize, int totalItems, bool?isLastPage = null)
        {
            Contract.Requires <ArgumentOutOfRangeException>(
                pageNumber >= PageNumberAndSize.FirstPageNumber,
                "An ordinal page number is not a zero-based index. The number must be at least one.");
            Contract.Requires <ArgumentOutOfRangeException>(
                totalItems >= 0, "The number of items in the list must not be negative!");

            if (pageSize >= PageNumberAndSize.MinimumPageSize && (totalItems > 0))
            {
                // A page of fixed size which has items.
                this.PageNumber      = pageNumber;
                this.LastItemNumber  = pageNumber * pageSize;
                this.FirstItemNumber = this.LastItemNumber - pageSize + 1;

                // Determine whether this is the last page,
                // either by a parameter value or by calculation.
                if ((isLastPage.HasValue && isLastPage.Value) ||
                    (PagingInfoCalculator.CalculateTotalPages(pageSize, totalItems) == this.PageNumber))
                {
                    // If is the last page, replace the
                    // calculated LastItemNumber with
                    // the totalItems value.
                    this.LastItemNumber = totalItems;
                }
            }
            else
            {
                // An unbounded or empty page.
                this.PageNumber      = PageNumberAndSize.FirstPageNumber;
                this.FirstItemNumber = totalItems > 0 ? 1 : 0;
                this.LastItemNumber  = totalItems;
            }
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PagingInfo"/> struct
 /// upon deserialization of another <see cref="PagingInfo"/> which
 /// this one replaces, used for lazy initialization by the internal
 /// <see cref="Calculator"/> property.
 /// </summary>
 /// <param name="calculator">
 /// A <see cref="PagingInfoCalculator"/> value initialized from
 /// the <see cref="CurrentPage"/> and <see cref="TotalItems"/>
 /// values of a <see cref="PagingInfo"/> value to be replaced
 /// by this new instance. To understand how this works,
 /// refer to the <see cref="Calculator"/> property.
 /// </param>
 private PagingInfo(PagingInfoCalculator calculator)
 {
     this.CurrentPage = calculator.CurrentPage;
     this.TotalItems  = calculator.TotalItems;
     this.calculator  = calculator;
     this.calculateAllPagesAndItemNumbers = calculator.IncludeAllPagesAndItemNumbers;
 }
Beispiel #3
0
 public IReadOnlyList <PageNumberAndItemNumbers> AllPagesAndItemNumbers()
 {
     // The value may have already been initialized...
     return(this.AllPages ?? (this.CurrentPage.HasValue
                         ? PagingInfoCalculator.AllPagesAndItemNumbers(this)
                         : new PageNumberAndItemNumbers[0]));
 }
Beispiel #4
0
        /// <summary>
        /// Calculates the full set of page numbers and item
        /// numbers from given <paramref name="pageSize"/>
        /// and <paramref name="totalItems"/> values.
        /// </summary>
        /// <param name="pageSize">
        /// The <see cref="PageNumberAndSize.Size"/>
        /// of each page in a "paged" collection.
        /// </param>
        /// <param name="totalItems">
        /// The total number of items in a "paged" collection.
        /// </param>
        /// <returns>
        /// The full set of page numbers and item numbers.
        /// </returns>
        /// <remarks>
        /// Relays to the internal <see cref="PagingInfoCalculator.AllPagesAndItemNumbers(byte,int)"/>
        /// method of <see cref="PagingInfoCalculator"/>.
        /// </remarks>
        public static IReadOnlyList <PageNumberAndItemNumbers> Calculate(
            byte pageSize, int totalItems)
        {
            // Zero as page size is acceptable,
            // indicating a single "unbounded" page.
            Contract.Requires <ArgumentOutOfRangeException>(
                totalItems >= 0, "The number of items in the list must not be negative!");

            return(PagingInfoCalculator.AllPagesAndItemNumbers(pageSize, totalItems));
        }
Beispiel #5
0
            public void CalculatesCorrectTotalPages()
            {
                Assert.Throws <ArgumentOutOfRangeException>(
                    () => PagingInfoCalculator.CalculateTotalPages(10, 0));

                Assert.Throws <ArgumentOutOfRangeException>(
                    () => PagingInfoCalculator.CalculateTotalPages(0, 1138));

                Assert.AreEqual(1, PagingInfoCalculator.CalculateTotalPages(10, 1));
                Assert.AreEqual(1, PagingInfoCalculator.CalculateTotalPages(10, 10));
                Assert.AreEqual(2, PagingInfoCalculator.CalculateTotalPages(10, 11));
                Assert.AreEqual(2, PagingInfoCalculator.CalculateTotalPages(10, 20));
            }