Polishing

This commit is contained in:
Juergen Hoeller
2018-08-10 19:23:44 +02:00
parent f931a3fb59
commit 2b2a5a414b
5 changed files with 67 additions and 66 deletions

View File

@@ -25,8 +25,8 @@ import org.springframework.util.StringUtils;
/**
* A data size, such as '12MB'.
* <p>
* This class models a size in terms of bytes and is immutable and thread-safe.
*
* <p>This class models a size in terms of bytes and is immutable and thread-safe.
*
* @author Stephane Nicoll
* @since 5.1
@@ -61,6 +61,7 @@ public final class DataSize implements Comparable<DataSize> {
private final long bytes;
private DataSize(long bytes) {
this.bytes = bytes;
}
@@ -113,13 +114,13 @@ 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, positive or
* negative
* @return a {@link DataSize}
* @param amount the amount of the size, measured in terms of the unit,
* positive or negative
* @return a corresponding {@link DataSize}
*/
public static DataSize of(long amount, DataUnit unit) {
Assert.notNull(unit, "Unit must not be null");
return new DataSize(Math.multiplyExact(amount, unit.getSize().toBytes()));
return new DataSize(Math.multiplyExact(amount, unit.size().toBytes()));
}
/**
@@ -162,20 +163,17 @@ public final class DataSize implements Comparable<DataSize> {
Matcher matcher = PATTERN.matcher(text);
Assert.state(matcher.matches(), "Does not match data size pattern");
DataUnit unit = determineDataUnit(matcher.group(2), defaultUnit);
Long amount = Long.parseLong(matcher.group(1));
long amount = Long.parseLong(matcher.group(1));
return DataSize.of(amount, unit);
}
catch (Exception ex) {
throw new IllegalArgumentException(
"'" + text + "' is not a valid data size", ex);
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
}
}
private static DataUnit determineDataUnit(String suffix,
@Nullable DataUnit defaultUnit) {
defaultUnit = (defaultUnit != null ? defaultUnit : DataUnit.BYTES);
return (StringUtils.hasLength(suffix) ? DataUnit.fromSuffix(suffix)
: defaultUnit);
private static DataUnit determineDataUnit(String suffix, @Nullable DataUnit defaultUnit) {
DataUnit defaultUnitToUse = (defaultUnit != null ? defaultUnit : DataUnit.BYTES);
return (StringUtils.hasLength(suffix) ? DataUnit.fromSuffix(suffix) : defaultUnitToUse);
}
/**
@@ -236,16 +234,17 @@ public final class DataSize implements Comparable<DataSize> {
return String.format("%dB", this.bytes);
}
@Override
public boolean equals(Object o) {
if (this == o) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (other == null || getClass() != other.getClass()) {
return false;
}
DataSize that = (DataSize) o;
return this.bytes == that.bytes;
DataSize otherSize = (DataSize) other;
return (this.bytes == otherSize.bytes);
}
@Override
@@ -254,4 +253,3 @@ public final class DataSize implements Comparable<DataSize> {
}
}

View File

@@ -32,35 +32,37 @@ public enum DataUnit {
BYTES("B", DataSize.ofBytes(1)),
/**
* KiloByte.
* KiloBytes.
*/
KILOBYTES("KB", DataSize.ofKiloBytes(1)),
/**
* MegaByte.
* MegaBytes.
*/
MEGABYTES("MB", DataSize.ofMegaBytes(1)),
/**
* TeraByte.
* GigaBytes.
*/
GIGABYTES("GB", DataSize.ofGigaBytes(1)),
/**
* TeraByte.
* TeraBytes.
*/
TERABYTES("TB", DataSize.ofTeraBytes(1));
private final String suffix;
private final DataSize size;
DataUnit(String suffix, DataSize size) {
this.suffix = suffix;
this.size = size;
}
protected DataSize getSize() {
DataSize size() {
return this.size;
}
@@ -68,7 +70,8 @@ public enum DataUnit {
* Return the {@link DataUnit} matching the specified {@code suffix}.
* @param suffix one of the standard suffix
* @return the {@link DataUnit} matching the specified {@code suffix}
* @throws IllegalArgumentException if the suffix does not match any instance
* @throws IllegalArgumentException if the suffix does not match any
* of this enum's constants
*/
public static DataUnit fromSuffix(String suffix) {
for (DataUnit candidate : values()) {