-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileFormatBase.cs
More file actions
135 lines (82 loc) · 4.13 KB
/
FileFormatBase.cs
File metadata and controls
135 lines (82 loc) · 4.13 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Gsemac.IO {
public abstract class FileFormatBase :
IFileFormat {
// Public members
public abstract IEnumerable<string> Extensions { get; }
public virtual IEnumerable<IFileSignature> Signatures => Enumerable.Empty<IFileSignature>();
public abstract IEnumerable<IMimeType> MimeTypes { get; }
public abstract string Name { get; }
public int CompareTo(object other) {
if (other is null)
throw new ArgumentNullException(nameof(other));
if (other is IFileFormat fileFormat)
return CompareTo(fileFormat);
throw new ArgumentException(string.Format(Core.Properties.ExceptionMessages.ObjectIsNotAnInstanceOfWithType, nameof(IFileFormat)), nameof(other));
}
public virtual int CompareTo(IFileFormat other) {
if (other is null)
throw new ArgumentNullException(nameof(other));
if (Equals(other))
return 0;
string lhsMimeType = GetMimeTypeString(this);
string rhsMimeType = GetMimeTypeString(other);
return lhsMimeType.CompareTo(rhsMimeType);
}
public override bool Equals(object other) {
if (ReferenceEquals(this, other))
return true;
if (other is null)
return false;
if (other is IFileFormat fileFormat)
return Equals(fileFormat);
throw new ArgumentException(string.Format(Core.Properties.ExceptionMessages.ObjectIsNotAnInstanceOfWithType, nameof(IFileFormat)), nameof(other));
}
public virtual bool Equals(IFileFormat other) {
if (other is null)
return false;
// Start by comparing the MIME types, as long as they're not generic (application/octet-stream).
string lhsMimeType = GetMimeTypeString(this);
string rhsMimeType = GetMimeTypeString(other);
if (!string.IsNullOrWhiteSpace(lhsMimeType) && !lhsMimeType.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase))
return lhsMimeType.Equals(rhsMimeType, StringComparison.OrdinalIgnoreCase);
// If we have a generic MIME type, check for matching file extensions.
return Extensions.Any(ext => other.Extensions.Any(otherExt => otherExt.Equals(ext, StringComparison.OrdinalIgnoreCase)));
}
public override int GetHashCode() {
string fileTypeStr = GetMimeTypeString(this)?.ToLowerInvariant();
if (string.IsNullOrWhiteSpace(fileTypeStr))
fileTypeStr = Extensions?.FirstOrDefault()?.ToLowerInvariant();
return (fileTypeStr ?? string.Empty).GetHashCode();
}
public override string ToString() {
return MimeTypes?.FirstOrDefault()?.ToString();
}
public static bool operator ==(FileFormatBase left, FileFormatBase right) {
if (left is null)
return right is null;
return left.Equals(right);
}
public static bool operator !=(FileFormatBase left, FileFormatBase right) {
return !(left == right);
}
public static bool operator <(FileFormatBase left, FileFormatBase right) {
return left is null ? right is object : left.CompareTo(right) < 0;
}
public static bool operator <=(FileFormatBase left, FileFormatBase right) {
return left is null || left.CompareTo(right) <= 0;
}
public static bool operator >(FileFormatBase left, FileFormatBase right) {
return left is object && left.CompareTo(right) > 0;
}
public static bool operator >=(FileFormatBase left, FileFormatBase right) {
return left is null ? right is null : left.CompareTo(right) >= 0;
}
// Private members
private static string GetMimeTypeString(IFileFormat fileFormat) {
return (fileFormat?.MimeTypes?.FirstOrDefault()?.ToString() ?? string.Empty).ToLowerInvariant();
}
}
}