Improve DataSize to support negative values
Issue: SPR-17154
This commit is contained in:
@@ -36,7 +36,7 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
/**
|
||||
* The pattern for parsing.
|
||||
*/
|
||||
private static final Pattern PATTERN = Pattern.compile("^(\\d+)([a-zA-Z]{0,2})$");
|
||||
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$");
|
||||
|
||||
/**
|
||||
* Bytes per KiloByte.
|
||||
@@ -68,7 +68,7 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
|
||||
/**
|
||||
* Obtain a {@link DataSize} representing the specified number of bytes.
|
||||
* @param bytes the number of bytes
|
||||
* @param bytes the number of bytes, positive or negative
|
||||
* @return a {@link DataSize}
|
||||
*/
|
||||
public static DataSize ofBytes(long bytes) {
|
||||
@@ -77,7 +77,7 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
|
||||
/**
|
||||
* Obtain a {@link DataSize} representing the specified number of kilobytes.
|
||||
* @param kiloBytes the number of kilobytes
|
||||
* @param kiloBytes the number of kilobytes, positive or negative
|
||||
* @return a {@link DataSize}
|
||||
*/
|
||||
public static DataSize ofKiloBytes(long kiloBytes) {
|
||||
@@ -86,7 +86,7 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
|
||||
/**
|
||||
* Obtain a {@link DataSize} representing the specified number of megabytes.
|
||||
* @param megaBytes the number of megabytes
|
||||
* @param megaBytes the number of megabytes, positive or negative
|
||||
* @return a {@link DataSize}
|
||||
*/
|
||||
public static DataSize ofMegaBytes(long megaBytes) {
|
||||
@@ -95,7 +95,7 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
|
||||
/**
|
||||
* Obtain a {@link DataSize} representing the specified number of gigabytes.
|
||||
* @param gigaBytes the number of gigabytes
|
||||
* @param gigaBytes the number of gigabytes, positive or negative
|
||||
* @return a {@link DataSize}
|
||||
*/
|
||||
public static DataSize ofGigaBytes(long gigaBytes) {
|
||||
@@ -104,7 +104,7 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
|
||||
/**
|
||||
* Obtain a {@link DataSize} representing the specified number of terabytes.
|
||||
* @param teraBytes the number of terabytes
|
||||
* @param teraBytes the number of terabytes, positive or negative
|
||||
* @return a {@link DataSize}
|
||||
*/
|
||||
public static DataSize ofTeraBytes(long teraBytes) {
|
||||
@@ -113,7 +113,8 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
|
||||
/**
|
||||
* Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}.
|
||||
* @param amount the amount of the size, measured in terms of the unit
|
||||
* @param amount the amount of the size, measured in terms of the unit, positive or
|
||||
* negative
|
||||
* @return a {@link DataSize}
|
||||
*/
|
||||
public static DataSize of(long amount, DataUnit unit) {
|
||||
@@ -177,6 +178,14 @@ public final class DataSize implements Comparable<DataSize> {
|
||||
: defaultUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this size is negative, excluding zero.
|
||||
* @return true if this size has a size less than zero bytes
|
||||
*/
|
||||
public boolean isNegative() {
|
||||
return this.bytes < 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of bytes in this instance.
|
||||
* @return the number of bytes
|
||||
|
||||
@@ -107,46 +107,111 @@ public class DataSizeTests {
|
||||
assertEquals(DataSize.ofKiloBytes(1), DataSize.parse("1024"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNegativeNumberWithDefaultUnitUsesBytes() {
|
||||
assertEquals(DataSize.ofBytes(-1), DataSize.parse("-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNullDefaultUnitUsesBytes() {
|
||||
assertEquals(DataSize.ofKiloBytes(1), DataSize.parse("1024", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNegativeNumberWithNullDefaultUnitUsesBytes() {
|
||||
assertEquals(DataSize.ofKiloBytes(-1), DataSize.parse("-1024", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithCustomDefaultUnit() {
|
||||
assertEquals(DataSize.ofKiloBytes(1), DataSize.parse("1", DataUnit.KILOBYTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNegativeNumberWithCustomDefaultUnit() {
|
||||
assertEquals(DataSize.ofKiloBytes(-1), DataSize.parse("-1", DataUnit.KILOBYTES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithBytes() {
|
||||
assertEquals(DataSize.ofKiloBytes(1), DataSize.parse("1024B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNegativeBytes() {
|
||||
assertEquals(DataSize.ofKiloBytes(-1), DataSize.parse("-1024B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithPostivieBytes() {
|
||||
assertEquals(DataSize.ofKiloBytes(1), DataSize.parse("+1024B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithKiloBytes() {
|
||||
assertEquals(DataSize.ofBytes(1024), DataSize.parse("1KB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNegativeKiloBytes() {
|
||||
assertEquals(DataSize.ofBytes(-1024), DataSize.parse("-1KB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithMegaBytes() {
|
||||
assertEquals(DataSize.ofMegaBytes(4), DataSize.parse("4MB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNegativeMegaBytes() {
|
||||
assertEquals(DataSize.ofMegaBytes(-4), DataSize.parse("-4MB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithGigaBytes() {
|
||||
assertEquals(DataSize.ofMegaBytes(1024), DataSize.parse("1GB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNegativeGigaBytes() {
|
||||
assertEquals(DataSize.ofMegaBytes(-1024), DataSize.parse("-1GB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithTeraBytes() {
|
||||
assertEquals(DataSize.ofTeraBytes(1), DataSize.parse("1TB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithNegativeTeraBytes() {
|
||||
assertEquals(DataSize.ofTeraBytes(-1), DataSize.parse("-1TB"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNegativeWithPositive() {
|
||||
assertFalse(DataSize.ofBytes(50).isNegative());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNegativeWithZero() {
|
||||
assertFalse(DataSize.ofBytes(0).isNegative());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNegativeWithNegative() {
|
||||
assertTrue(DataSize.ofBytes(-1).isNegative());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringUsesBytes() {
|
||||
assertEquals("1024B", DataSize.ofKiloBytes(1).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringWithNegativeBytes() {
|
||||
assertEquals("-1024B", DataSize.ofKiloBytes(-1).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWithUnsupportedUnit() {
|
||||
this.thrown.expect(IllegalArgumentException.class);
|
||||
|
||||
Reference in New Issue
Block a user