Skip to content

Commit

Permalink
DATAREDIS-1212 Remove redundant null-check in RedisKeyValueAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
theigl committed Oct 13, 2020
1 parent b27fbd0 commit a9de7cc
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@
*/
package org.springframework.data.redis.connection;

import org.springframework.data.redis.connection.util.ByteArrayWrapper;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
* Default message implementation.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Thomas Heigl
*/
public class DefaultMessage implements Message {

Expand All @@ -43,6 +39,9 @@ public DefaultMessage(byte[] channel, byte[] body) {
this.channel = channel;
}

/**
* @return
*/
public byte[] getChannel() {
return channel.clone();
}
Expand All @@ -51,28 +50,9 @@ public byte[] getBody() {
return body.clone();
}

public boolean hasChannel() {
return !ObjectUtils.isEmpty(channel);
}

public boolean hasBody() {
return !ObjectUtils.isEmpty(body);
}

public boolean channelStartsWith(byte[] prefix) {
return ByteUtils.startsWith(channel, prefix);
}

public boolean bodyStartsWith(byte[] prefix) {
return ByteUtils.startsWith(body, prefix);
}

public ByteArrayWrapper getChannelAsWrapper() {
return new ByteArrayWrapper(channel);
}

@Override
public String toString() {

if (toString == null) {
toString = new String(body);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

import java.io.Serializable;

import org.springframework.data.redis.connection.util.ByteArrayWrapper;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.lang.Nullable;

/**
* Class encapsulating a Redis message body and its properties.
*
* @author Costin Leau
* @author Christoph Strobl
* @author Thomas Heigl
*/
public interface Message extends Serializable {

Expand All @@ -43,50 +40,4 @@ public interface Message extends Serializable {
* @return message channel. Never {@literal null}.
*/
byte[] getChannel();

/**
* Check if the message has a channel
*
* @return {@code true} if the message has a channel, otherwise {@code false}
*/
default boolean hasChannel() {
return !ObjectUtils.isEmpty(getChannel());
}

/**
* Check if the message has a body
*
* @return {@code true} if the message has a body, otherwise {@code false}
*/
default boolean hasBody() {
return !ObjectUtils.isEmpty(getBody());
}

/**
* Checks if the message channel starts with the given prefix
*
* @return {@code true} if the channel starts with the given prefix, otherwise {@code false}
*/
default boolean channelStartsWith(byte[] prefix) {
return ByteUtils.startsWith(getChannel(), prefix);
}

/**
* Checks if the message body starts with the given prefix
*
* @return {@code true} if the body starts with the given prefix, otherwise {@code false}
*/
default boolean bodyStartsWith(byte[] prefix) {
return ByteUtils.startsWith(getBody(), prefix);
}

/**
* Returns the channel associated with the message wrapped in a {@link ByteArrayWrapper}
*
* @return the wrapped channel
*/
default ByteArrayWrapper getChannelAsWrapper() {
return new ByteArrayWrapper(getChannel());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public void onMessage(Message message, @Nullable byte[] pattern) {

Object value = CollectionUtils.isEmpty(hash) ? null : converter.read(Object.class, new RedisData(hash));

String channel = message.hasChannel()
String channel = !ObjectUtils.isEmpty(message.getChannel())
? converter.getConversionService().convert(message.getChannel(), String.class)
: null;

Expand All @@ -826,7 +826,7 @@ public void onMessage(Message message, @Nullable byte[] pattern) {

private boolean isKeyExpirationMessage(Message message) {

if (message == null || !message.hasChannel() || !message.hasBody()) {
if (message == null) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public KeyspaceEventMessageListener(RedisMessageListenerContainer listenerContai
@Override
public void onMessage(Message message, @Nullable byte[] pattern) {

if (message == null || !message.hasChannel() || !message.hasBody()) {
if (message == null || ObjectUtils.isEmpty(message.getChannel()) || ObjectUtils.isEmpty(message.getBody())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ public void onMessage(Message message, @Nullable byte[] pattern) {
} else {
pattern = null;
// do channel matching first
listeners = channelMapping.get(message.getChannelAsWrapper());
listeners = channelMapping.get(new ByteArrayWrapper(message.getChannel()));
}

if (!CollectionUtils.isEmpty(listeners)) {
Expand Down

This file was deleted.

0 comments on commit a9de7cc

Please sign in to comment.