|
16 | 16 |
|
17 | 17 | package cd.go.contrib.elasticagent.utils;
|
18 | 18 |
|
19 |
| -import com.google.common.collect.ImmutableSortedMap; |
20 | 19 | import io.fabric8.kubernetes.api.model.Quantity;
|
21 | 20 | import org.apache.commons.lang3.StringUtils;
|
22 | 21 |
|
23 | 22 | import java.text.DecimalFormat;
|
| 23 | +import java.util.Collections; |
24 | 24 | import java.util.Locale;
|
25 | 25 | import java.util.Map;
|
26 | 26 | import java.util.Objects;
|
| 27 | +import java.util.TreeMap; |
27 | 28 | import java.util.regex.Matcher;
|
28 | 29 | import java.util.regex.Pattern;
|
29 | 30 |
|
30 |
| -import static com.google.common.base.Preconditions.checkArgument; |
31 | 31 | import static java.util.Objects.requireNonNull;
|
32 | 32 |
|
33 | 33 | public class Size implements Comparable<Size> {
|
34 | 34 | private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S+)");
|
35 | 35 |
|
36 |
| - private static final Map<String, SizeUnit> SUFFIXES = ImmutableSortedMap.<String, SizeUnit>orderedBy(String.CASE_INSENSITIVE_ORDER) |
37 |
| - .put("B", SizeUnit.BYTES) |
38 |
| - .put("byte", SizeUnit.BYTES) |
39 |
| - .put("bytes", SizeUnit.BYTES) |
40 |
| - .put("K", SizeUnit.KILOBYTES) |
41 |
| - .put("KB", SizeUnit.KILOBYTES) |
42 |
| - .put("Ki", SizeUnit.KILOBYTES) |
43 |
| - .put("KiB", SizeUnit.KILOBYTES) |
44 |
| - .put("kilobyte", SizeUnit.KILOBYTES) |
45 |
| - .put("kilobytes", SizeUnit.KILOBYTES) |
46 |
| - .put("M", SizeUnit.MEGABYTES) |
47 |
| - .put("Mi", SizeUnit.MEGABYTES) |
48 |
| - .put("MB", SizeUnit.MEGABYTES) |
49 |
| - .put("MiB", SizeUnit.MEGABYTES) |
50 |
| - .put("megabyte", SizeUnit.MEGABYTES) |
51 |
| - .put("megabytes", SizeUnit.MEGABYTES) |
52 |
| - .put("G", SizeUnit.GIGABYTES) |
53 |
| - .put("Gi", SizeUnit.GIGABYTES) |
54 |
| - .put("GB", SizeUnit.GIGABYTES) |
55 |
| - .put("GiB", SizeUnit.GIGABYTES) |
56 |
| - .put("gigabyte", SizeUnit.GIGABYTES) |
57 |
| - .put("gigabytes", SizeUnit.GIGABYTES) |
58 |
| - .put("T", SizeUnit.TERABYTES) |
59 |
| - .put("TB", SizeUnit.TERABYTES) |
60 |
| - .put("Ti", SizeUnit.TERABYTES) |
61 |
| - .put("TiB", SizeUnit.TERABYTES) |
62 |
| - .put("terabyte", SizeUnit.TERABYTES) |
63 |
| - .put("terabytes", SizeUnit.TERABYTES) |
64 |
| - .build(); |
| 36 | + private static final Map<String, SizeUnit> SUFFIXES; |
| 37 | + |
| 38 | + static { |
| 39 | + Map<String, SizeUnit> tmpSuffixes = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
| 40 | + tmpSuffixes.put("B", SizeUnit.BYTES); |
| 41 | + tmpSuffixes.put("byte", SizeUnit.BYTES); |
| 42 | + tmpSuffixes.put("bytes", SizeUnit.BYTES); |
| 43 | + tmpSuffixes.put("K", SizeUnit.KILOBYTES); |
| 44 | + tmpSuffixes.put("KB", SizeUnit.KILOBYTES); |
| 45 | + tmpSuffixes.put("Ki", SizeUnit.KILOBYTES); |
| 46 | + tmpSuffixes.put("KiB", SizeUnit.KILOBYTES); |
| 47 | + tmpSuffixes.put("kilobyte", SizeUnit.KILOBYTES); |
| 48 | + tmpSuffixes.put("kilobytes", SizeUnit.KILOBYTES); |
| 49 | + tmpSuffixes.put("M", SizeUnit.MEGABYTES); |
| 50 | + tmpSuffixes.put("Mi", SizeUnit.MEGABYTES); |
| 51 | + tmpSuffixes.put("MB", SizeUnit.MEGABYTES); |
| 52 | + tmpSuffixes.put("MiB", SizeUnit.MEGABYTES); |
| 53 | + tmpSuffixes.put("megabyte", SizeUnit.MEGABYTES); |
| 54 | + tmpSuffixes.put("megabytes", SizeUnit.MEGABYTES); |
| 55 | + tmpSuffixes.put("G", SizeUnit.GIGABYTES); |
| 56 | + tmpSuffixes.put("Gi", SizeUnit.GIGABYTES); |
| 57 | + tmpSuffixes.put("GB", SizeUnit.GIGABYTES); |
| 58 | + tmpSuffixes.put("GiB", SizeUnit.GIGABYTES); |
| 59 | + tmpSuffixes.put("gigabyte", SizeUnit.GIGABYTES); |
| 60 | + tmpSuffixes.put("gigabytes", SizeUnit.GIGABYTES); |
| 61 | + tmpSuffixes.put("T", SizeUnit.TERABYTES); |
| 62 | + tmpSuffixes.put("TB", SizeUnit.TERABYTES); |
| 63 | + tmpSuffixes.put("Ti", SizeUnit.TERABYTES); |
| 64 | + tmpSuffixes.put("TiB", SizeUnit.TERABYTES); |
| 65 | + tmpSuffixes.put("terabyte", SizeUnit.TERABYTES); |
| 66 | + tmpSuffixes.put("terabytes", SizeUnit.TERABYTES); |
| 67 | + SUFFIXES = Collections.unmodifiableMap(tmpSuffixes); |
| 68 | + } |
| 69 | + |
65 | 70 | private final double count;
|
66 | 71 | private final SizeUnit unit;
|
67 | 72 |
|
@@ -95,7 +100,9 @@ public static Size parse(String size) {
|
95 | 100 | throw new IllegalArgumentException();
|
96 | 101 | }
|
97 | 102 | final Matcher matcher = SIZE_PATTERN.matcher(size);
|
98 |
| - checkArgument(matcher.matches(), "Invalid size: " + size); |
| 103 | + if (!matcher.matches()) { |
| 104 | + throw new IllegalArgumentException("Invalid size: " + size); |
| 105 | + } |
99 | 106 |
|
100 | 107 | final double count = Double.parseDouble(matcher.group(1));
|
101 | 108 | final SizeUnit unit = SUFFIXES.get(matcher.group(2));
|
|
0 commit comments