-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathIniPropertyCollection.cs
186 lines (176 loc) · 6.31 KB
/
IniPropertyCollection.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using BytecodeApi.Extensions;
using System.Collections;
namespace BytecodeApi.IniParser;
/// <summary>
/// Represents a collection of INI file properties.
/// </summary>
public sealed class IniPropertyCollection : ICollection<IniProperty>
{
private readonly List<IniProperty> Properties;
/// <summary>
/// Gets the <see cref="IniProperty" /> at the specified index.
/// </summary>
/// <param name="index">The index at which to retrieve the <see cref="IniProperty" />.</param>
public IniProperty this[int index]
{
get
{
Check.IndexOutOfRange(index, Count);
return Properties[index];
}
}
/// <summary>
/// Gets the <see cref="IniProperty" /> with the specified case sensitive name.
/// </summary>
/// <param name="name">A <see cref="string" /> specifying the name of the property.</param>
public IniProperty this[string name]
{
get
{
Check.ArgumentNull(name);
return Properties.FirstOrDefault(p => p.Name == name) ?? throw Throw.KeyNotFound($"A property with the name '{name}' was not found.");
}
}
/// <summary>
/// Gets the number of properties in this <see cref="IniPropertyCollection" />.
/// </summary>
public int Count => Properties.Count;
/// <summary>
/// Gets a value indicating whether the <see cref="IniPropertyCollection" /> is read-only.
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Initializes a new instance of the <see cref="IniPropertyCollection" /> class.
/// </summary>
public IniPropertyCollection()
{
Properties = new();
}
/// <summary>
/// Processes all duplicate properties within this collection according to the specified behavior.
/// </summary>
/// <param name="behavior">An <see cref="IniDuplicatePropertyNameBehavior" /> object specifying how duplicates are processed.</param>
public void ProcessDuplicates(IniDuplicatePropertyNameBehavior behavior)
{
ProcessDuplicates(behavior, false);
}
/// <summary>
/// Processes all duplicate properties within this collection according to the specified behavior.
/// </summary>
/// <param name="behavior">An <see cref="IniDuplicatePropertyNameBehavior" /> object specifying how duplicates are processed.</param>
/// <param name="ignoreCase"><see langword="true" /> to ignore character casing during name comparison.</param>
public void ProcessDuplicates(IniDuplicatePropertyNameBehavior behavior, bool ignoreCase)
{
List<IniProperty> removedProperties = new();
switch (behavior)
{
case IniDuplicatePropertyNameBehavior.Abort:
if (Properties.DistinctBy(property => ignoreCase ? property.Name.ToLower() : property.Name).Count() != Count)
{
throw Throw.Format("Duplicate property found.");
}
break;
case IniDuplicatePropertyNameBehavior.Ignore:
for (int i = 1; i < Count; i++)
{
if (Properties.Take(i).Any(property => property.Name.Equals(Properties[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)))
{
removedProperties.Add(Properties[i]);
}
}
break;
case IniDuplicatePropertyNameBehavior.Overwrite:
for (int i = 1; i < Count; i++)
{
if (Properties.Take(i).FirstOrDefault(property => property.Name.Equals(Properties[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)) is IniProperty firstProperty)
{
firstProperty.Value = Properties[i].Value;
removedProperties.Add(Properties[i]);
}
}
break;
case IniDuplicatePropertyNameBehavior.Duplicate:
break;
default:
throw Throw.InvalidEnumArgument(nameof(behavior), behavior);
}
Properties.RemoveRange(removedProperties);
}
/// <summary>
/// Adds an <see cref="IniProperty" /> to the end of the <see cref="IniPropertyCollection" />.
/// </summary>
/// <param name="item">The <see cref="IniProperty" /> to be added to the end of the <see cref="IniPropertyCollection" />.</param>
public void Add(IniProperty item)
{
Check.ArgumentNull(item);
Properties.Add(item);
}
/// <summary>
/// Adds an <see cref="IniProperty" /> to the end of the <see cref="IniPropertyCollection" />.
/// </summary>
/// <param name="name">The name of the INI property.</param>
/// <param name="value">The value of the INI property.</param>
/// <returns>
/// The newly added <see cref="IniProperty" />.
/// </returns>
public IniProperty Add(string name, string value)
{
Check.ArgumentNull(name);
Check.ArgumentNull(value);
IniProperty property = new(name, value);
Add(property);
return property;
}
/// <summary>
/// Removes the first occurrence of a specific <see cref="IniProperty" /> from the <see cref="IniPropertyCollection" />.
/// </summary>
/// <param name="item">The <see cref="IniProperty" /> to remove from the <see cref="IniPropertyCollection" />.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="item" /> is successfully removed;
/// otherwise, <see langword="false" />.
/// This method also returns <see langword="false" />, if <paramref name="item" /> was not found in the <see cref="IniPropertyCollection" />.
/// </returns>
public bool Remove(IniProperty item)
{
return Properties.Remove(item);
}
/// <summary>
/// Removes all elements from the <see cref="IniPropertyCollection" />.
/// </summary>
public void Clear()
{
Properties.Clear();
}
/// <summary>
/// Determines whether an element is in the <see cref="IniPropertyCollection" />.
/// </summary>
/// <param name="item">The <see cref="IniProperty" /> to locate in the <see cref="IniPropertyCollection" />.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="item" /> is found in the <see cref="IniPropertyCollection" />;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Contains(IniProperty item)
{
return Properties.Contains(item);
}
void ICollection<IniProperty>.CopyTo(IniProperty[] array, int arrayIndex)
{
Check.ArgumentNull(array);
Check.IndexOutOfRange(arrayIndex, array.Length - Count + 1);
Properties.CopyTo(array, arrayIndex);
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="IniPropertyCollection" />.
/// </summary>
/// <returns>
/// An enumerator that can be used to iterate through the <see cref="IniPropertyCollection" />.
/// </returns>
public IEnumerator<IniProperty> GetEnumerator()
{
return Properties.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Properties.GetEnumerator();
}
}