Skip to content

Commit

Permalink
Cleanup code.
Browse files Browse the repository at this point in the history
Added null check.
Remove interface members which already defined in parent interface.
  • Loading branch information
a-gubskiy committed May 8, 2021
1 parent 4fec717 commit 47c1340
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 179 deletions.
13 changes: 9 additions & 4 deletions src/X.PagedList/BasePagedList.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace X.PagedList
{
/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
/// <typeparam name = "T">The type of object the collection should contain.</typeparam>
/// <seealso cref = "IPagedList{T}" />
/// <seealso cref = "List{T}" />
[PublicAPI]
public abstract class BasePagedList<T> : PagedListMetaData, IPagedList<T>
{
protected readonly List<T> Subset = new List<T>();
Expand All @@ -25,7 +29,8 @@ protected internal BasePagedList()
}

/// <summary>
/// Initializes a new instance of a type deriving from <see cref = "BasePagedList{T}" /> and sets properties needed to calculate position and size data on the subset and superset.
/// Initializes a new instance of a type deriving from <see cref = "BasePagedList{T}" /> and sets properties
/// needed to calculate position and size data on the subset and superset.
/// </summary>
/// <param name = "pageNumber">The one-based index of the subset of objects contained by this instance.</param>
/// <param name = "pageSize">The maximum size of any individual subset.</param>
Expand Down Expand Up @@ -56,7 +61,7 @@ protected internal BasePagedList(int pageNumber, int pageSize, int totalItemCoun
? (int)Math.Ceiling(TotalItemCount / (double)PageSize)
: 0;

bool pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount;
var pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount;

HasPreviousPage = pageNumberIsGood && PageNumber > 1;
HasNextPage = pageNumberIsGood && PageNumber < PageCount;
Expand Down
209 changes: 108 additions & 101 deletions src/X.PagedList/IPagedList.cs
Original file line number Diff line number Diff line change
@@ -1,121 +1,128 @@
using System.Collections.Generic;
using JetBrains.Annotations;

namespace X.PagedList
{
/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// </remarks>
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
/// <seealso cref="IEnumerable{T}"/>
public interface IPagedList<out T> : IPagedList, IReadOnlyList<T>, IEnumerable<T>
{
///<summary>
/// Gets the element at the specified index.
///</summary>
///<param name="index">The zero-based index of the element to get.</param>
T this[int index] { get; }

///<summary>
/// Gets the number of elements contained on this page.
///</summary>
int Count { get; }

/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
/// <seealso cref="IEnumerable{T}"/>
[PublicAPI]
public interface IPagedList<out T> : IPagedList, IReadOnlyList<T>
{
///<summary>
/// Gets a non-enumerable copy of this paged list.
///</summary>
///<returns>A non-enumerable copy of this paged list.</returns>
PagedListMetaData GetMetaData();
}
}

/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// </remarks>
public interface IPagedList
{
/// <summary>
/// Total number of subsets within the superset.
/// </summary>
/// <value>
/// Total number of subsets within the superset.
/// </value>
int PageCount { get; }
/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
public interface IPagedList
{
/// <summary>
/// Total number of subsets within the superset.
/// </summary>
/// <value>
/// Total number of subsets within the superset.
/// </value>
int PageCount { get; }

/// <summary>
/// Total number of objects contained within the superset.
/// </summary>
/// <value>
/// Total number of objects contained within the superset.
/// </value>
int TotalItemCount { get; }
/// <summary>
/// Total number of objects contained within the superset.
/// </summary>
/// <value>
/// Total number of objects contained within the superset.
/// </value>
int TotalItemCount { get; }

/// <summary>
/// One-based index of this subset within the superset, zero if the superset is empty.
/// </summary>
/// <value>
/// One-based index of this subset within the superset, zero if the superset is empty.
/// </value>
int PageNumber { get; }
/// <summary>
/// One-based index of this subset within the superset, zero if the superset is empty.
/// </summary>
/// <value>
/// One-based index of this subset within the superset, zero if the superset is empty.
/// </value>
int PageNumber { get; }

/// <summary>
/// Maximum size any individual subset.
/// </summary>
/// <value>
/// Maximum size any individual subset.
/// </value>
int PageSize { get; }
/// <summary>
/// Maximum size any individual subset.
/// </summary>
/// <value>
/// Maximum size any individual subset.
/// </value>
int PageSize { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is NOT the first subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is NOT the first subset within the superset.
/// </value>
bool HasPreviousPage { get; }
/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the first subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the first subset within the superset.
/// </value>
bool HasPreviousPage { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is NOT the last subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is NOT the last subset within the superset.
/// </value>
bool HasNextPage { get; }
/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the last subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is NOT the last subset within the superset.
/// </value>
bool HasNextPage { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is the first subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is the first subset within the superset.
/// </value>
bool IsFirstPage { get; }
/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the first subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the first subset within the superset.
/// </value>
bool IsFirstPage { get; }

/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is the last subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this is the last subset within the superset.
/// </value>
bool IsLastPage { get; }
/// <summary>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the last subset within the superset.
/// </summary>
/// <value>
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
/// is the last subset within the superset.
/// </value>
bool IsLastPage { get; }

/// <summary>
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
/// </summary>
/// <value>
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
/// </value>
int FirstItemOnPage { get; }
/// <summary>
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </summary>
/// <value>
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </value>
int FirstItemOnPage { get; }

/// <summary>
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
/// </summary>
/// <value>
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
/// </value>
int LastItemOnPage { get; }
}
/// <summary>
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </summary>
/// <value>
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber
/// is greater than PageCount.
/// </value>
int LastItemOnPage { get; }
}
}
45 changes: 34 additions & 11 deletions src/X.PagedList/PagedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using JetBrains.Annotations;

namespace X.PagedList
{
[PublicAPI]
public class PagedList<T, TKey> : BasePagedList<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into
/// subsets the size of the supplied pageSize. The instance then only contains the objects contained in the
/// subset specified by index.
/// </summary>
/// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
/// <param name="superset">
/// The collection of objects to be divided into subsets. If the collection
/// implements <see cref="IQueryable{T}"/>, it will be treated as such.
/// </param>
/// <param name="keySelector">Expression for Order</param>
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
/// <param name="pageNumber">
/// The one-based index of the subset of objects to be contained by this instance.
/// </param>
/// <param name="pageSize">The maximum size of any individual subset.</param>
/// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
/// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
Expand Down Expand Up @@ -48,10 +57,12 @@ private void InitSubset(IEnumerable<T> superset, Func<T, TKey> keySelectorMethod
}

/// <summary>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </summary>
/// <remarks>
/// Represents a subset of a collection of objects that can be individually accessed by index and containing metadata about the superset collection of objects this subset was created from.
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
/// metadata about the superset collection of objects this subset was created from.
/// </remarks>
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
/// <seealso cref="IPagedList{T}"/>
Expand All @@ -61,17 +72,24 @@ private void InitSubset(IEnumerable<T> superset, Func<T, TKey> keySelectorMethod
public class PagedList<T> : BasePagedList<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset
/// into subsets the size of the supplied pageSize. The instance then only contains the objects contained
/// in the subset specified by index.
/// </summary>
/// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
/// <param name="superset">
/// The collection of objects to be divided into subsets. If the collection
/// implements <see cref="IQueryable{T}"/>, it will be treated as such.
/// </param>
/// <param name="pageNumber">
/// The one-based index of the subset of objects to be contained by this instance.
/// </param>
/// <param name="pageSize">The maximum size of any individual subset.</param>
/// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
/// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
public PagedList(IQueryable<T> superset, int pageNumber, int pageSize)
: base(pageNumber, pageSize, superset?.Count() ?? 0)
{
if (TotalItemCount > 0)
if (TotalItemCount > 0 && superset != null)
{
Subset.AddRange(pageNumber == 1
? superset.Take(pageSize).ToList()
Expand All @@ -81,9 +99,14 @@ public PagedList(IQueryable<T> superset, int pageNumber, int pageSize)
}

/// <summary>
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into subsets the size of the supplied pageSize. The instance then only containes the objects contained in the subset specified by index.
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset
/// into subsets the size of the supplied pageSize. The instance then only contains the objects contained in
/// the subset specified by index.
/// </summary>
/// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
/// <param name="superset">
/// The collection of objects to be divided into subsets. If the collection
/// implements <see cref="IQueryable{T}"/>, it will be treated as such.
/// </param>
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
/// <param name="pageSize">The maximum size of any individual subset.</param>
/// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
Expand Down
Loading

0 comments on commit 47c1340

Please sign in to comment.