-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResolutionType.cs
More file actions
166 lines (141 loc) · 4.71 KB
/
Copy pathResolutionType.cs
File metadata and controls
166 lines (141 loc) · 4.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright © 2024 Travelonium AB
*
* This file is part of Arcadeia.
*
* Arcadeia is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arcadeia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Arcadeia. If not, see <https://www.gnu.org/licenses/>.
*
*/
using System;
namespace Arcadeia
{
/// <summary>
/// The type representing a photo or video file's resolution. It has a width and a height property
/// and multiple ways of supplying them.
/// </summary>
public class ResolutionType : IComparable, IEquatable<ResolutionType>
{
public long? Width { get; set; }
public long? Height { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ResolutionType"/> class having been supplied
/// the resolution inside a string of "WxH" format.
/// </summary>
/// <param name="resolution">The resolution with width and height separated using 'x'.</param>
public ResolutionType(string resolution)
{
// Initialize the Width and Height.
Width = Height = 0;
try
{
if (resolution.Contains('x'))
{
Width = Convert.ToInt64(resolution.Substring(0, resolution.IndexOf('x')));
Height = Convert.ToInt64(resolution[(resolution.IndexOf('x') + 1)..]);
}
}
catch (Exception)
{
Width = Height = 0;
return;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ResolutionType"/> class having been supplied
/// the width and the height.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public ResolutionType(long? width, long? height)
{
Width = width;
Height = height;
}
/// <summary>
/// Parameterless constructor used for deserialization of JSON values.
/// </summary>
public ResolutionType()
{
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String" /> that represents this instance. The format in which the
/// resolution is returned is "WxH".
/// </returns>
public override string ToString()
{
string resolution = "";
if ((Width > 0) && (Height > 0))
{
resolution = string.Format("{0}x{1}", Width, Height);
}
return resolution;
}
public int CompareTo(object? obj)
{
if (obj == null) return 1;
if (obj is not ResolutionType other)
{
throw new ArgumentException("The supplied object is not a ResolutionType object.");
}
if (((other.Width == Width) && (other.Height == Height)) || ((other.Width + other.Height) == (Width + Height))) return 0;
if ((other.Width + other.Height) > (Width + Height)) return -1;
return 1;
}
public bool Equals(ResolutionType? other)
{
if (other is null)
{
return false;
}
// Optimization for a common success case.
if (Object.ReferenceEquals(this, other))
{
return true;
}
// If run-time types are not exactly the same, return false.
if (this.GetType() != other.GetType())
{
return false;
}
// Return true if the fields match.
return (Width == other.Width) && (Height == other.Height);
}
public static bool operator ==(ResolutionType lhs, ResolutionType rhs)
{
if (lhs is null)
{
if (rhs is null)
{
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(ResolutionType lhs, ResolutionType rhs) => !(lhs == rhs);
public override bool Equals(object? obj)
{
return Equals(obj as ResolutionType);
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
}