Skip to content

Commit 979fb6a

Browse files
jxblummp911de
authored andcommitted
Simplify logic in RedisMessageListenerContainer and supporting classes.
Closes #2662 Original pull request: #2663
1 parent 5d29747 commit 979fb6a

File tree

4 files changed

+265
-256
lines changed

4 files changed

+265
-256
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2017-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.listener;
17+
18+
import org.springframework.lang.Nullable;
19+
import org.springframework.util.Assert;
20+
import org.springframework.util.ObjectUtils;
21+
22+
/**
23+
* Abstract base class for defining {@link Topic Topics}.
24+
*
25+
* @author John Blum
26+
* @see org.springframework.data.redis.listener.Topic
27+
* @since 3.2.0
28+
*/
29+
abstract class AbstractTopic implements Topic {
30+
31+
private final String name;
32+
33+
AbstractTopic(String label, String name) {
34+
Assert.notNull(name,() -> label + " must not be null");
35+
this.name = name;
36+
}
37+
38+
@Override
39+
public String getTopic() {
40+
return this.name;
41+
}
42+
43+
@Override
44+
public boolean equals(@Nullable Object obj) {
45+
46+
if (this == obj) {
47+
return true;
48+
}
49+
50+
if (!(obj instanceof AbstractTopic that)) {
51+
return false;
52+
}
53+
54+
// Must be exact Topic type
55+
if (this.getClass() != that.getClass()) {
56+
return false;
57+
}
58+
59+
return ObjectUtils.nullSafeEquals(this.getTopic(), that.getTopic());
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return ObjectUtils.nullSafeHashCode(getTopic());
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return getTopic();
70+
}
71+
}

src/main/java/org/springframework/data/redis/listener/ChannelTopic.java

+12-51
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,32 @@
1515
*/
1616
package org.springframework.data.redis.listener;
1717

18-
import org.springframework.lang.Nullable;
19-
import org.springframework.util.Assert;
20-
import org.springframework.util.ObjectUtils;
21-
2218
/**
23-
* Channel topic implementation (maps to a Redis channel).
19+
* {@link Topic Channel Topic} implementation mapping to a Redis channel.
2420
*
2521
* @author Costin Leau
2622
* @author Mark Paluch
23+
* @author John Blum
2724
*/
28-
public class ChannelTopic implements Topic {
29-
30-
private final String channelName;
31-
32-
/**
33-
* Constructs a new {@link ChannelTopic} instance.
34-
*
35-
* @param name must not be {@literal null}.
36-
*/
37-
public ChannelTopic(String name) {
38-
39-
Assert.notNull(name, "Topic name must not be null");
40-
41-
this.channelName = name;
42-
}
25+
public class ChannelTopic extends AbstractTopic {
4326

4427
/**
4528
* Create a new {@link ChannelTopic} for channel subscriptions.
4629
*
47-
* @param name the channel name, must not be {@literal null} or empty.
48-
* @return the {@link ChannelTopic} for {@code channelName}.
30+
* @param channelName {@link String name} of the Redis channel; must not be {@literal null} or {@literal empty}.
31+
* @return the {@link ChannelTopic} for the given {@code channelName}.
4932
* @since 2.1
5033
*/
51-
public static ChannelTopic of(String name) {
52-
return new ChannelTopic(name);
34+
public static ChannelTopic of(String channelName) {
35+
return new ChannelTopic(channelName);
5336
}
5437

5538
/**
56-
* @return topic name.
39+
* Constructs a new {@link ChannelTopic} instance.
40+
*
41+
* @param channelName must not be {@literal null}.
5742
*/
58-
@Override
59-
public String getTopic() {
60-
return channelName;
61-
}
62-
63-
@Override
64-
public String toString() {
65-
return channelName;
66-
}
67-
68-
@Override
69-
public boolean equals(@Nullable Object o) {
70-
71-
if (this == o)
72-
return true;
73-
if (o == null || getClass() != o.getClass())
74-
return false;
75-
76-
ChannelTopic that = (ChannelTopic) o;
77-
78-
return ObjectUtils.nullSafeEquals(channelName, that.channelName);
79-
}
80-
81-
@Override
82-
public int hashCode() {
83-
return ObjectUtils.nullSafeHashCode(channelName);
43+
public ChannelTopic(String channelName) {
44+
super("Topic name", channelName);
8445
}
8546
}

src/main/java/org/springframework/data/redis/listener/PatternTopic.java

+9-49
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,32 @@
1515
*/
1616
package org.springframework.data.redis.listener;
1717

18-
import org.springframework.lang.Nullable;
19-
import org.springframework.util.Assert;
20-
import org.springframework.util.ObjectUtils;
21-
2218
/**
23-
* Pattern topic (matching multiple channels).
19+
* {@link Topic} {@link String pattern} matching multiple Redis channels.
2420
*
2521
* @author Costin Leau
2622
* @author Mark Paluch
2723
* @author Christoph Strobl
2824
*/
29-
public class PatternTopic implements Topic {
30-
31-
private final String channelPattern;
32-
33-
/**
34-
* Constructs a new {@link PatternTopic} instance.
35-
*
36-
* @param pattern must not be {@literal null}.
37-
*/
38-
public PatternTopic(String pattern) {
39-
40-
Assert.notNull(pattern, "Pattern must not be null");
41-
42-
this.channelPattern = pattern;
43-
}
25+
public class PatternTopic extends AbstractTopic {
4426

4527
/**
4628
* Create a new {@link PatternTopic} for channel subscriptions based on a {@code pattern}.
4729
*
48-
* @param pattern the channel pattern, must not be {@literal null} or empty.
49-
* @return the {@link PatternTopic} for {@code pattern}.
30+
* @param pattern {@link String pattern} used to match channels; must not be {@literal null} or {@literal empty}.
31+
* @return the {@link PatternTopic} for the given {@code pattern}.
5032
* @since 2.1
5133
*/
5234
public static PatternTopic of(String pattern) {
5335
return new PatternTopic(pattern);
5436
}
5537

5638
/**
57-
* @return channel pattern.
39+
* Constructs a new {@link PatternTopic} instance.
40+
*
41+
* @param channelPattern must not be {@literal null}.
5842
*/
59-
@Override
60-
public String getTopic() {
61-
return channelPattern;
62-
}
63-
64-
@Override
65-
public String toString() {
66-
return channelPattern;
67-
}
68-
69-
@Override
70-
public boolean equals(@Nullable Object o) {
71-
72-
if (this == o)
73-
return true;
74-
if (o == null || getClass() != o.getClass())
75-
return false;
76-
77-
PatternTopic that = (PatternTopic) o;
78-
79-
return ObjectUtils.nullSafeEquals(channelPattern, that.channelPattern);
80-
}
81-
82-
@Override
83-
public int hashCode() {
84-
return ObjectUtils.nullSafeHashCode(channelPattern);
43+
public PatternTopic(String channelPattern) {
44+
super("Pattern", channelPattern);
8545
}
8646
}

0 commit comments

Comments
 (0)