1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.spring.format;
17
18 import java.util.Locale;
19
20 import org.kuali.common.util.FormatUtils;
21 import org.kuali.common.util.primitives.Numbers;
22 import org.springframework.format.Formatter;
23
24 public final class BytesFormatter implements Formatter<Number> {
25
26 public BytesFormatter() {
27 this(true);
28 }
29
30 public BytesFormatter(boolean printDecimalDigits) {
31 this.printDecimalDigits = printDecimalDigits;
32 }
33
34 public boolean isPrintDecimalDigits() {
35 return printDecimalDigits;
36 }
37
38 private final boolean printDecimalDigits;
39
40 @Override
41 public Number parse(String size, Locale locale) {
42 return Numbers.narrow(FormatUtils.getBytes(size));
43 }
44
45 @Override
46 public String print(Number number, Locale locale) {
47 if (printDecimalDigits) {
48 return FormatUtils.getSize(number.longValue());
49 } else {
50 return FormatUtils.getIntegerSize(number.longValue());
51 }
52 }
53
54 }