Repository navigation
Expand file tree
/
Copy pathVirtualResponse.cs
More file actions
116 lines (100 loc) · 3.71 KB
/
Copy pathVirtualResponse.cs
File metadata and controls
116 lines (100 loc) · 3.71 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
// 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.
// ReSharper disable once CheckNamespace
namespace DynamicData;
/// <summary>
/// Defines values used to virtualise the result set.
/// </summary>
internal sealed class VirtualResponse(int size, int startIndex, int totalSize) : IEquatable<IVirtualResponse>, IVirtualResponse
{
public static IEqualityComparer<IVirtualResponse?> DefaultComparer { get; } = new TotalSizeStartIndexSizeEqualityComparer();
/// <summary>
/// Gets the requested size of the virtualised data.
/// </summary>
public int Size { get; } = size;
/// <summary>
/// Gets the starting index.
/// </summary>
public int StartIndex { get; } = startIndex;
/// <summary>
/// Gets the total size of the underlying cache.
/// </summary>
public int TotalSize { get; } = totalSize;
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(IVirtualResponse? other) => DefaultComparer.Equals(this, other);
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to the current <see cref="object" />.
/// </summary>
/// <returns>
/// true if the specified object is equal to the current object; otherwise, false.
/// </returns>
/// <param name="obj">The object to compare with the current object. </param>
public override bool Equals(object? obj) => obj is IVirtualResponse item && Equals(item);
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="object" />.
/// </returns>
public override int GetHashCode()
{
unchecked
{
var hashCode = Size;
hashCode = (hashCode * 397) ^ StartIndex;
hashCode = (hashCode * 397) ^ TotalSize;
return hashCode;
}
}
/// <summary>
/// Returns a <see cref="string"/> that represents the current <see cref="object"/>.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents the current <see cref="object"/>.
/// </returns>
public override string ToString() => $"Size: {Size}, StartIndex: {StartIndex}, TotalSize: {TotalSize}";
private sealed class TotalSizeStartIndexSizeEqualityComparer : IEqualityComparer<IVirtualResponse?>
{
public bool Equals(IVirtualResponse? x, IVirtualResponse? 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.TotalSize == y.TotalSize && x.StartIndex == y.StartIndex && x.Size == y.Size;
}
public int GetHashCode(IVirtualResponse? obj)
{
if (obj is null)
{
return 0;
}
unchecked
{
var hashCode = obj.TotalSize;
hashCode = (hashCode * 397) ^ obj.StartIndex;
hashCode = (hashCode * 397) ^ obj.Size;
return hashCode;
}
}
}
}