Repository navigation
Expand file tree
/
Copy pathPageRequest.cs
More file actions
132 lines (109 loc) · 3.32 KB
/
Copy pathPageRequest.cs
File metadata and controls
132 lines (109 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Diagnostics.CodeAnalysis;
// ReSharper disable once CheckNamespace
namespace DynamicData;
/// <summary>
/// Represents a new page request.
/// </summary>
public sealed class PageRequest : IPageRequest, IEquatable<IPageRequest>
{
/// <summary>
/// The default page request.
/// </summary>
public static readonly IPageRequest Default = new PageRequest();
/// <summary>
/// Represents an empty page.
/// </summary>
public static readonly IPageRequest Empty = new PageRequest(0, 0);
private static readonly IEqualityComparer<IPageRequest?> _pageSizeComparerInstance = new PageSizeEqualityComparer();
/// <summary>
/// Initializes a new instance of the <see cref="PageRequest"/> class.
/// </summary>
/// <param name="page">The page.</param>
/// <param name="size">The size.</param>
public PageRequest(int page, int size)
{
if (page < 0)
{
throw new ArgumentException("Page must be positive");
}
if (size < 0)
{
throw new ArgumentException("Size must be positive");
}
Page = page;
Size = size;
}
/// <summary>
/// Initializes a new instance of the <see cref="PageRequest"/> class.
/// </summary>
public PageRequest()
{
}
/// <summary>
/// Gets the default comparer.
/// </summary>
/// <value>
/// The default comparer.
/// </value>
[SuppressMessage("Design", "CA1822: Member can be static", Justification = "Backwards compatibilty")]
public IEqualityComparer<IPageRequest?> DefaultComparer => _pageSizeComparerInstance;
/// <summary>
/// Gets the page to move to.
/// </summary>
public int Page { get; } = 1;
/// <summary>
/// Gets the page size.
/// </summary>
public int Size { get; } = 25;
/// <inheritdoc />
public bool Equals(IPageRequest? other) => DefaultComparer.Equals(this, other);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is IPageRequest value && Equals(value);
/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
return (Size * 397) ^ Page;
}
}
/// <inheritdoc />
public override string ToString() => $"Page: {Page}, Size: {Size}";
private sealed class PageSizeEqualityComparer : IEqualityComparer<IPageRequest?>
{
public bool Equals(IPageRequest? x, IPageRequest? y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (x is null)
{
return false;
}
if (y is null)
{
return false;
}
if (x.GetType() != y.GetType())
{
return false;
}
return x.Page == y.Page && x.Size == y.Size;
}
public int GetHashCode(IPageRequest? obj)
{
if (obj is null)
{
return 0;
}
unchecked
{
return (obj.Page * 397) ^ obj.Size;
}
}
}
}