-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodecCapabilities.cs
More file actions
60 lines (37 loc) · 1.76 KB
/
CodecCapabilities.cs
File metadata and controls
60 lines (37 loc) · 1.76 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Gsemac.IO {
public sealed class CodecCapabilities :
ICodecCapabilities {
// Public members
public IFileFormat Format { get; }
public bool CanRead { get; }
public bool CanWrite { get; }
public CodecCapabilities(IFileFormat format, bool canRead, bool canWrite) {
if (format is null)
throw new ArgumentNullException(nameof(format));
Format = format;
CanRead = canRead;
CanWrite = canWrite;
}
public int CompareTo(object obj) {
if (obj is null)
throw new ArgumentNullException(nameof(obj));
if (obj is ICodecCapabilities codecCapabilities)
return CompareTo(codecCapabilities);
throw new ArgumentException(string.Format(Core.Properties.ExceptionMessages.ObjectIsNotAnInstanceOfWithType, nameof(ICodecCapabilities)), nameof(obj));
}
public int CompareTo(ICodecCapabilities other) {
return Format.CompareTo(other.Format);
}
public static IEnumerable<ICodecCapabilities> Flatten(IEnumerable<ICodecCapabilities> formatCapabilities) {
if (formatCapabilities is null)
throw new ArgumentNullException(nameof(formatCapabilities));
// We want to group formats together so that CanRead and CanWrite are true if there is at least one instance of the format for which they are true.
return formatCapabilities.GroupBy(f => f.Format)
.Select(group => new CodecCapabilities(group.Key, group.Any(f => f.CanRead), group.Any(f => f.CanWrite)))
.Where(f => f.CanRead || f.CanWrite);
}
}
}