Add requireState(..) assertion method to RedisAssertions.

Use RedisAssertions.requireState(..) in RedisAccessor.getRequiredConnectionFactory().

Closes #2611
This commit is contained in:
John Blum
2023-06-14 14:13:19 -07:00
parent f3de2d51dc
commit 2b2e050004
4 changed files with 155 additions and 9 deletions

View File

@@ -19,9 +19,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.lang.NonNull;
import org.springframework.data.redis.util.RedisAssertions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Base class for {@link RedisTemplate} defining common properties. Not intended to be used directly.
@@ -62,14 +61,8 @@ public class RedisAccessor implements InitializingBean {
* @see #getConnectionFactory()
* @since 2.0
*/
@NonNull
public RedisConnectionFactory getRequiredConnectionFactory() {
RedisConnectionFactory connectionFactory = getConnectionFactory();
Assert.state(connectionFactory != null, "RedisConnectionFactory is required");
return connectionFactory;
return RedisAssertions.requireState(getConnectionFactory(), "RedisConnectionFactory is required");
}
/**

View File

@@ -56,4 +56,33 @@ public abstract class RedisAssertions {
Assert.notNull(target, message);
return target;
}
/**
* Asserts the given {@link Object} is not {@literal null}.
*
* @param <T> {@link Class type} of {@link Object} being asserted.
* @param target {@link Object} to evaluate.
* @param message {@link String} containing the message for the thrown exception.
* @param arguments array of {@link Object} arguments used to format the {@link String message}.
* @return the given {@link Object}.
* @throws IllegalArgumentException if the {@link Object target} is {@literal null}.
* @see #requireObject(Object, Supplier)
*/
public static <T> T requireState(@Nullable T target, String message, Object... arguments) {
return requireState(target, () -> String.format(message, arguments));
}
/**
* Asserts the given {@link Object} is not {@literal null}.
*
* @param <T> {@link Class type} of {@link Object} being asserted.
* @param target {@link Object} to evaluate.
* @param message {@link Supplier} supplying the message for the thrown exception.
* @return the given {@link Object}.
* @throws IllegalArgumentException if the {@link Object target} is {@literal null}.
*/
public static <T> T requireState(@Nullable T target, Supplier<String> message) {
Assert.state(target != null, message);
return target;
}
}