From 37b261ba9159671362443a92c0d1f71893bc2386 Mon Sep 17 00:00:00 2001 From: Benjamin Trent Date: Wed, 14 Oct 2020 16:56:18 -0400 Subject: [PATCH] Add helper functions for ByteSizeValues of a given unit (#63679) As a developer that periodically worries about absolute sizes of bytes, I love our good ByteSizeValue abstractions + and the unit sizes. But, I find the pattern `new ByteSizeValue(, ).getBytes()` frustrating. This commit adds some simple convenience methods for each byte unit and constructing a ByteSizeValue of the given size for that unit. --- .../common/unit/ByteSizeValue.java | 24 +++++++++++++ .../common/unit/ByteSizeValueTests.java | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java b/server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java index e2290173cc31..ebb8993e62b1 100644 --- a/server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java +++ b/server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java @@ -46,6 +46,30 @@ static class DeprecationLoggerHolder { public static final ByteSizeValue ZERO = new ByteSizeValue(0, ByteSizeUnit.BYTES); + public static ByteSizeValue ofBytes(long size) { + return new ByteSizeValue(size); + } + + public static ByteSizeValue ofKb(long size) { + return new ByteSizeValue(size, ByteSizeUnit.KB); + } + + public static ByteSizeValue ofMb(long size) { + return new ByteSizeValue(size, ByteSizeUnit.MB); + } + + public static ByteSizeValue ofGb(long size) { + return new ByteSizeValue(size, ByteSizeUnit.GB); + } + + public static ByteSizeValue ofTb(long size) { + return new ByteSizeValue(size, ByteSizeUnit.TB); + } + + public static ByteSizeValue ofPb(long size) { + return new ByteSizeValue(size, ByteSizeUnit.PB); + } + private final long size; private final ByteSizeUnit unit; diff --git a/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java b/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java index 81c0c796ce17..f263892abcaf 100644 --- a/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java +++ b/server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java @@ -25,6 +25,7 @@ import org.hamcrest.MatcherAssert; import java.io.IOException; +import java.util.function.Function; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -325,4 +326,37 @@ public void testGetBytesAsInt() { } } } + + public void testOfBytes() { + testOf(ByteSizeUnit.BYTES, ByteSizeValue::ofBytes); + } + + public void testOfKb() { + testOf(ByteSizeUnit.KB, ByteSizeValue::ofKb); + } + + public void testOfMb() { + testOf(ByteSizeUnit.MB, ByteSizeValue::ofMb); + } + + public void testOfGb() { + testOf(ByteSizeUnit.GB, ByteSizeValue::ofGb); + } + + public void testOfTb() { + testOf(ByteSizeUnit.TB, ByteSizeValue::ofTb); + } + + public void testOfPb() { + testOf(ByteSizeUnit.PB, ByteSizeValue::ofPb); + } + + private void testOf(ByteSizeUnit unit, Function byteSizeValueFunction) { + for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { + long size = randomIntBetween(1, 1000); + ByteSizeValue expected = new ByteSizeValue(size, unit); + ByteSizeValue actual = byteSizeValueFunction.apply(size); + assertThat(actual, equalTo(expected)); + } + } }