forked from opennetty/opennetty-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenNettyCommand.cs
More file actions
239 lines (202 loc) · 7.22 KB
/
Copy pathOpenNettyCommand.cs
File metadata and controls
239 lines (202 loc) · 7.22 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* 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 command associated with an OpenNetty message.
/// </summary>
[DebuggerDisplay("{ToString(),nq}")]
public readonly struct OpenNettyCommand : IEquatable<OpenNettyCommand>
{
/// <summary>
/// Creates a new instance of the <see cref="OpenNettyCommand"/> structure.
/// </summary>
/// <param name="category">The category.</param>
/// <param name="value">The value.</param>
public OpenNettyCommand(OpenNettyCategory category, string value)
: this(category, value, [])
{
}
/// <summary>
/// Creates a new instance of the <see cref="OpenNettyCommand"/> structure.
/// </summary>
/// <param name="category">The category.</param>
/// <param name="value">The value.</param>
/// <param name="parameters">The additional parameters, if applicable.</param>
public OpenNettyCommand(OpenNettyCategory category, 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));
}
}
}
}
Category = category;
Value = value;
Parameters = parameters;
}
/// <summary>
/// Gets the category associated with the command.
/// </summary>
public OpenNettyCategory Category { get; }
/// <summary>
/// Gets the value associated with the command.
/// </summary>
public string Value { get; }
/// <summary>
/// Gets the additional parameters associated with the command, if applicable.
/// </summary>
public ImmutableArray<string> Parameters { get; } = [];
/// <inheritdoc/>
public bool Equals(OpenNettyCommand other)
{
if (Value is null)
{
return other.Value is null;
}
if (Category != other.Category)
{
return false;
}
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 OpenNettyCommand command && Equals(command);
/// <inheritdoc/>
public override int GetHashCode()
{
if (Value is null)
{
return 0;
}
var hash = new HashCode();
hash.Add(Category);
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 command.
/// </summary>
/// <returns>The <see cref="string"/> representation of the current command.</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 command to a list of <see cref="OpenNettyParameter"/>.
/// </summary>
/// <returns>The list of <see cref="OpenNettyParameter"/> representing this command.</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>
/// Creates a copy of the current instance with the specified parameters attached.
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns>A copy of the current instance with the specified parameters attached</returns>
public OpenNettyCommand WithParameters(params ImmutableArray<string> parameters) => new(Category, Value, parameters);
/// <summary>
/// Determines whether two <see cref="OpenNettyCommand"/> 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 ==(OpenNettyCommand left, OpenNettyCommand right) => left.Equals(right);
/// <summary>
/// Determines whether two <see cref="OpenNettyCommand"/> 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 !=(OpenNettyCommand left, OpenNettyCommand right) => !(left == right);
}