spring-projects / spring-projects/spring-data-redis
Simplify afterPropertiesSet() Method in `RedisTemplate` Using Objects.requireNonNullElse
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.9k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
The afterPropertiesSet() method currently contains multiple null checks and assignments that can be simplified. By utilizing Java 9’s Objects.requireNonNullElse and Objects.requireNonNullElseGet methods, we can make the code more concise, readable, and maintainable.
AS-IS
public void afterPropertiesSet() {
super.afterPropertiesSet();
if (defaultSerializer == null) {
defaultSerializer = new JdkSerializationRedisSerializer(
classLoader != null ? classLoader : this.getClass().getClassLoader());
}
if (enableDefaultSerializer) {
if (keySerializer == null) {
keySerializer = defaultSerializer;
}
if (valueSerializer == null) {
valueSerializer = defaultSerializer;
}
if (hashKeySerializer == null) {
hashKeySerializer = defaultSerializer;
}
if (hashValueSerializer == null) {
hashValueSerializer = defaultSerializer;
}
}
if (scriptExecutor == null) {
this.scriptExecutor = new DefaultScriptExecutor<>(this);
}
initialized = true;
}
TO-BE
public void afterPropertiesSet() {
super.afterPropertiesSet();
defaultSerializer = Objects.requireNonNullElseGet(
defaultSerializer,
() -> new JdkSerializationRedisSerializer(
Objects.requireNonNullElse(classLoader, getClass().getClassLoader()))
);
if (enableDefaultSerializer) {
keySerializer = Objects.requireNonNullElse(keySerializer, defaultSerializer);
valueSerializer = Objects.requireNonNullElse(valueSerializer, defaultSerializer);
hashKeySerializer = Objects.requireNonNullElse(hashKeySerializer, defaultSerializer);
hashValueSerializer = Objects.requireNonNullElse(hashValueSerializer, defaultSerializer);
}
scriptExecutor = Objects.requireNonNullElseGet(
scriptExecutor,
() -> new DefaultScriptExecutor<>(this)
);
initialized = true;
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at RedisTemplate.afterPropertiesSet() and compare its current null-handling branches with the proposed Objects-based form. Check the surrounding imports and relevant existing tests, then verify that initialization and serializer defaults remain unchanged while the method becomes more concise.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, redis
- Domain
- databases
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100