File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed
main/java/cd/go/contrib/elasticagent/utils
test/java/cd/go/contrib/elasticagent/utils Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change 27
27
import static java .util .Objects .requireNonNull ;
28
28
29
29
public class Size implements Comparable <Size > {
30
- private static final Pattern SIZE_PATTERN = Pattern .compile ("(\\ d+)\\ s*(\\ S+ )" );
30
+ private static final Pattern SIZE_PATTERN = Pattern .compile ("(\\ d+)\\ s*(\\ S* )" );
31
31
32
32
private static final Map <String , SizeUnit > SUFFIXES ;
33
33
@@ -101,10 +101,11 @@ public static Size parse(String size) {
101
101
}
102
102
103
103
final double count = Double .parseDouble (matcher .group (1 ));
104
- final SizeUnit unit = SUFFIXES . get ( matcher .group (2 ) );
105
- if (unit == null ) {
104
+ String suffix = matcher .group (2 );
105
+ if (! suffix . isBlank () && ! SUFFIXES . containsKey ( suffix ) ) {
106
106
throw new IllegalArgumentException ("Invalid size: " + size + ". Wrong size unit" );
107
107
}
108
+ final SizeUnit unit = SUFFIXES .getOrDefault (suffix , SizeUnit .BYTES );
108
109
109
110
return new Size (count , unit );
110
111
}
Original file line number Diff line number Diff line change @@ -37,11 +37,6 @@ public void shouldThrowExceptionIfTheGivenSizeHasDifferentSuffix() {
37
37
assertThrows (IllegalArgumentException .class , () -> Size .parse ("24000kig" ));
38
38
}
39
39
40
- @ Test
41
- public void shouldThrowExceptionIfTheGivenSizeHasNoUint () {
42
- assertThrows (IllegalArgumentException .class , () -> Size .parse ("24000" ));
43
- }
44
-
45
40
@ Test
46
41
public void shouldThrowExceptionIfTheGivenSizeIsNull () {
47
42
assertThrows (IllegalArgumentException .class , () -> Size .parse (null ));
@@ -57,6 +52,12 @@ public void fromQuantity() {
57
52
assertEquals ("58 GB" , Size .fromQuantity (Quantity .parse ("58Gi" )).readableSize ());
58
53
}
59
54
55
+ @ Test
56
+ public void defaultSizeUnitIsBytes () {
57
+ Size oneKB = Size .parse ("1024" );
58
+ assertEquals (oneKB .toBytes (), 1024 , 0 );
59
+ }
60
+
60
61
@ Test
61
62
public void toBytes () {
62
63
Size oneKB = Size .parse ("1Ki" );
You can’t perform that action at this time.
0 commit comments