22
22
/**
23
23
* Class that encapsulates number of bytes, with helpers to handle units properly.
24
24
*/
25
- public final class ByteAmount implements Comparable < ByteAmount > {
25
+ public final class ByteAmount extends ResourceMeasure < Long > {
26
26
private static final long KB = 1024L ;
27
27
private static final long MB = KB * 1024 ;
28
28
private static final long GB = MB * 1024 ;
@@ -34,10 +34,9 @@ public final class ByteAmount implements Comparable<ByteAmount> {
34
34
private static final long MAX_KB = Math .round (Long .MAX_VALUE / KB );
35
35
36
36
public static final ByteAmount ZERO = ByteAmount .fromBytes (0 );
37
- private final long bytes ;
38
37
39
- private ByteAmount (long bytes ) {
40
- this . bytes = bytes ;
38
+ private ByteAmount (Long value ) {
39
+ super ( value ) ;
41
40
}
42
41
43
42
/**
@@ -67,7 +66,8 @@ public static ByteAmount fromMegabytes(long megabytes) {
67
66
68
67
/**
69
68
* Creates a ByteAmount value in gigabytes. If the gigabytes value
70
- * is >= Long.MAX_VALUE / 1024 / 1024 / 1024, the byte representation is capped at Long.MAX_VALUE.
69
+ * is >= Long.MAX_VALUE / 1024 / 1024 / 1024,
70
+ * the byte representation is capped at Long.MAX_VALUE.
71
71
*
72
72
* @param gigabytes value in gigabytes to represent
73
73
* @return a ByteAmount object repressing the number of GBs passed
@@ -85,7 +85,7 @@ public static ByteAmount fromGigabytes(long gigabytes) {
85
85
* @return number of bytes
86
86
*/
87
87
public long asBytes () {
88
- return bytes ;
88
+ return super . getValue () ;
89
89
}
90
90
91
91
/**
@@ -97,7 +97,7 @@ public long asBytes() {
97
97
* @return returns the ByteValue in MBs or 0 if the value is < (1024 * 1024) / 2
98
98
*/
99
99
public long asMegabytes () {
100
- return Math .round (( double ) bytes / MB );
100
+ return Math .round (value . doubleValue () / MB );
101
101
}
102
102
103
103
/**
@@ -109,7 +109,7 @@ public long asMegabytes() {
109
109
* @return returns the ByteValue in KBs or 0 if the value is < (1024) / 2
110
110
*/
111
111
public long asKilobytes () {
112
- return Math .round (( double ) bytes / KB );
112
+ return Math .round (value . doubleValue () / KB );
113
113
}
114
114
115
115
/**
@@ -121,15 +121,7 @@ public long asKilobytes() {
121
121
* @return returns the ByteValue in GBs or 0 if the value is < (1024 * 1024 * 1024) / 2
122
122
*/
123
123
public long asGigabytes () {
124
- return Math .round ((double ) bytes / GB );
125
- }
126
-
127
- /**
128
- * Convenience methdod to determine if byte value is zero
129
- * @return true if the byte value is 0
130
- */
131
- public boolean isZero () {
132
- return ZERO .equals (this );
124
+ return Math .round (value .doubleValue () / GB );
133
125
}
134
126
135
127
/**
@@ -138,10 +130,11 @@ public boolean isZero() {
138
130
* @return a new ByteValue of this minus other ByteValue
139
131
* @throws IllegalArgumentException if subtraction would overshoot Long.MIN_VALUE
140
132
*/
141
- public ByteAmount minus (ByteAmount other ) {
142
- checkArgument (Long .MIN_VALUE + other .asBytes () <= asBytes (), String .format (
143
- "Subtracting %s from %s would overshoot Long.MIN_LONG" , other , this ));
144
- return ByteAmount .fromBytes (asBytes () - other .asBytes ());
133
+ @ Override
134
+ public ByteAmount minus (ResourceMeasure <Long > other ) {
135
+ checkArgument (Long .MIN_VALUE + other .value <= value ,
136
+ String .format ("Subtracting %s from %s would overshoot Long.MIN_LONG" , other , this ));
137
+ return ByteAmount .fromBytes (value - other .value );
145
138
}
146
139
147
140
/**
@@ -150,10 +143,11 @@ public ByteAmount minus(ByteAmount other) {
150
143
* @return a new ByteValue of this plus other ByteValue
151
144
* @throws IllegalArgumentException if addition would exceed Long.MAX_VALUE
152
145
*/
153
- public ByteAmount plus (ByteAmount other ) {
154
- checkArgument (Long .MAX_VALUE - asBytes () >= other .asBytes (), String .format (
155
- "Adding %s to %s would exceed Long.MAX_LONG" , other , this ));
156
- return ByteAmount .fromBytes (asBytes () + other .asBytes ());
146
+ @ Override
147
+ public ByteAmount plus (ResourceMeasure <Long > other ) {
148
+ checkArgument (Long .MAX_VALUE - value >= other .value ,
149
+ String .format ("Adding %s to %s would exceed Long.MAX_LONG" , other , this ));
150
+ return ByteAmount .fromBytes (value + other .value );
157
151
}
158
152
159
153
/**
@@ -162,10 +156,11 @@ public ByteAmount plus(ByteAmount other) {
162
156
* @return a new ByteValue of this ByteValue multiplied by factor
163
157
* @throws IllegalArgumentException if multiplication would exceed Long.MAX_VALUE
164
158
*/
159
+ @ Override
165
160
public ByteAmount multiply (int factor ) {
166
- checkArgument (asBytes () <= Long .MAX_VALUE / factor , String . format (
167
- "Multiplying %s by %d would exceed Long.MAX_LONG" , this , factor ));
168
- return ByteAmount .fromBytes (asBytes () * factor );
161
+ checkArgument (value <= Long .MAX_VALUE / factor ,
162
+ String . format ( "Multiplying %s by %d would exceed Long.MAX_LONG" , this , factor ));
163
+ return ByteAmount .fromBytes (value * factor );
169
164
}
170
165
171
166
/**
@@ -175,9 +170,10 @@ public ByteAmount multiply(int factor) {
175
170
* @param factor value to divide by
176
171
* @return a new ByteValue of this ByteValue divided by factor
177
172
*/
173
+ @ Override
178
174
public ByteAmount divide (int factor ) {
179
175
checkArgument (factor != 0 , String .format ("Can not divide %s by 0" , this ));
180
- return ByteAmount .fromBytes (Math .round (( double ) this . asBytes () / ( double ) factor ));
176
+ return ByteAmount .fromBytes (Math .round (value . doubleValue () / factor ));
181
177
}
182
178
183
179
/**
@@ -187,30 +183,15 @@ public ByteAmount divide(int factor) {
187
183
* @return a new ByteValue of this ByteValue increased by percentage
188
184
* @throws IllegalArgumentException if increase would exceed Long.MAX_VALUE
189
185
*/
186
+ @ Override
190
187
public ByteAmount increaseBy (int percentage ) {
191
- checkArgument (percentage >= 0 , String . format (
192
- "Increasing by negative percent (%d) not supported" , percentage ));
188
+ checkArgument (percentage >= 0 ,
189
+ String . format ( "Increasing by negative percent (%d) not supported" , percentage ));
193
190
double factor = 1.0 + ((double ) percentage / 100 );
194
191
long max = Math .round (Long .MAX_VALUE / factor );
195
- checkArgument (asBytes () <= max , String .format (
196
- "Increasing %s by %d percent would exceed Long.MAX_LONG" , this , percentage ));
197
- return ByteAmount .fromBytes (Math .round ((double ) asBytes () * factor ));
198
- }
199
-
200
- public boolean greaterThan (ByteAmount other ) {
201
- return this .asBytes () > other .asBytes ();
202
- }
203
-
204
- public boolean greaterOrEqual (ByteAmount other ) {
205
- return this .asBytes () >= other .asBytes ();
206
- }
207
-
208
- public boolean lessThan (ByteAmount other ) {
209
- return this .asBytes () < other .asBytes ();
210
- }
211
-
212
- public boolean lessOrEqual (ByteAmount other ) {
213
- return this .asBytes () <= other .asBytes ();
192
+ checkArgument (value <= max ,
193
+ String .format ("Increasing %s by %d percent would exceed Long.MAX_LONG" , this , percentage ));
194
+ return ByteAmount .fromBytes (Math .round (value .doubleValue () * factor ));
214
195
}
215
196
216
197
public ByteAmount max (ByteAmount other ) {
@@ -221,42 +202,19 @@ public ByteAmount max(ByteAmount other) {
221
202
}
222
203
}
223
204
224
- @ Override
225
- public int compareTo (ByteAmount other ) {
226
- return Long .compare (asBytes (), other .asBytes ());
227
- }
228
-
229
- @ Override
230
- public boolean equals (Object other ) {
231
- if (this == other ) {
232
- return true ;
233
- }
234
- if (other == null || getClass () != other .getClass ()) {
235
- return false ;
236
- }
237
-
238
- ByteAmount that = (ByteAmount ) other ;
239
- return bytes == that .bytes ;
240
- }
241
-
242
- @ Override
243
- public int hashCode () {
244
- return (int ) (bytes ^ (bytes >>> 32 ));
245
- }
246
-
247
205
@ Override
248
206
public String toString () {
249
- String value ;
207
+ String str ;
250
208
if (asGigabytes () > 0 ) {
251
- value = String .format ("%.1f GB (%d bytes)" , ( double ) asBytes () / GB , asBytes () );
209
+ str = String .format ("%.1f GB (%d bytes)" , value . doubleValue () / GB , value );
252
210
} else if (asMegabytes () > 0 ) {
253
- value = String .format ("%.1f MB (%d bytes)" , ( double ) asBytes () / MB , asBytes () );
211
+ str = String .format ("%.1f MB (%d bytes)" , value . doubleValue () / MB , value );
254
212
} else if (asKilobytes () > 0 ) {
255
- value = String .format ("%.1f KB (%d bytes)" , ( double ) asBytes () / KB , asBytes () );
213
+ str = String .format ("%.1f KB (%d bytes)" , value . doubleValue () / KB , value );
256
214
} else {
257
- value = bytes + " bytes" ;
215
+ str = value + " bytes" ;
258
216
}
259
- return String .format ("ByteAmount{%s}" , value );
217
+ return String .format ("ByteAmount{%s}" , str );
260
218
}
261
219
262
220
private void checkArgument (boolean condition , String errorMessage ) {
0 commit comments