Skip to content

Commit 47c1340

Browse files
committed
Cleanup code.
Added null check. Remove interface members which already defined in parent interface.
1 parent 4fec717 commit 47c1340

8 files changed

+331
-179
lines changed

src/X.PagedList/BasePagedList.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using JetBrains.Annotations;
45

56
namespace X.PagedList
67
{
78
/// <summary>
8-
/// 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.
9+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
10+
/// metadata about the superset collection of objects this subset was created from.
911
/// </summary>
1012
/// <remarks>
11-
/// 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.
13+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
14+
/// metadata about the superset collection of objects this subset was created from.
1215
/// </remarks>
1316
/// <typeparam name = "T">The type of object the collection should contain.</typeparam>
1417
/// <seealso cref = "IPagedList{T}" />
1518
/// <seealso cref = "List{T}" />
19+
[PublicAPI]
1620
public abstract class BasePagedList<T> : PagedListMetaData, IPagedList<T>
1721
{
1822
protected readonly List<T> Subset = new List<T>();
@@ -25,7 +29,8 @@ protected internal BasePagedList()
2529
}
2630

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

59-
bool pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount;
64+
var pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount;
6065

6166
HasPreviousPage = pageNumberIsGood && PageNumber > 1;
6267
HasNextPage = pageNumberIsGood && PageNumber < PageCount;

src/X.PagedList/IPagedList.cs

+108-101
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,128 @@
11
using System.Collections.Generic;
2+
using JetBrains.Annotations;
23

34
namespace X.PagedList
45
{
5-
/// <summary>
6-
/// 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.
7-
/// </summary>
8-
/// <remarks>
9-
/// 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.
10-
/// </remarks>
11-
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
12-
/// <seealso cref="IEnumerable{T}"/>
13-
public interface IPagedList<out T> : IPagedList, IReadOnlyList<T>, IEnumerable<T>
14-
{
15-
///<summary>
16-
/// Gets the element at the specified index.
17-
///</summary>
18-
///<param name="index">The zero-based index of the element to get.</param>
19-
T this[int index] { get; }
20-
21-
///<summary>
22-
/// Gets the number of elements contained on this page.
23-
///</summary>
24-
int Count { get; }
25-
6+
/// <summary>
7+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
8+
/// metadata about the superset collection of objects this subset was created from.
9+
/// </summary>
10+
/// <remarks>
11+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
12+
/// metadata about the superset collection of objects this subset was created from.
13+
/// </remarks>
14+
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
15+
/// <seealso cref="IEnumerable{T}"/>
16+
[PublicAPI]
17+
public interface IPagedList<out T> : IPagedList, IReadOnlyList<T>
18+
{
2619
///<summary>
2720
/// Gets a non-enumerable copy of this paged list.
2821
///</summary>
2922
///<returns>A non-enumerable copy of this paged list.</returns>
3023
PagedListMetaData GetMetaData();
31-
}
24+
}
3225

33-
/// <summary>
34-
/// 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.
35-
/// </summary>
36-
/// <remarks>
37-
/// 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.
38-
/// </remarks>
39-
public interface IPagedList
40-
{
41-
/// <summary>
42-
/// Total number of subsets within the superset.
43-
/// </summary>
44-
/// <value>
45-
/// Total number of subsets within the superset.
46-
/// </value>
47-
int PageCount { get; }
26+
/// <summary>
27+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
28+
/// metadata about the superset collection of objects this subset was created from.
29+
/// </summary>
30+
/// <remarks>
31+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
32+
/// metadata about the superset collection of objects this subset was created from.
33+
/// </remarks>
34+
public interface IPagedList
35+
{
36+
/// <summary>
37+
/// Total number of subsets within the superset.
38+
/// </summary>
39+
/// <value>
40+
/// Total number of subsets within the superset.
41+
/// </value>
42+
int PageCount { get; }
4843

49-
/// <summary>
50-
/// Total number of objects contained within the superset.
51-
/// </summary>
52-
/// <value>
53-
/// Total number of objects contained within the superset.
54-
/// </value>
55-
int TotalItemCount { get; }
44+
/// <summary>
45+
/// Total number of objects contained within the superset.
46+
/// </summary>
47+
/// <value>
48+
/// Total number of objects contained within the superset.
49+
/// </value>
50+
int TotalItemCount { get; }
5651

57-
/// <summary>
58-
/// One-based index of this subset within the superset, zero if the superset is empty.
59-
/// </summary>
60-
/// <value>
61-
/// One-based index of this subset within the superset, zero if the superset is empty.
62-
/// </value>
63-
int PageNumber { get; }
52+
/// <summary>
53+
/// One-based index of this subset within the superset, zero if the superset is empty.
54+
/// </summary>
55+
/// <value>
56+
/// One-based index of this subset within the superset, zero if the superset is empty.
57+
/// </value>
58+
int PageNumber { get; }
6459

65-
/// <summary>
66-
/// Maximum size any individual subset.
67-
/// </summary>
68-
/// <value>
69-
/// Maximum size any individual subset.
70-
/// </value>
71-
int PageSize { get; }
60+
/// <summary>
61+
/// Maximum size any individual subset.
62+
/// </summary>
63+
/// <value>
64+
/// Maximum size any individual subset.
65+
/// </value>
66+
int PageSize { get; }
7267

73-
/// <summary>
74-
/// 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.
75-
/// </summary>
76-
/// <value>
77-
/// 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.
78-
/// </value>
79-
bool HasPreviousPage { get; }
68+
/// <summary>
69+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
70+
/// is NOT the first subset within the superset.
71+
/// </summary>
72+
/// <value>
73+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
74+
/// is NOT the first subset within the superset.
75+
/// </value>
76+
bool HasPreviousPage { get; }
8077

81-
/// <summary>
82-
/// 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.
83-
/// </summary>
84-
/// <value>
85-
/// 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.
86-
/// </value>
87-
bool HasNextPage { get; }
78+
/// <summary>
79+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
80+
/// is NOT the last subset within the superset.
81+
/// </summary>
82+
/// <value>
83+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
84+
/// is NOT the last subset within the superset.
85+
/// </value>
86+
bool HasNextPage { get; }
8887

89-
/// <summary>
90-
/// 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.
91-
/// </summary>
92-
/// <value>
93-
/// 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.
94-
/// </value>
95-
bool IsFirstPage { get; }
88+
/// <summary>
89+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
90+
/// is the first subset within the superset.
91+
/// </summary>
92+
/// <value>
93+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
94+
/// is the first subset within the superset.
95+
/// </value>
96+
bool IsFirstPage { get; }
9697

97-
/// <summary>
98-
/// 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.
99-
/// </summary>
100-
/// <value>
101-
/// 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.
102-
/// </value>
103-
bool IsLastPage { get; }
98+
/// <summary>
99+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
100+
/// is the last subset within the superset.
101+
/// </summary>
102+
/// <value>
103+
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this
104+
/// is the last subset within the superset.
105+
/// </value>
106+
bool IsLastPage { get; }
104107

105-
/// <summary>
106-
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
107-
/// </summary>
108-
/// <value>
109-
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
110-
/// </value>
111-
int FirstItemOnPage { get; }
108+
/// <summary>
109+
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber
110+
/// is greater than PageCount.
111+
/// </summary>
112+
/// <value>
113+
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber
114+
/// is greater than PageCount.
115+
/// </value>
116+
int FirstItemOnPage { get; }
112117

113-
/// <summary>
114-
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
115-
/// </summary>
116-
/// <value>
117-
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber is greater than PageCount.
118-
/// </value>
119-
int LastItemOnPage { get; }
120-
}
118+
/// <summary>
119+
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber
120+
/// is greater than PageCount.
121+
/// </summary>
122+
/// <value>
123+
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber
124+
/// is greater than PageCount.
125+
/// </value>
126+
int LastItemOnPage { get; }
127+
}
121128
}

src/X.PagedList/PagedList.cs

+34-11
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Linq.Expressions;
5+
using JetBrains.Annotations;
56

67
namespace X.PagedList
78
{
9+
[PublicAPI]
810
public class PagedList<T, TKey> : BasePagedList<T>
911
{
1012
/// <summary>
11-
/// 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.
13+
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset into
14+
/// subsets the size of the supplied pageSize. The instance then only contains the objects contained in the
15+
/// subset specified by index.
1216
/// </summary>
13-
/// <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>
17+
/// <param name="superset">
18+
/// The collection of objects to be divided into subsets. If the collection
19+
/// implements <see cref="IQueryable{T}"/>, it will be treated as such.
20+
/// </param>
1421
/// <param name="keySelector">Expression for Order</param>
15-
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
22+
/// <param name="pageNumber">
23+
/// The one-based index of the subset of objects to be contained by this instance.
24+
/// </param>
1625
/// <param name="pageSize">The maximum size of any individual subset.</param>
1726
/// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
1827
/// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
@@ -48,10 +57,12 @@ private void InitSubset(IEnumerable<T> superset, Func<T, TKey> keySelectorMethod
4857
}
4958

5059
/// <summary>
51-
/// 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.
60+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
61+
/// metadata about the superset collection of objects this subset was created from.
5262
/// </summary>
5363
/// <remarks>
54-
/// 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.
64+
/// Represents a subset of a collection of objects that can be individually accessed by index and containing
65+
/// metadata about the superset collection of objects this subset was created from.
5566
/// </remarks>
5667
/// <typeparam name="T">The type of object the collection should contain.</typeparam>
5768
/// <seealso cref="IPagedList{T}"/>
@@ -61,17 +72,24 @@ private void InitSubset(IEnumerable<T> superset, Func<T, TKey> keySelectorMethod
6172
public class PagedList<T> : BasePagedList<T>
6273
{
6374
/// <summary>
64-
/// 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.
75+
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset
76+
/// into subsets the size of the supplied pageSize. The instance then only contains the objects contained
77+
/// in the subset specified by index.
6578
/// </summary>
66-
/// <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>
67-
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
79+
/// <param name="superset">
80+
/// The collection of objects to be divided into subsets. If the collection
81+
/// implements <see cref="IQueryable{T}"/>, it will be treated as such.
82+
/// </param>
83+
/// <param name="pageNumber">
84+
/// The one-based index of the subset of objects to be contained by this instance.
85+
/// </param>
6886
/// <param name="pageSize">The maximum size of any individual subset.</param>
6987
/// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>
7088
/// <exception cref="ArgumentOutOfRangeException">The specified page size cannot be less than one.</exception>
7189
public PagedList(IQueryable<T> superset, int pageNumber, int pageSize)
7290
: base(pageNumber, pageSize, superset?.Count() ?? 0)
7391
{
74-
if (TotalItemCount > 0)
92+
if (TotalItemCount > 0 && superset != null)
7593
{
7694
Subset.AddRange(pageNumber == 1
7795
? superset.Take(pageSize).ToList()
@@ -81,9 +99,14 @@ public PagedList(IQueryable<T> superset, int pageNumber, int pageSize)
8199
}
82100

83101
/// <summary>
84-
/// 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.
102+
/// Initializes a new instance of the <see cref="PagedList{T}"/> class that divides the supplied superset
103+
/// into subsets the size of the supplied pageSize. The instance then only contains the objects contained in
104+
/// the subset specified by index.
85105
/// </summary>
86-
/// <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>
106+
/// <param name="superset">
107+
/// The collection of objects to be divided into subsets. If the collection
108+
/// implements <see cref="IQueryable{T}"/>, it will be treated as such.
109+
/// </param>
87110
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
88111
/// <param name="pageSize">The maximum size of any individual subset.</param>
89112
/// <exception cref="ArgumentOutOfRangeException">The specified index cannot be less than zero.</exception>

0 commit comments

Comments
 (0)