forked from opennetty/opennetty-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenNettyCategory.cs
More file actions
218 lines (184 loc) · 6.39 KB
/
Copy pathOpenNettyCategory.cs
File metadata and controls
218 lines (184 loc) · 6.39 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/opennetty/opennetty-core for more information concerning
* the license and the contributors participating to this project.
*/
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text;
using static OpenNetty.OpenNettyConstants;
namespace OpenNetty;
/// <summary>
/// Represents the category of an OpenNetty message.
/// </summary>
[DebuggerDisplay("{ToString(),nq}")]
public readonly struct OpenNettyCategory : IEquatable<OpenNettyCategory>
{
/// <summary>
/// Creates a new instance of the <see cref="OpenNettyCategory"/> structure.
/// </summary>
/// <param name="value">The value.</param>
public OpenNettyCategory(string value)
: this(value, [])
{
}
/// <summary>
/// Creates a new instance of the <see cref="OpenNettyCategory"/> structure.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="parameters">The additional parameters, if applicable.</param>
public OpenNettyCategory(string value, ImmutableArray<string> parameters)
{
ArgumentNullException.ThrowIfNull(value);
// Ensure the value only includes ASCII digits.
foreach (var character in value)
{
if (!char.IsAsciiDigit(character))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0004), nameof(value));
}
}
// Ensure the parameters only include ASCII digits.
if (!Parameters.IsDefaultOrEmpty)
{
for (var index = 0; index < parameters.Length; index++)
{
foreach (var character in parameters[index])
{
if (!char.IsAsciiDigit(character))
{
throw new ArgumentException(SR.GetResourceString(SR.ID0004), nameof(value));
}
}
}
}
Value = value;
Parameters = parameters;
}
/// <summary>
/// Gets the value associated with the category.
/// </summary>
public string Value { get; }
/// <summary>
/// Gets the additional parameters associated with the category, if applicable.
/// </summary>
public ImmutableArray<string> Parameters { get; } = [];
/// <inheritdoc/>
public bool Equals(OpenNettyCategory other)
{
if (Value is null)
{
return other.Value is null;
}
if (!string.Equals(Value, other.Value, StringComparison.Ordinal))
{
return false;
}
if (!Parameters.IsDefaultOrEmpty && !other.Parameters.IsDefaultOrEmpty)
{
if (Parameters.Length != other.Parameters.Length)
{
return false;
}
for (var index = 0; index < Parameters.Length; index++)
{
if (!string.Equals(Parameters[index], other.Parameters[index], StringComparison.Ordinal))
{
return false;
}
}
}
else if (Parameters.IsDefaultOrEmpty && !other.Parameters.IsDefaultOrEmpty)
{
return false;
}
else if (!Parameters.IsDefaultOrEmpty && other.Parameters.IsDefaultOrEmpty)
{
return false;
}
return true;
}
/// <inheritdoc/>
public override bool Equals(object? obj) => obj is OpenNettyAddress address && Equals(address);
/// <inheritdoc/>
public override int GetHashCode()
{
if (Value is null)
{
return 0;
}
var hash = new HashCode();
hash.Add(Value);
if (!Parameters.IsDefaultOrEmpty)
{
hash.Add(Parameters.Length);
for (var index = 0; index < Parameters.Length; index++)
{
hash.Add(Parameters[index]);
}
}
else
{
hash.Add(0);
}
return hash.ToHashCode();
}
/// <summary>
/// Computes the <see cref="string"/> representation of the current category.
/// </summary>
/// <returns>The <see cref="string"/> representation of the current category.</returns>
public override string ToString()
{
if (Value is null)
{
return string.Empty;
}
if (Parameters.IsDefaultOrEmpty)
{
return Value;
}
var builder = new StringBuilder();
builder.Append(Value);
for (var index = 0; index < Parameters.Length; index++)
{
builder.Append((char) Separators.Hash[0]);
builder.Append(Parameters[index]);
}
return builder.ToString();
}
/// <summary>
/// Converts the category to a list of <see cref="OpenNettyParameter"/>.
/// </summary>
/// <returns>The list of <see cref="OpenNettyParameter"/> representing this category.</returns>
public ImmutableArray<OpenNettyParameter> ToParameters()
{
if (Value is null)
{
return [];
}
var builder = ImmutableArray.CreateBuilder<OpenNettyParameter>();
builder.Add(new OpenNettyParameter(Value));
if (!Parameters.IsDefaultOrEmpty)
{
for (var index = 0; index < Parameters.Length; index++)
{
builder.Add(new OpenNettyParameter(Parameters[index]));
}
}
return builder.ToImmutable();
}
/// <summary>
/// Determines whether two <see cref="OpenNettyCategory"/> instances are equal.
/// </summary>
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns><see langword="true"/> if the two instances are equal, <see langword="false"/> otherwise.</returns>
public static bool operator ==(OpenNettyCategory left, OpenNettyCategory right) => left.Equals(right);
/// <summary>
/// Determines whether two <see cref="OpenNettyCategory"/> instances are not equal.
/// </summary>
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns><see langword="true"/> if the two instances are not equal, <see langword="false"/> otherwise.</returns>
public static bool operator !=(OpenNettyCategory left, OpenNettyCategory right) => !(left == right);
}