Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify afterPropertiesSet() Method in RedisTemplate Using Objects.requireNonNullElse #3003

Open
kjb512 opened this issue Sep 23, 2024 · 0 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@kjb512
Copy link

kjb512 commented Sep 23, 2024

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;
}
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 23, 2024
@kjb512 kjb512 changed the title Simplify afterPropertiesSet() Method in 'RedisTemplate' Using Objects.requireNonNullElse Simplify afterPropertiesSet() Method in RedisTemplate Using Objects.requireNonNullElse Sep 23, 2024
@mp911de mp911de added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
3 participants