1+ /*
2+ * Copyright 2016 The LmdbJava Project, http://lmdbjava.org/
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
116package org .lmdbjava ;
217
318import java .text .DecimalFormat ;
419import java .text .NumberFormat ;
520
621/**
7- * A {@code BinaryByteUnit} represents power-of-two byte sizes at a given unit of granularity and
8- * provides utility methods to convert across units. A {@code BinaryByteUnit} does not maintain
9- * byte size information, but only helps organize and use byte size representations that may be
10- * maintained separately across various contexts.
22+ * A {@code BinaryByteUnit} represents power-of-two byte sizes at a given unit
23+ * of granularity and provides utility methods to convert across units. A
24+ * {@code BinaryByteUnit} does not maintain byte size information, but only
25+ * helps organize and use byte size representations that may be maintained
26+ * separately across various contexts.
1127 *
1228 * @author Jake Wharton
1329 */
1430public enum ByteUnit {
1531
16- /** Byte unit representing one byte. */
32+ /**
33+ * Byte unit representing one byte.
34+ */
1735 BYTES {
1836 @ Override public long convert (long sourceCount , ByteUnit sourceUnit ) {
1937 return sourceUnit .toBytes (sourceCount );
@@ -43,8 +61,9 @@ public enum ByteUnit {
4361 return count / (PB / B );
4462 }
4563 },
46-
47- /** A byte unit representing 1024 bytes. */
64+ /**
65+ * A byte unit representing 1024 bytes.
66+ */
4867 KIBIBYTES {
4968 @ Override public long convert (long sourceCount , ByteUnit sourceUnit ) {
5069 return sourceUnit .toKibibytes (sourceCount );
@@ -74,8 +93,9 @@ public enum ByteUnit {
7493 return count / (PB / KB );
7594 }
7695 },
77-
78- /** A byte unit representing 1024 kibibytes. */
96+ /**
97+ * A byte unit representing 1024 kibibytes.
98+ */
7999 MEBIBYTES {
80100 @ Override public long convert (long sourceCount , ByteUnit sourceUnit ) {
81101 return sourceUnit .toMebibytes (sourceCount );
@@ -105,8 +125,9 @@ public enum ByteUnit {
105125 return count / (PB / MB );
106126 }
107127 },
108-
109- /** A byte unit representing 1024 mebibytes. */
128+ /**
129+ * A byte unit representing 1024 mebibytes.
130+ */
110131 GIBIBYTES {
111132 @ Override public long convert (long sourceCount , ByteUnit sourceUnit ) {
112133 return sourceUnit .toGibibytes (sourceCount );
@@ -136,8 +157,9 @@ public enum ByteUnit {
136157 return count / (PB / GB );
137158 }
138159 },
139-
140- /** A byte unit representing 1024 gibibytes. */
160+ /**
161+ * A byte unit representing 1024 gibibytes.
162+ */
141163 TEBIBYTES {
142164 @ Override public long convert (long sourceCount , ByteUnit sourceUnit ) {
143165 return sourceUnit .toTebibytes (sourceCount );
@@ -167,8 +189,9 @@ public enum ByteUnit {
167189 return count / (PB / TB );
168190 }
169191 },
170-
171- /** A byte unit representing 1024 tebibytes. */
192+ /**
193+ * A byte unit representing 1024 tebibytes.
194+ */
172195 PEBIBYTES {
173196 @ Override public long convert (long sourceCount , ByteUnit sourceUnit ) {
174197 return sourceUnit .toPebibytes (sourceCount );
@@ -200,115 +223,149 @@ public enum ByteUnit {
200223 };
201224
202225 private static final long B = 1L ;
203- private static final long KB = B * 1024L ;
204- private static final long MB = KB * 1024L ;
205- private static final long GB = MB * 1024L ;
206- private static final long TB = GB * 1024L ;
207- private static final long PB = TB * 1024L ;
226+ private static final long KB = B * 1_024L ;
227+ private static final long MB = KB * 1_024L ;
228+ private static final long GB = MB * 1_024L ;
229+ private static final long TB = GB * 1_024L ;
230+ private static final long PB = TB * 1_024L ;
208231
209232 private static final long MAX = Long .MAX_VALUE ;
210233
211234 /**
212- * Converts the given size in the given unit to bytes. Conversions with arguments that would
213- * numerically overflow saturate to {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE}
214- * if positive.
235+ * Converts the given size in the given unit to bytes. Conversions with
236+ * arguments that would numerically overflow saturate to
237+ * {@code Long.MIN_VALUE} if negative or {@code Long.MAX_VALUE} if positive.
215238 *
216239 * @param count the bit count
217- * @return the converted count, or {@code Long.MIN_VALUE} if conversion would negatively
218- * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
240+ * @return the converted count, or {@code Long.MIN_VALUE} if conversion would
241+ * negatively overflow, or {@code Long.MAX_VALUE} if it would
242+ * positively overflow.
219243 */
220- public long toBytes (long count ) { throw new AbstractMethodError (); }
244+ public long toBytes (long count ) {
245+ throw new AbstractMethodError ();
246+ }
221247
222248 /**
223- * Converts the given size in the given unit to this unit. Conversions from finer to coarser
224- * granularities truncate, so lose precision. For example, converting from {@code 999} bytes to
225- * kibibytes results in {@code 0}. Conversions from coarser to finer granularities with arguments
226- * that would numerically overflow saturate to {@code Long.MIN_VALUE} if negative or
249+ * Converts the given size in the given unit to this unit. Conversions from
250+ * finer to coarser granularities truncate, so lose precision. For example,
251+ * converting from {@code 999} bytes to kibibytes results in {@code 0}.
252+ * Conversions from coarser to finer granularities with arguments that would
253+ * numerically overflow saturate to {@code Long.MIN_VALUE} if negative or
227254 * {@code Long.MAX_VALUE} if positive.
228255 * <p>
229256 * For example, to convert 10 kilobytes to bytes, use:
230257 * {@code ByteUnit.KIBIBYTES.convert(10, ByteUnit.BYTES)}
231258 *
232259 * @param sourceCount the size in the given {@code sourceUnit}.
233- * @param sourceUnit the unit of the {@code sourceCount} argument.
234- * @return the converted size in this unit, or {@code Long.MIN_VALUE} if conversion would
235- * negatively overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
260+ * @param sourceUnit the unit of the {@code sourceCount} argument.
261+ * @return the converted size in this unit, or {@code Long.MIN_VALUE} if
262+ * conversion would negatively overflow, or {@code Long.MAX_VALUE} if
263+ * it would positively overflow.
236264 */
237265 public long convert (long sourceCount , ByteUnit sourceUnit ) {
238266 throw new AbstractMethodError ();
239267 }
240268
241269 /**
242- * Equivalent to {@link #convert(long, ByteUnit) KIBIBYTES.convert(count, this)}.
270+ * Equivalent to
271+ * {@link #convert(long, ByteUnit) KIBIBYTES.convert(count, this)}.
272+ *
243273 * @param count the bit count
244- * @return the converted count, or {@code Long.MIN_VALUE} if conversion would negatively
245- * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
274+ * @return the converted count, or {@code Long.MIN_VALUE} if conversion would
275+ * negatively overflow, or {@code Long.MAX_VALUE} if it would
276+ * positively overflow.
246277 */
247278 public long toKibibytes (long count ) {
248279 throw new AbstractMethodError ();
249280 }
250281
251282 /**
252- * Equivalent to {@link #convert(long, ByteUnit) MEBIBYTES.convert(count, this)}.
283+ * Equivalent to
284+ * {@link #convert(long, ByteUnit) MEBIBYTES.convert(count, this)}.
285+ *
253286 * @param count the bit count
254- * @return the converted count, or {@code Long.MIN_VALUE} if conversion would negatively
255- * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
287+ * @return the converted count, or {@code Long.MIN_VALUE} if conversion would
288+ * negatively overflow, or {@code Long.MAX_VALUE} if it would
289+ * positively overflow.
256290 */
257291 public long toMebibytes (long count ) {
258292 throw new AbstractMethodError ();
259293 }
260294
261295 /**
262- * Equivalent to {@link #convert(long, ByteUnit) GIBIBYTES.convert(count, this)}.
296+ * Equivalent to
297+ * {@link #convert(long, ByteUnit) GIBIBYTES.convert(count, this)}.
298+ *
263299 * @param count the bit count
264- * @return the converted count, or {@code Long.MIN_VALUE} if conversion would negatively
265- * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
300+ * @return the converted count, or {@code Long.MIN_VALUE} if conversion would
301+ * negatively overflow, or {@code Long.MAX_VALUE} if it would
302+ * positively overflow.
266303 */
267304 public long toGibibytes (long count ) {
268305 throw new AbstractMethodError ();
269306 }
270307
271308 /**
272- * Equivalent to {@link #convert(long, ByteUnit) TEBIBYTES.convert(count, this)}.
309+ * Equivalent to
310+ * {@link #convert(long, ByteUnit) TEBIBYTES.convert(count, this)}.
311+ *
273312 * @param count the bit count
274- * @return the converted count, or {@code Long.MIN_VALUE} if conversion would negatively
275- * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
313+ * @return the converted count, or {@code Long.MIN_VALUE} if conversion would
314+ * negatively overflow, or {@code Long.MAX_VALUE} if it would
315+ * positively overflow.
276316 */
277317 public long toTebibytes (long count ) {
278318 throw new AbstractMethodError ();
279319 }
280320
281321 /**
282- * Equivalent to {@link #convert(long, ByteUnit) PEBIBYTES.convert(count, this)}.
322+ * Equivalent to
323+ * {@link #convert(long, ByteUnit) PEBIBYTES.convert(count, this)}.
324+ *
283325 * @param count the bit count
284- * @return the converted count, or {@code Long.MIN_VALUE} if conversion would negatively
285- * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
326+ * @return the converted count, or {@code Long.MIN_VALUE} if conversion would
327+ * negatively overflow, or {@code Long.MAX_VALUE} if it would
328+ * positively overflow.
286329 */
287330 public long toPebibytes (long count ) {
288331 throw new AbstractMethodError ();
289332 }
290333
291- private static final String [] UNITS = { "B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" };
334+ private static final String [] UNITS = {"B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" };
292335
293336 /**
294- * Return {@code bytes} as human-readable size string (e.g., "1.2 GiB". This will use a default
295- * {@link DecimalFormat} instance for formatting the number.
337+ * Return {@code bytes} as human-readable size string (e.g., "1.2 GiB". This
338+ * will use a default {@link DecimalFormat} instance for formatting the
339+ * number.
340+ *
341+ * @param bytes
342+ * @return
296343 */
297344 public static String format (long bytes ) {
298345 return format (bytes , new DecimalFormat (DEFAULT_FORMAT_PATTERN ));
299346 }
300347
301348 /**
302- * Return {@code bytes} as human-readable size string (e.g., "1.2 GiB". This will use a
303- * {@link DecimalFormat} instance with {@code pattern} for formatting the number.
349+ * Return {@code bytes} as human-readable size string (e.g., "1.2 GiB". This
350+ * will use a {@link DecimalFormat} instance with {@code pattern} for
351+ * formatting the number.
352+ *
353+ * @param bytes
354+ * @param pattern
355+ * @return
304356 */
305357 public static String format (long bytes , String pattern ) {
306358 return format (bytes , new DecimalFormat (pattern ));
307359 }
308360
309361 /**
310- * Return {@code bytes} as human-readable size string (e.g., "1.2 GiB". This will use {@code
362+ * Return {@code bytes} as human-readable size string (e.g., "1.2 GiB". This
363+ * will use {@code
311364 * format} for formatting the number.
365+ *
366+ * @param bytes
367+ * @param format
368+ * @return
312369 */
313370 public static String format (long bytes , NumberFormat format ) {
314371 if (bytes < 0 ) {
@@ -326,7 +383,9 @@ public static String format(long bytes, NumberFormat format) {
326383
327384 static final String DEFAULT_FORMAT_PATTERN = "#,##0.#" ;
328385
329- /** Multiply {@code size} by {@code factor} accounting for overflow. */
386+ /**
387+ * Multiply {@code size} by {@code factor} accounting for overflow.
388+ */
330389 private static long multiply (long size , long factor , long over ) {
331390 if (size > over ) {
332391 return Long .MAX_VALUE ;
@@ -336,4 +395,4 @@ private static long multiply(long size, long factor, long over) {
336395 }
337396 return size * factor ;
338397 }
339- }
398+ }
0 commit comments