-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMediaContainerFlags.cs
169 lines (144 loc) · 4.37 KB
/
MediaContainerFlags.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
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
167
168
169
/*
* 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;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
namespace Arcadeia
{
public class MediaContainerFlags
{
#region Constants
public enum Flag
{
None = 0,
Deleted = 1 << 0,
Favorite = 1 << 1
}
#endregion // Constants
private readonly HashSet<Flag> _flags = new();
#region Fields
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MediaContainerFlags"/> instance is
/// flagged as favorite.
/// </summary>
/// <value>
/// <c>true</c> if flagged as favorite; otherwise, <c>false</c>.
/// </value>
public bool Favorite
{
get
{
return GetFlagValue(Flag.Favorite);
}
set
{
SetFlagValue(Flag.Favorite, value);
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MediaContainerFlags"/> instance is
/// flagged as deleted.
/// </summary>
/// <value>
/// <c>true</c> if flagged as deleted; otherwise, <c>false</c>.
/// </value>
public bool Deleted
{
get
{
return GetFlagValue(Flag.Deleted);
}
set
{
SetFlagValue(Flag.Deleted, value);
}
}
/// <summary>
/// Returns a list of all the flags set on the <see cref="MediaContainer"/>.
/// </summary>
public HashSet<Flag> All
{
get => _flags;
}
#endregion // Fields
#region Operators
/// <summary>
/// Returns all the flags set as a string array.
/// </summary>
/// <returns>A string array listing all the set flags.</returns>
public string[] ToArray()
{
return All.Select(flag => Enum.GetName(typeof(MediaContainerFlags.Flag), flag)).OfType<string>().ToArray();
}
#endregion // Operators
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="MediaContainerFlags"/> class.
/// </summary>
public MediaContainerFlags()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MediaContainerFlags"/> class by parsing the.
/// </summary>
/// <param name="self">The node element representing the current MediaContainer.</param>
/// <exception cref="NullReferenceException">The MediaContainerFlags cannot be initialized using
/// a non-existing element!</exception>
public MediaContainerFlags(IEnumerable<string>? flags)
{
if (flags == null) return;
foreach (var flag in flags)
{
_flags.Add(Enum.Parse<Flag>(flag, true));
}
}
#endregion // Constructors
#region Common Functionality
/// <summary>
/// Returns the value of the specified flag.
/// </summary>
/// <param name="flag">The flag.</param>
/// <returns>
/// <c>true</c> if flag is set; otherwise, <c>false</c>.
/// </returns>
private bool GetFlagValue(Flag flag)
{
return _flags.Contains(flag);
}
/// <summary>
/// Sets the value of the supplied flag value.
/// </summary>
/// <param name="flag">The flag to set/reset.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
private void SetFlagValue(Flag flag, bool value)
{
if (value)
{
_flags.Add(flag);
}
else
{
_flags.Remove(flag);
}
}
#endregion // Common Functionality
}
}