Skip to content

Commit

Permalink
ByteString -> ByteCount (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisjoe authored and markelliot committed Jan 21, 2019
1 parent 7aef84a commit a83b96a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ human-readable-types
--------------------

The following types are currently provided:
* [HumanReadableByteString](human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteString.java)
* [HumanReadableByteCount](human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableByteCount.java)
* [HumanReadableDuration](human-readable-types/src/main/java/com/palantir/humanreadabletypes/HumanReadableDuration.java)

### Example Usage
Expand All @@ -36,7 +36,7 @@ package com.palantir.example;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.palantir.humanreadabletypes.HumanReadableByteString;
import com.palantir.humanreadabletypes.HumanReadableByteCount;
import com.palantir.humanreadabletypes.HumanReadableDuration;
import org.immutables.value.Value;

Expand All @@ -48,7 +48,7 @@ public abstract class ExampleConfiguration {
public abstract HumanReadableDuration getMaximumConnectTimeout();

@JsonProperty("maximum-file-size")
public abstract HumanReadableByteString getMaximumFileSize();
public abstract HumanReadableByteCount getMaximumFileSize();
}

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
* A human-readable type for binary quantities.
* <p>
* This class allows for parsing strings representing binary quantities into usable byte quantities. Strings should
* match {@link HumanReadableByteString#BYTE_STRING_PATTERN} which represents the numeric binary quantity with a suffix
* match {@link HumanReadableByteCount#BYTE_COUNT_PATTERN} which represents the numeric binary quantity with a suffix
* representing the {@link ByteUnit} to use for this byte string. Suffixes may be pluralized or not regardless of the
* actual numeric quantity.
* <p>
* All {@code equal}, {@code compareTo} and {@code hashCode} implementations assume normalized values, i.e. they work
* off of the total number of bytes. Therefore, {@code 1024 bytes} would be equivalent to {@code 1 kibibyte}.
*/
public final class HumanReadableByteString implements Comparable<HumanReadableByteString>, Serializable {
public final class HumanReadableByteCount implements Comparable<HumanReadableByteCount>, Serializable {
/**
* Serialization version.
*/
Expand All @@ -44,7 +44,7 @@ public final class HumanReadableByteString implements Comparable<HumanReadableBy
/**
* The pattern for parsing byte strings.
*/
private static final Pattern BYTE_STRING_PATTERN = Pattern.compile("([0-9]+)\\s?([a-rt-z]+)?s?");
private static final Pattern BYTE_COUNT_PATTERN = Pattern.compile("([0-9]+)\\s?([a-rt-z]+)?s?");

private static final Map<String, ByteUnit> SUFFIXES = createSuffixes();

Expand Down Expand Up @@ -74,84 +74,84 @@ private static Map<String, ByteUnit> createSuffixes() {
private final ByteUnit unit;

/**
* Obtains a new {@link HumanReadableByteString} using {@link ByteUnit#BYTE}.
* Obtains a new {@link HumanReadableByteCount} using {@link ByteUnit#BYTE}.
*
* @param size the number of bytes
*/
public static HumanReadableByteString bytes(long size) {
return new HumanReadableByteString(size, ByteUnit.BYTE);
public static HumanReadableByteCount bytes(long size) {
return new HumanReadableByteCount(size, ByteUnit.BYTE);
}

/**
* Obtains a new {@link HumanReadableByteString} using {@link ByteUnit#KiB}.
* Obtains a new {@link HumanReadableByteCount} using {@link ByteUnit#KiB}.
*
* @param size the number of kibibytes
*/
public static HumanReadableByteString kibibytes(long size) {
return new HumanReadableByteString(size, ByteUnit.KiB);
public static HumanReadableByteCount kibibytes(long size) {
return new HumanReadableByteCount(size, ByteUnit.KiB);
}

/**
* Obtains a new {@link HumanReadableByteString} using {@link ByteUnit#MiB}.
* Obtains a new {@link HumanReadableByteCount} using {@link ByteUnit#MiB}.
*
* @param size the number of mebibytes
*/
public static HumanReadableByteString mebibytes(long size) {
return new HumanReadableByteString(size, ByteUnit.MiB);
public static HumanReadableByteCount mebibytes(long size) {
return new HumanReadableByteCount(size, ByteUnit.MiB);
}

/**
* Obtains a new {@link HumanReadableByteString} using {@link ByteUnit#GiB}.
* Obtains a new {@link HumanReadableByteCount} using {@link ByteUnit#GiB}.
*
* @param size the number of gibibytes
*/
public static HumanReadableByteString gibibytes(long size) {
return new HumanReadableByteString(size, ByteUnit.GiB);
public static HumanReadableByteCount gibibytes(long size) {
return new HumanReadableByteCount(size, ByteUnit.GiB);
}

/**
* Obtains a new {@link HumanReadableByteString} using {@link ByteUnit#TiB}.
* Obtains a new {@link HumanReadableByteCount} using {@link ByteUnit#TiB}.
*
* @param size the number of tebibytes
*/
public static HumanReadableByteString tebibytes(long size) {
return new HumanReadableByteString(size, ByteUnit.TiB);
public static HumanReadableByteCount tebibytes(long size) {
return new HumanReadableByteCount(size, ByteUnit.TiB);
}

/**
* Obtains a new {@link HumanReadableByteString} using {@link ByteUnit#PiB}.
* Obtains a new {@link HumanReadableByteCount} using {@link ByteUnit#PiB}.
*
* @param size the number of pebibytes
*/
public static HumanReadableByteString pebibytes(long size) {
return new HumanReadableByteString(size, ByteUnit.PiB);
public static HumanReadableByteCount pebibytes(long size) {
return new HumanReadableByteCount(size, ByteUnit.PiB);
}

/**
* Constructs a new {@link HumanReadableByteString} from the provided string representation.
* Constructs a new {@link HumanReadableByteCount} from the provided string representation.
*
* @param byteString the string representation of this byte string
* @return the parsed {@link HumanReadableByteString}
* @param byteCount the string representation of this byte string
* @return the parsed {@link HumanReadableByteCount}
* @throws IllegalArgumentException if the provided byte string is invalid
* @throws NumberFormatException if the provided size cannot be parsed
*/
@JsonCreator
public static HumanReadableByteString valueOf(String byteString) {
String lower = byteString.toLowerCase(Locale.ROOT).trim();
public static HumanReadableByteCount valueOf(String byteCount) {
String lower = byteCount.toLowerCase(Locale.ROOT).trim();

try {
Matcher matcher = BYTE_STRING_PATTERN.matcher(lower);
Matcher matcher = BYTE_COUNT_PATTERN.matcher(lower);

Preconditions.checkArgument(matcher.matches(), "Invalid byte string: %s", byteString);
Preconditions.checkArgument(matcher.matches(), "Invalid byte string: %s", byteCount);

long size = Long.parseLong(matcher.group(1));
String suffix = matcher.group(2);

if (suffix != null && !SUFFIXES.containsKey(suffix)) {
throw new IllegalArgumentException("Invalid byte string: " + byteString + ". Wrong byte unit");
throw new IllegalArgumentException("Invalid byte string: " + byteCount + ". Wrong byte unit");
}

return new HumanReadableByteString(size, suffix != null ? SUFFIXES.get(suffix) : ByteUnit.BYTE);
return new HumanReadableByteCount(size, suffix != null ? SUFFIXES.get(suffix) : ByteUnit.BYTE);

} catch (NumberFormatException e) {
String byteError = "Size must be specified as bytes (b), "
Expand All @@ -162,13 +162,13 @@ public static HumanReadableByteString valueOf(String byteString) {
}
}

private HumanReadableByteString(long size, ByteUnit unit) {
private HumanReadableByteCount(long size, ByteUnit unit) {
this.size = size;
this.unit = Preconditions.checkNotNull(unit, "unit must not be null");
}

/**
* The size of this byte string in {@link HumanReadableByteString.ByteUnit binary units}.
* The size of this byte string in {@link HumanReadableByteCount.ByteUnit binary units}.
*/
public long getSize() {
return size;
Expand All @@ -189,39 +189,39 @@ public long toBytes() {
}

/**
* Compares this byte string to the specified {@code HumanReadableByteString}.
* Compares this byte string to the specified {@code HumanReadableByteCount}.
* <p>
* The comparison is based on the total number of bytes.
* It is "consistent with equals", as defined by {@link Comparable}.
*
* @param otherByteString the other byte string to compare to, not null
* @param otherByteCount the other byte string to compare to, not null
* @return the comparator value, negative if less, positive if greater
*/
@Override
public int compareTo(HumanReadableByteString otherByteString) {
if (unit == otherByteString.unit) {
return Long.compare(size, otherByteString.size);
public int compareTo(HumanReadableByteCount otherByteCount) {
if (unit == otherByteCount.unit) {
return Long.compare(size, otherByteCount.size);
}
return Long.compare(toBytes(), otherByteString.toBytes());
return Long.compare(toBytes(), otherByteCount.toBytes());
}

/**
* Checks if this byte string is equal to the specified {@code HumanReadableByteString}.
* Checks if this byte string is equal to the specified {@code HumanReadableByteCount}.
* <p>
* The comparison is based on the total number of bytes.
*
* @param otherByteString the other byte string, null returns false
* @param otherByteCount the other byte string, null returns false
* @return true if the other byte string is equal to this one
*/
@Override
public boolean equals(Object otherByteString) {
if (this == otherByteString) {
public boolean equals(Object otherByteCount) {
if (this == otherByteCount) {
return true;
}
if ((otherByteString == null) || (getClass() != otherByteString.getClass())) {
if ((otherByteCount == null) || (getClass() != otherByteCount.getClass())) {
return false;
}
final HumanReadableByteString other = (HumanReadableByteString) otherByteString;
final HumanReadableByteCount other = (HumanReadableByteCount) otherByteCount;
if (unit == other.unit) {
return size == other.size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static HumanReadableDuration days(long count) {
}

/**
* Constructs a new {@link HumanReadableByteString} from the provided string representation.
* Constructs a new {@link HumanReadableByteCount} from the provided string representation.
*
* @param duration the string HumanReadableDuration of this duration
* @return the parsed {@link HumanReadableDuration}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.stream.Collectors;
import org.junit.Test;

public final class HumanReadableByteStringTests {
public final class HumanReadableByteCountTests {

@Test
public void testParseByte() {
Expand Down Expand Up @@ -58,46 +58,46 @@ public void testParsePebiBytes() {

@Test
public void testInvalidString() {
assertThatThrownBy(() -> HumanReadableByteString.valueOf("Ten bytes"))
assertThatThrownBy(() -> HumanReadableByteCount.valueOf("Ten bytes"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid byte string: Ten bytes");
}

@Test
public void testInvalidUnits() {
assertThatThrownBy(() -> HumanReadableByteString.valueOf("10 kilobytes"))
assertThatThrownBy(() -> HumanReadableByteCount.valueOf("10 kilobytes"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid byte string: 10 kilobytes. Wrong byte unit");
}

@Test
public void testEquals() {
assertThat(HumanReadableByteString.bytes(1024).equals(HumanReadableByteString.kibibytes(1))).isTrue();
assertThat(HumanReadableByteString.bytes(1).equals(HumanReadableByteString.valueOf("1"))).isTrue();
assertThat(HumanReadableByteString.mebibytes(1024).equals(HumanReadableByteString.gibibytes(1))).isTrue();
assertThat(HumanReadableByteString.tebibytes(1024).equals(HumanReadableByteString.pebibytes(1))).isTrue();
assertThat(HumanReadableByteString.bytes(1024).equals(HumanReadableByteString.mebibytes(1))).isFalse();
assertThat(HumanReadableByteCount.bytes(1024).equals(HumanReadableByteCount.kibibytes(1))).isTrue();
assertThat(HumanReadableByteCount.bytes(1).equals(HumanReadableByteCount.valueOf("1"))).isTrue();
assertThat(HumanReadableByteCount.mebibytes(1024).equals(HumanReadableByteCount.gibibytes(1))).isTrue();
assertThat(HumanReadableByteCount.tebibytes(1024).equals(HumanReadableByteCount.pebibytes(1))).isTrue();
assertThat(HumanReadableByteCount.bytes(1024).equals(HumanReadableByteCount.mebibytes(1))).isFalse();
}

@Test
public void testCompareTo() {
assertThat(HumanReadableByteString.bytes(2048).compareTo(HumanReadableByteString.kibibytes(1))).isEqualTo(1);
assertThat(HumanReadableByteString.bytes(1024).compareTo(HumanReadableByteString.kibibytes(1))).isEqualTo(0);
assertThat(HumanReadableByteString.mebibytes(1).compareTo(HumanReadableByteString.gibibytes(1))).isEqualTo(-1);
assertThat(HumanReadableByteCount.bytes(2048).compareTo(HumanReadableByteCount.kibibytes(1))).isEqualTo(1);
assertThat(HumanReadableByteCount.bytes(1024).compareTo(HumanReadableByteCount.kibibytes(1))).isEqualTo(0);
assertThat(HumanReadableByteCount.mebibytes(1).compareTo(HumanReadableByteCount.gibibytes(1))).isEqualTo(-1);
}

@Test
public void testToString() {
assertThat(HumanReadableByteString.valueOf("1 byte").toString()).isEqualTo("1 byte");
assertThat(HumanReadableByteString.valueOf("1 bytes").toString()).isEqualTo("1 byte");
assertThat(HumanReadableByteString.valueOf("2 byte").toString()).isEqualTo("2 bytes");
assertThat(HumanReadableByteString.valueOf("2 bytes").toString()).isEqualTo("2 bytes");
assertThat(HumanReadableByteCount.valueOf("1 byte").toString()).isEqualTo("1 byte");
assertThat(HumanReadableByteCount.valueOf("1 bytes").toString()).isEqualTo("1 byte");
assertThat(HumanReadableByteCount.valueOf("2 byte").toString()).isEqualTo("2 bytes");
assertThat(HumanReadableByteCount.valueOf("2 bytes").toString()).isEqualTo("2 bytes");
}

private static void assetStringsEqualToBytes(long expectedBytes, String... byteStrings) {
assertThat(Arrays.stream(byteStrings)
.map(HumanReadableByteString::valueOf)
.map(HumanReadableByteString::toBytes)
private static void assetStringsEqualToBytes(long expectedBytes, String... byteCounts) {
assertThat(Arrays.stream(byteCounts)
.map(HumanReadableByteCount::valueOf)
.map(HumanReadableByteCount::toBytes)
.collect(Collectors.toList())
).allMatch(Predicate.isEqual(expectedBytes));
}
Expand Down

0 comments on commit a83b96a

Please sign in to comment.