Skip to content

Commit

Permalink
using the new methods in the other projects
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Atanasov <[email protected]>
  • Loading branch information
ata-nas committed Nov 11, 2024
1 parent d0b8434 commit cacfb7e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.hedera.block.common.utils;

import java.util.Objects;

/** A utility class used to assert various preconditions. */
public final class Preconditions {
/**
Expand All @@ -32,8 +34,29 @@ public final class Preconditions {
* checked is blank
*/
public static String requireNotBlank(final String toCheck) {
return requireNotBlank(toCheck, null);
}

/**
* This method asserts a given {@link String} is not blank, meaning it is
* not {@code null} or does not contain only whitespaces as defined by
* {@link String#isBlank()}. If the given {@link String} is not blank, then
* we return it, else we throw {@link IllegalArgumentException}.
*
* @param toCheck a {@link String} to be checked if is blank as defined
* above
* @param errorMessage the error message to be used in the exception if the
* input {@link String} to be checked is blank, if null, a default message
* @return the {@link String} to be checked if it is not blank as defined
* above
* @throws IllegalArgumentException if the input {@link String} to be
* checked is blank
*/
public static String requireNotBlank(final String toCheck, final String errorMessage) {
if (StringUtilities.isBlank(toCheck)) {
throw new IllegalArgumentException("A String required to be non-blank is blank.");
final String message =
Objects.isNull(errorMessage) ? "The input String is required to be non-blank." : errorMessage;
throw new IllegalArgumentException(message);
} else {
return toCheck;
}
Expand All @@ -49,8 +72,27 @@ public static String requireNotBlank(final String toCheck) {
* positive
*/
public static int requirePositive(final int toCheck) {
return requirePositive(toCheck, null);
}

/**
* This method asserts a given integer is a positive. An integer is positive
* if it is NOT equal to zero and is greater than zero.
*
* @param toCheck the integer to check if it is a positive power of two
* @param errorMessage the error message to be used in the exception if the
* input integer to check is not positive, if null, a default message will
* be used
* @return the number to check if it is positive
* @throws IllegalArgumentException if the input number to check is not
* positive
*/
public static int requirePositive(final int toCheck, final String errorMessage) {
if (0 >= toCheck) {
throw new IllegalArgumentException("The input integer [%d] is required be positive.".formatted(toCheck));
final String message = Objects.isNull(errorMessage)
? "The input integer [%d] is required be positive.".formatted(toCheck)
: errorMessage;
throw new IllegalArgumentException(message);
} else {
return toCheck;
}
Expand All @@ -65,9 +107,26 @@ public static int requirePositive(final int toCheck) {
* power of two
*/
public static int requirePowerOfTwo(final int toCheck) {
return requirePowerOfTwo(toCheck, null);
}

/**
* This method asserts a given integer is a power of two.
*
* @param toCheck the number to check if it is a power of two
* @param errorMessage the error message to be used in the exception if the
* input integer to check is not a power of two, if null, a default message
* will be used
* @return the number to check if it is a power of two
* @throws IllegalArgumentException if the input number to check is not a
* power of two
*/
public static int requirePowerOfTwo(final int toCheck, final String errorMessage) {
if (!MathUtilities.isPowerOfTwo(toCheck)) {
throw new IllegalArgumentException(
"The input integer [%d] is required to be a power of two.".formatted(toCheck));
final String message = Objects.isNull(errorMessage)
? "The input integer [%d] is required to be a power of two.".formatted(toCheck)
: errorMessage;
throw new IllegalArgumentException(message);
} else {
return toCheck;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import java.util.function.Consumer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand All @@ -29,75 +30,108 @@ class PreconditionsTest {
/**
* This test aims to verify that the
* {@link Preconditions#requireNotBlank(String)} will return the input
* 'toTest' parameter if the non-blank check passes.
* 'toTest' parameter if the non-blank check passes. Test includes
* overloads.
*
* @param toTest parameterized, the string to test
*/
@ParameterizedTest
@MethodSource("com.hedera.block.common.CommonsTestUtility#nonBlankStrings")
void testRequireNotBlankPass(final String toTest) {
final Consumer<String> asserts =
actual -> assertThat(actual).isNotNull().isNotBlank().isEqualTo(toTest);

final String actual = Preconditions.requireNotBlank(toTest);
assertThat(actual).isNotNull().isNotBlank().isEqualTo(toTest);
assertThat(actual).satisfies(asserts);

final String actualOverload = Preconditions.requireNotBlank(toTest, "test error message");
assertThat(actualOverload).satisfies(asserts);
}

/**
* This test aims to verify that the
* {@link Preconditions#requireNotBlank(String)} will throw an
* {@link IllegalArgumentException} if the non-blank check fails.
* {@link IllegalArgumentException} if the non-blank check fails. Test
* includes overloads.
*
* @param toTest parameterized, the string to test
*/
@ParameterizedTest
@MethodSource("com.hedera.block.common.CommonsTestUtility#blankStrings")
void testRequireNotBlankFail(final String toTest) {
assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requireNotBlank(toTest));

final String testErrorMessage = "test error message";
assertThatIllegalArgumentException()
.isThrownBy(() -> Preconditions.requireNotBlank(toTest, testErrorMessage))
.withMessage(testErrorMessage);
}

/**
* This test aims to verify that the
* {@link Preconditions#requirePositive(int)} will return the input 'toTest'
* parameter if the positive check passes.
* parameter if the positive check passes. Test includes overloads.
*
* @param toTest parameterized, the number to test
*/
@ParameterizedTest
@MethodSource("com.hedera.block.common.CommonsTestUtility#positiveIntegers")
void testRequirePositivePass(final int toTest) {
final Consumer<Integer> asserts =
actual -> assertThat(actual).isPositive().isEqualTo(toTest);

final int actual = Preconditions.requirePositive(toTest);
assertThat(actual).isPositive().isEqualTo(toTest);
assertThat(actual).satisfies(asserts);

final int actualOverload = Preconditions.requirePositive(toTest, "test error message");
assertThat(actualOverload).satisfies(asserts);
}

/**
* This test aims to verify that the
* {@link Preconditions#requirePositive(int)} will throw an
* {@link IllegalArgumentException} if the positive check fails.
* {@link IllegalArgumentException} if the positive check fails. Test
* includes overloads.
*
* @param toTest parameterized, the number to test
*/
@ParameterizedTest
@MethodSource("com.hedera.block.common.CommonsTestUtility#zeroAndNegativeIntegers")
void testRequirePositiveFail(final int toTest) {
assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePositive(toTest));

final String testErrorMessage = "test error message";
assertThatIllegalArgumentException()
.isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage))
.withMessage(testErrorMessage);
}

/**
* This test aims to verify that the
* {@link Preconditions#requirePowerOfTwo(int)} will return the input
* 'toTest' parameter if the power of two check passes.
* 'toTest' parameter if the power of two check passes. Test includes
* overloads.
*
* @param toTest parameterized, the number to test
*/
@ParameterizedTest
@MethodSource("com.hedera.block.common.CommonsTestUtility#powerOfTwoIntegers")
void testRequirePowerOfTwoPass(final int toTest) {
final Consumer<Integer> asserts =
actual -> assertThat(actual).isPositive().isEqualTo(toTest);

final int actual = Preconditions.requirePowerOfTwo(toTest);
assertThat(actual).isPositive().isEqualTo(toTest);
assertThat(actual).satisfies(asserts);

final int actualOverload = Preconditions.requirePowerOfTwo(toTest, "test error message");
assertThat(actualOverload).satisfies(asserts);
}

/**
* This test aims to verify that the
* {@link Preconditions#requirePowerOfTwo(int)} will throw an
* {@link IllegalArgumentException} if the power of two check fails.
* {@link IllegalArgumentException} if the power of two check fails. Test
* includes overloads.
*
* @param toTest parameterized, the number to test
*/
Expand All @@ -108,5 +142,10 @@ void testRequirePowerOfTwoPass(final int toTest) {
})
void testRequirePowerOfTwoFail(final int toTest) {
assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest));

final String testErrorMessage = "test error message";
assertThatIllegalArgumentException()
.isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest, testErrorMessage))
.withMessage(testErrorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.lang.System.Logger.Level.INFO;

import com.hedera.block.common.utils.Preconditions;
import com.swirlds.config.api.ConfigData;
import com.swirlds.config.api.ConfigProperty;

Expand All @@ -43,14 +44,8 @@ public record MediatorConfig(
* @throws IllegalArgumentException if the configuration is invalid
*/
public MediatorConfig {
if (ringBufferSize <= 0) {
throw new IllegalArgumentException("Ring buffer size must be greater than 0");
}

if ((ringBufferSize & (ringBufferSize - 1)) != 0) {
throw new IllegalArgumentException("Ring buffer size must be a power of 2");
}

Preconditions.requirePositive(ringBufferSize, "Mediator Ring Buffer Size must be positive!");
Preconditions.requirePowerOfTwo(ringBufferSize, "Mediator Ring Buffer Size must be a power of 2!");
LOGGER.log(INFO, "Mediator configuration mediator.ringBufferSize: " + ringBufferSize);
LOGGER.log(INFO, "Mediator configuration mediator.type: " + type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.lang.System.Logger.Level.INFO;

import com.hedera.block.common.utils.Preconditions;
import com.swirlds.config.api.ConfigData;
import com.swirlds.config.api.ConfigProperty;

Expand All @@ -38,14 +39,8 @@ public record NotifierConfig(@ConfigProperty(defaultValue = "1024") int ringBuff
* @throws IllegalArgumentException if the configuration is invalid
*/
public NotifierConfig {
if (ringBufferSize <= 0) {
throw new IllegalArgumentException("Ring buffer size must be greater than 0");
}

if ((ringBufferSize & (ringBufferSize - 1)) != 0) {
throw new IllegalArgumentException("Ring buffer size must be a power of 2");
}

Preconditions.requirePositive(ringBufferSize, "Notifier Ring Buffer Size must be positive!");
Preconditions.requirePowerOfTwo(ringBufferSize, "Notifier Ring Buffer Size must be a power of 2!");
LOGGER.log(INFO, "Notifier configuration notifier.ringBufferSize: " + ringBufferSize);
}
}

0 comments on commit cacfb7e

Please sign in to comment.