|
14 | 14 | @JsonAdapter(Quantity.QuantityAdapter.class)
|
15 | 15 | public class Quantity {
|
16 | 16 |
|
17 |
| - private final BigDecimal number; |
18 |
| - private Format format; |
| 17 | + private final BigDecimal number; |
| 18 | + private Format format; |
19 | 19 |
|
20 |
| - public enum Format { |
21 |
| - DECIMAL_EXPONENT(10), DECIMAL_SI(10), BINARY_SI(2); |
| 20 | + public enum Format { |
| 21 | + DECIMAL_EXPONENT(10), DECIMAL_SI(10), BINARY_SI(2); |
22 | 22 |
|
23 |
| - private int base; |
| 23 | + private int base; |
24 | 24 |
|
25 |
| - Format(final int base) { |
26 |
| - this.base = base; |
27 |
| - } |
28 |
| - |
29 |
| - public int getBase() { |
30 |
| - return base; |
31 |
| - } |
| 25 | + Format(final int base) { |
| 26 | + this.base = base; |
32 | 27 | }
|
33 | 28 |
|
34 |
| - public Quantity(final BigDecimal number, final Format format) { |
35 |
| - this.number = number; |
36 |
| - this.format = format; |
| 29 | + public int getBase() { |
| 30 | + return base; |
37 | 31 | }
|
38 |
| - |
39 |
| - public Quantity(final String value) { |
40 |
| - this.number = fromString(value).number; |
41 |
| - this.format = fromString(value).format; |
| 32 | + } |
| 33 | + |
| 34 | + public Quantity(final BigDecimal number, final Format format) { |
| 35 | + this.number = number; |
| 36 | + this.format = format; |
| 37 | + } |
| 38 | + |
| 39 | + public Quantity(final String value) { |
| 40 | + this.number = fromString(value).number; |
| 41 | + this.format = fromString(value).format; |
| 42 | + } |
| 43 | + |
| 44 | + public BigDecimal getNumber() { |
| 45 | + return number; |
| 46 | + } |
| 47 | + |
| 48 | + public Format getFormat() { |
| 49 | + return format; |
| 50 | + } |
| 51 | + |
| 52 | + public static Quantity fromString(final String value) { |
| 53 | + return new QuantityFormatter().parse(value); |
| 54 | + } |
| 55 | + |
| 56 | + public String toSuffixedString() { |
| 57 | + return new QuantityFormatter().format(this); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + return "Quantity{" + |
| 63 | + "number=" + number + |
| 64 | + ", format=" + format + |
| 65 | + '}'; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object o) { |
| 70 | + if (this == o) { |
| 71 | + return true; |
42 | 72 | }
|
43 |
| - |
44 |
| - public BigDecimal getNumber() { |
45 |
| - return number; |
46 |
| - } |
47 |
| - |
48 |
| - public Format getFormat() { |
49 |
| - return format; |
| 73 | + if (o == null || getClass() != o.getClass()) { |
| 74 | + return false; |
50 | 75 | }
|
51 | 76 |
|
52 |
| - public static Quantity fromString(final String value) { |
53 |
| - return new QuantityFormatter().parse(value); |
54 |
| - } |
| 77 | + Quantity otherQuantity = (Quantity) o; |
55 | 78 |
|
56 |
| - public String toSuffixedString() { |
57 |
| - return new QuantityFormatter().format(this); |
58 |
| - } |
| 79 | + return ObjectUtils.compare(this.number, otherQuantity.number) == 0 && |
| 80 | + Objects.equals(this.format, otherQuantity.format); |
| 81 | + } |
59 | 82 |
|
| 83 | + public class QuantityAdapter extends TypeAdapter<Quantity> { |
60 | 84 | @Override
|
61 |
| - public String toString() { |
62 |
| - return "Quantity{" + |
63 |
| - "number=" + number + |
64 |
| - ", format=" + format + |
65 |
| - '}'; |
| 85 | + public void write(JsonWriter jsonWriter, Quantity quantity) throws IOException { |
| 86 | + jsonWriter.value(quantity.toSuffixedString()); |
66 | 87 | }
|
67 | 88 |
|
68 | 89 | @Override
|
69 |
| - public boolean equals(Object o) { |
70 |
| - if (this == o) { |
71 |
| - return true; |
72 |
| - } |
73 |
| - if (o == null || getClass() != o.getClass()) { |
74 |
| - return false; |
75 |
| - } |
76 |
| - |
77 |
| - Quantity otherQuantity = (Quantity) o; |
78 |
| - |
79 |
| - return ObjectUtils.compare(this.number, otherQuantity.number) == 0 && |
80 |
| - Objects.equals(this.format, otherQuantity.format); |
81 |
| - } |
82 |
| - |
83 |
| - public class QuantityAdapter extends TypeAdapter<Quantity> { |
84 |
| - @Override |
85 |
| - public void write(JsonWriter jsonWriter, Quantity quantity) throws IOException { |
86 |
| - jsonWriter.value(quantity.toSuffixedString()); |
87 |
| - } |
88 |
| - |
89 |
| - @Override |
90 |
| - public Quantity read(JsonReader jsonReader) throws IOException { |
91 |
| - return Quantity.fromString(jsonReader.nextString()); |
92 |
| - } |
| 90 | + public Quantity read(JsonReader jsonReader) throws IOException { |
| 91 | + return Quantity.fromString(jsonReader.nextString()); |
93 | 92 | }
|
| 93 | + } |
94 | 94 | }
|
0 commit comments