-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparableObservable.cs
110 lines (90 loc) · 6.88 KB
/
ComparableObservable.cs
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
#region Copyright (C) 2017-2022 Cody Bock
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
//
// More information can be found here: https://www.gnu.org/licenses/gpl-3.0.en.html
#endregion
#region Using Directives
using System;
#endregion
namespace MVVMUtils;
/// <summary> Represents a singular observed property with <see cref="IComparable{T}"/> support. </summary>
/// <typeparam name="T">The observed property type.</typeparam>
public class ComparableObservable<T> : Observable<T>, IComparable<T>, IComparable<ComparableObservable<T>>, IComparable where T : IComparable<T> {
/// <inheritdoc />
public int CompareTo( ComparableObservable<T>? Other ) => Other is null ? 1 : CompareTo(Other.Value);
/// <inheritdoc />
public int CompareTo( T? Other ) => Value is null ? Other is null ? 0 : 1 : Value.CompareTo(Other);
/// <inheritdoc />
public int CompareTo( object? Obj ) => Obj switch {
null => 1,
_ => ReferenceEquals(this, Obj)
? 0
: Obj switch {
ComparableObservable<T> Obs => CompareTo(Obs),
T Val => CompareTo(Val),
_ => throw new ArgumentException($"Object must be of type {nameof(ComparableObservable<T>)}")
}
};
/// <summary> Lesser than comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is lesser than <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator <( ComparableObservable<T>? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null : Left.CompareTo(Right) < 0;
/// <summary> Greater than comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is greater than <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator >( ComparableObservable<T>? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null : Left.CompareTo(Right) > 0;
/// <summary> Lesser than or equal to comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is lesser than or equal to <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator <=( ComparableObservable<T>? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null : Left.CompareTo(Right) <= 0;
/// <summary> Greater than or equal to comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is greater than or equal to <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator >=( ComparableObservable<T>? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null : Left.CompareTo(Right) >= 0;
/// <summary> Lesser than comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is lesser than <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator <( T? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null || Right.Value is null : Left.CompareTo(Right!.Value) < 0;
/// <summary> Greater than comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is greater than <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator >( T? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null || Right.Value is null : Left.CompareTo(Right!.Value) > 0;
/// <summary> Lesser than or equal to comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is lesser than or equal to <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator <=( T? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null || Right.Value is null : Left.CompareTo(Right!.Value) <= 0;
/// <summary> Greater than or equal to comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is greater than or equal to <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator >=( T? Left, ComparableObservable<T>? Right ) => Left is null ? Right is null || Right.Value is null : Left.CompareTo(Right!.Value) >= 0;
/// <summary> Lesser than comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is lesser than <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator <( ComparableObservable<T>? Left, T? Right ) => Left is null || Left.Value is null ? Right is null : Left.CompareTo(Right) < 0;
/// <summary> Greater than comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is greater than <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator >( ComparableObservable<T>? Left, T? Right ) => Left is null || Left.Value is null ? Right is null : Left.CompareTo(Right) > 0;
/// <summary> Lesser than or equal to comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is lesser than or equal to <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator <=( ComparableObservable<T>? Left, T? Right ) => Left is null || Left.Value is null ? Right is null : Left.CompareTo(Right) <= 0;
/// <summary> Greater than or equal to comparative. </summary>
/// <param name="Left">The left value.</param>
/// <param name="Right">The right value.</param>
/// <returns><see langword="true"/> if <paramref name="Left"/> is greater than or equal to <paramref name="Right"/>; otherwise <see langword="false"/>.</returns>
public static bool operator >=( ComparableObservable<T>? Left, T? Right ) => Left is null || Left.Value is null ? Right is null : Left.CompareTo(Right) >= 0;
}