Skip to content

Remove unnecessary usages of Optional. #3203

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.springframework.data.redis.cache;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisKeyCommands;
Expand All @@ -34,6 +33,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author John Blum
* @author Yong-Hyun Kim
* @since 2.6
*/
public abstract class BatchStrategies {
Expand Down Expand Up @@ -82,14 +82,15 @@ public long cleanCache(RedisConnection connection, String name, byte[] pattern)

RedisKeyCommands commands = connection.keyCommands();

byte[][] keys = Optional.ofNullable(commands.keys(pattern)).orElse(Collections.emptySet())
.toArray(new byte[0][]);
Set<byte[]> keys = commands.keys(pattern);

if (keys.length > 0) {
commands.del(keys);
}
if (keys == null || keys.isEmpty()) {
return 0;
}

commands.del(keys.toArray(new byte[0][]));

return keys.length;
return keys.size();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Yong-Hyun Kim
* @since 2.0
*/
public class RedisPassword {
Expand All @@ -55,10 +56,11 @@ private RedisPassword(char[] thePassword) {
*/
public static RedisPassword of(@Nullable String passwordAsString) {

return Optional.ofNullable(passwordAsString) //
.filter(StringUtils::hasText) //
.map(it -> new RedisPassword(it.toCharArray())) //
.orElseGet(RedisPassword::none);
if (!StringUtils.hasText(passwordAsString)) {
return none();
}

return new RedisPassword(passwordAsString.toCharArray());
}

/**
Expand All @@ -69,10 +71,11 @@ public static RedisPassword of(@Nullable String passwordAsString) {
*/
public static RedisPassword of(char @Nullable[] passwordAsChars) {

return Optional.ofNullable(passwordAsChars) //
.filter(it -> !ObjectUtils.isEmpty(passwordAsChars)) //
.map(it -> new RedisPassword(Arrays.copyOf(it, it.length))) //
.orElseGet(RedisPassword::none);
if (ObjectUtils.isEmpty(passwordAsChars)) {
return none();
}

return new RedisPassword(Arrays.copyOf(passwordAsChars, passwordAsChars.length));
}

/**
Expand Down