77
88namespace RabbitMQ . AMQP . Client
99{
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 >
1114 {
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+
1222 private readonly long _bytes ;
13- private string _input ;
1423
15- private ByteCapacity ( long bytes , string input )
24+ /// <summary>
25+ ///
26+ /// </summary>
27+ /// <param name="bytes"></param>
28+ public ByteCapacity ( long bytes )
1629 {
17- _input = input ;
1830 _bytes = bytes ;
1931 }
2032
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>
3038 public static ByteCapacity B ( long bytes )
3139 {
3240 return new ByteCapacity ( bytes ) ;
3341 }
3442
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 )
3649 {
37- return new ByteCapacity ( megabytes * KilobytesMultiplier ) ;
50+ return new ByteCapacity ( kilobytes * KilobytesMultiplier ) ;
3851 }
3952
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>
4058 public static ByteCapacity Mb ( long megabytes )
4159 {
4260 return new ByteCapacity ( megabytes * MegabytesMultiplier ) ;
4361 }
4462
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>
4568 public static ByteCapacity Gb ( long gigabytes )
4669 {
4770 return new ByteCapacity ( gigabytes * GigabytesMultiplier ) ;
4871 }
4972
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>
5078 public static ByteCapacity Tb ( long terabytes )
5179 {
5280 return new ByteCapacity ( terabytes * TerabytesMultiplier ) ;
5381 }
5482
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+ }
56102
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 )
58109 {
59110 Match match = s_sizeRegex . Match ( value ) ;
60111 if ( ! match . Success )
61112 {
62113 throw new ArgumentException ( "Invalid capacity size format." , nameof ( value ) ) ;
63114 }
64115
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 ( ) ;
67118
68119 return unit switch
69120 {
@@ -75,24 +126,53 @@ public static ByteCapacity From(string value)
75126 } ;
76127 }
77128
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 )
79135 {
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 ) ;
81147 }
82148
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 )
84155 {
85- if ( ReferenceEquals ( null , other ) )
156+ if ( obj is null )
86157 {
87158 return false ;
88159 }
89160
90- if ( ReferenceEquals ( this , other ) )
161+ if ( ReferenceEquals ( this , obj ) )
91162 {
92163 return true ;
93164 }
94165
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 ( ) ;
96176 }
97177 }
98178}
0 commit comments