File tree Expand file tree Collapse file tree
main/java/org/opensearch/dataprepper/model/types
test/java/org/opensearch/dataprepper/model/types Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020 *
2121 * @since 2.1
2222 */
23- public class ByteCount {
23+ public class ByteCount implements Comparable < ByteCount > {
2424 private static final Pattern BYTE_PATTERN = Pattern .compile ("^(?<value>\\ d+\\ .?\\ d*)(?<unit>[a-z]+)?\\ z" );
2525 private static final ByteCount ZERO_BYTES = new ByteCount (0 );
2626 private final long bytes ;
@@ -167,4 +167,9 @@ public boolean equals(final Object otherObject) {
167167 public String toString () {
168168 return bytes + Unit .BYTE .unitString ;
169169 }
170+
171+ @ Override
172+ public int compareTo (final ByteCount otherByteCount ) {
173+ return Long .compare (this .bytes , otherByteCount .bytes );
174+ }
170175}
Original file line number Diff line number Diff line change 1010import org .junit .jupiter .params .provider .CsvSource ;
1111import org .junit .jupiter .params .provider .ValueSource ;
1212
13- import java .util .Random ;
14-
1513import static org .hamcrest .CoreMatchers .not ;
1614import static org .hamcrest .MatcherAssert .assertThat ;
1715import static org .hamcrest .Matchers .containsString ;
2119import static org .junit .jupiter .api .Assertions .assertThrows ;
2220
2321class ByteCountTest {
24- private final Random random = new Random ();
25-
2622 @ ParameterizedTest
2723 @ ValueSource (strings = {
2824 ".1b" ,
@@ -262,4 +258,24 @@ void toString_returns_string_that_parses_to_the_same_value(final long bytes) {
262258 assertThat (parsedByteCount , equalTo (objectUnderTest ));
263259 assertThat (parsedByteCount .hashCode (), equalTo (objectUnderTest .hashCode ()));
264260 }
261+
262+ @ ParameterizedTest
263+ @ CsvSource ({
264+ "0b, 0b, 0" ,
265+ "1b, 1b, 0" ,
266+ "0b, 1b, -1" ,
267+ "1b, 0b, 1" ,
268+ "1kb, 1024b, 0" ,
269+ "512b, 1kb, -1" ,
270+ "2kb, 1kb, 1" ,
271+ "1mb, 1kb, 1" ,
272+ "1kb, 1mb, -1" ,
273+ "1gb, 1mb, 1" ,
274+ "500mb, 1gb, -1"
275+ })
276+ void compareTo_returns_expected_comparison_result (final String firstByteString , final String secondByteString , final int expectedResult ) {
277+ final ByteCount first = ByteCount .parse (firstByteString );
278+ final ByteCount second = ByteCount .parse (secondByteString );
279+ assertThat (first .compareTo (second ), equalTo (expectedResult ));
280+ }
265281}
You can’t perform that action at this time.
0 commit comments