7
7
8
8
namespace RabbitMQ . AMQP . Client
9
9
{
10
- public partial class ByteCapacity : IEquatable < ByteCapacity >
10
+ /// <summary>
11
+ /// Class for specifying a binary size, with units
12
+ /// </summary>
13
+ public class ByteCapacity : IEquatable < ByteCapacity >
11
14
{
15
+ private const int KilobytesMultiplier = 1000 ;
16
+ private const int MegabytesMultiplier = 1000 * 1000 ;
17
+ private const int GigabytesMultiplier = 1000 * 1000 * 1000 ;
18
+ private const long TerabytesMultiplier = 1000L * 1000L * 1000L * 1000L ;
19
+
20
+ private static readonly Regex s_sizeRegex = new ( @"^(\d+)([kKmMgGtTpP]?[bB]?)$" , RegexOptions . Compiled ) ;
21
+
12
22
private readonly long _bytes ;
13
- private string _input ;
14
23
15
- private ByteCapacity ( long bytes , string input )
24
+ /// <summary>
25
+ ///
26
+ /// </summary>
27
+ /// <param name="bytes"></param>
28
+ public ByteCapacity ( long bytes )
16
29
{
17
- _input = input ;
18
30
_bytes = bytes ;
19
31
}
20
32
21
- private ByteCapacity ( long bytes ) : this ( bytes , bytes . ToString ( ) )
22
- {
23
- }
24
-
25
- private const int KilobytesMultiplier = 1000 ;
26
- private const int MegabytesMultiplier = 1000 * 1000 ;
27
- private const int GigabytesMultiplier = 1000 * 1000 * 1000 ;
28
- private const long TerabytesMultiplier = 1000L * 1000L * 1000L * 1000L ;
29
-
33
+ /// <summary>
34
+ /// Specify an amount in bytes
35
+ /// </summary>
36
+ /// <param name="bytes">The size, in bytes</param>
37
+ /// <returns><see cref="ByteCapacity"/></returns>
30
38
public static ByteCapacity B ( long bytes )
31
39
{
32
40
return new ByteCapacity ( bytes ) ;
33
41
}
34
42
35
- public static ByteCapacity Kb ( long megabytes )
43
+ /// <summary>
44
+ /// Specify an amount, in kilobytes
45
+ /// </summary>
46
+ /// <param name="kilobytes">The size, in kilobytes</param>
47
+ /// <returns><see cref="ByteCapacity"/></returns>
48
+ public static ByteCapacity Kb ( long kilobytes )
36
49
{
37
- return new ByteCapacity ( megabytes * KilobytesMultiplier ) ;
50
+ return new ByteCapacity ( kilobytes * KilobytesMultiplier ) ;
38
51
}
39
52
53
+ /// <summary>
54
+ /// Specify an amount, in megabytes
55
+ /// </summary>
56
+ /// <param name="megabytes">The size, in megabytes</param>
57
+ /// <returns><see cref="ByteCapacity"/></returns>
40
58
public static ByteCapacity Mb ( long megabytes )
41
59
{
42
60
return new ByteCapacity ( megabytes * MegabytesMultiplier ) ;
43
61
}
44
62
63
+ /// <summary>
64
+ /// Specify an amount, in gigabytes
65
+ /// </summary>
66
+ /// <param name="gigabytes">The size, in gigabytes</param>
67
+ /// <returns><see cref="ByteCapacity"/></returns>
45
68
public static ByteCapacity Gb ( long gigabytes )
46
69
{
47
70
return new ByteCapacity ( gigabytes * GigabytesMultiplier ) ;
48
71
}
49
72
73
+ /// <summary>
74
+ /// Specify an amount, in terabytes
75
+ /// </summary>
76
+ /// <param name="terabytes">The size, in terabytes</param>
77
+ /// <returns><see cref="ByteCapacity"/></returns>
50
78
public static ByteCapacity Tb ( long terabytes )
51
79
{
52
80
return new ByteCapacity ( terabytes * TerabytesMultiplier ) ;
53
81
}
54
82
55
- private static readonly Regex s_sizeRegex = new Regex ( @"^(\d+)([kKmMgGtTpP]?[bB]?)$" , RegexOptions . Compiled ) ;
83
+ /// <summary>
84
+ /// Explicitly convert a string into a <see cref="ByteCapacity"/>
85
+ /// </summary>
86
+ /// <param name="value">The value, as string</param>
87
+ /// <returns><see cref="ByteCapacity"/></returns>
88
+ public static explicit operator ByteCapacity ( string value )
89
+ {
90
+ return Parse ( value ) ;
91
+ }
92
+
93
+ /// <summary>
94
+ /// Cast a <see cref="ByteCapacity"/> into a <see cref="long"/>
95
+ /// </summary>
96
+ /// <param name="value">The value</param>
97
+ /// <returns>The total number of bytes.</returns>
98
+ public static implicit operator long ( ByteCapacity value )
99
+ {
100
+ return value . _bytes ;
101
+ }
56
102
57
- public static ByteCapacity From ( string value )
103
+ /// <summary>
104
+ /// Parse a string into a <see cref="ByteCapacity"/>
105
+ /// </summary>
106
+ /// <param name="value">The value, as string</param>
107
+ /// <returns><see cref="ByteCapacity"/></returns>
108
+ public static ByteCapacity Parse ( string value )
58
109
{
59
110
Match match = s_sizeRegex . Match ( value ) ;
60
111
if ( ! match . Success )
61
112
{
62
113
throw new ArgumentException ( "Invalid capacity size format." , nameof ( value ) ) ;
63
114
}
64
115
65
- var size = long . Parse ( match . Groups [ 1 ] . Value ) ;
66
- var unit = match . Groups [ 2 ] . Value . ToLower ( ) ;
116
+ long size = long . Parse ( match . Groups [ 1 ] . Value ) ;
117
+ string unit = match . Groups [ 2 ] . Value . ToLowerInvariant ( ) ;
67
118
68
119
return unit switch
69
120
{
@@ -75,24 +126,53 @@ public static ByteCapacity From(string value)
75
126
} ;
76
127
}
77
128
78
- public long ToBytes ( )
129
+ /// <summary>
130
+ /// Returns a value indicating whether this instance is equal to a specified <see cref="ByteCapacity"/> value.
131
+ /// </summary>
132
+ /// <param name="obj">An object to compare with this instance.</param>
133
+ /// <returns>true if obj is an instance of <see cref="ByteCapacity"/>and equals the value of this instance; otherwise, false.</returns>
134
+ public override bool Equals ( object ? obj )
79
135
{
80
- return _bytes ;
136
+ if ( obj is null )
137
+ {
138
+ return false ;
139
+ }
140
+
141
+ if ( ReferenceEquals ( this , obj ) )
142
+ {
143
+ return true ;
144
+ }
145
+
146
+ return Equals ( obj as ByteCapacity ) ;
81
147
}
82
148
83
- public bool Equals ( ByteCapacity ? other )
149
+ /// <summary>
150
+ /// Returns a value indicating whether this instance is equal to a specified <see cref="ByteCapacity"/> value.
151
+ /// </summary>
152
+ /// <param name="obj">An object to compare with this instance.</param>
153
+ /// <returns>true if obj has the same value as this instance; otherwise, false.</returns>
154
+ public bool Equals ( ByteCapacity ? obj )
84
155
{
85
- if ( ReferenceEquals ( null , other ) )
156
+ if ( obj is null )
86
157
{
87
158
return false ;
88
159
}
89
160
90
- if ( ReferenceEquals ( this , other ) )
161
+ if ( ReferenceEquals ( this , obj ) )
91
162
{
92
163
return true ;
93
164
}
94
165
95
- return _bytes == other . _bytes ;
166
+ return _bytes == obj . _bytes ;
167
+ }
168
+
169
+ /// <summary>
170
+ /// Returns the hash code for this instance.
171
+ /// </summary>
172
+ /// <returns>A 32-bit signed integer hash code.</returns>
173
+ public override int GetHashCode ( )
174
+ {
175
+ return _bytes . GetHashCode ( ) ;
96
176
}
97
177
}
98
178
}
0 commit comments