Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @since 2.1
*/
public class ByteCount {
public class ByteCount implements Comparable<ByteCount> {
private static final Pattern BYTE_PATTERN = Pattern.compile("^(?<value>\\d+\\.?\\d*)(?<unit>[a-z]+)?\\z");
private static final ByteCount ZERO_BYTES = new ByteCount(0);
private final long bytes;
Expand Down Expand Up @@ -167,4 +167,9 @@ public boolean equals(final Object otherObject) {
public String toString() {
return bytes + Unit.BYTE.unitString;
}

@Override
public int compareTo(final ByteCount otherByteCount) {
return Long.compare(this.bytes, otherByteCount.bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Random;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -21,8 +19,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

class ByteCountTest {
private final Random random = new Random();

@ParameterizedTest
@ValueSource(strings = {
".1b",
Expand Down Expand Up @@ -262,4 +258,24 @@ void toString_returns_string_that_parses_to_the_same_value(final long bytes) {
assertThat(parsedByteCount, equalTo(objectUnderTest));
assertThat(parsedByteCount.hashCode(), equalTo(objectUnderTest.hashCode()));
}

@ParameterizedTest
@CsvSource({
"0b, 0b, 0",
"1b, 1b, 0",
"0b, 1b, -1",
"1b, 0b, 1",
"1kb, 1024b, 0",
"512b, 1kb, -1",
"2kb, 1kb, 1",
"1mb, 1kb, 1",
"1kb, 1mb, -1",
"1gb, 1mb, 1",
"500mb, 1gb, -1"
})
void compareTo_returns_expected_comparison_result(final String firstByteString, final String secondByteString, final int expectedResult) {
final ByteCount first = ByteCount.parse(firstByteString);
final ByteCount second = ByteCount.parse(secondByteString);
assertThat(first.compareTo(second), equalTo(expectedResult));
}
}
Loading