/
SubscriberData.java
131 lines (116 loc) · 3.66 KB
/
SubscriberData.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.utils.pubsub.ddata;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Properties of a subscriber.
*/
@NotThreadSafe
public final class SubscriberData {
private final Set<String> topics;
@Nullable private final Predicate<Collection<String>> filter;
@Nullable private final String group;
private SubscriberData(final Set<String> topics,
@Nullable final Predicate<Collection<String>> filter,
@Nullable final String group) {
this.topics = topics;
this.filter = filter;
this.group = group;
}
/**
* Create subscriber data.
*
* @param topics topics the subscriber subscribes to.
* @param filter topic filter of the subscriber.
* @param group the group the subscriber belongs to, if any.
* @return the subscriber data.
*/
public static SubscriberData of(final Set<String> topics,
@Nullable final Predicate<Collection<String>> filter,
@Nullable final String group) {
return new SubscriberData(topics, filter, group);
}
/**
* Create an immutable copy of this object.
*
* @return the immutable copy.
*/
public SubscriberData export() {
return new SubscriberData(Set.copyOf(topics), filter, group);
}
/**
* Create a copy of this object with topics replaced.
*
* @param topics the new topics.
* @return the new subscriber data.
*/
public SubscriberData withTopics(final Set<String> topics) {
return new SubscriberData(topics, filter, group);
}
/**
* Create a copy of this object with filter replaced.
*
* @param filter the new filter.
* @return the new subscriber data.
*/
public SubscriberData withFilter(@Nullable final Predicate<Collection<String>> filter) {
return new SubscriberData(topics, filter, group);
}
/**
* @return topics the subscriber subscribes to.
*/
public Set<String> getTopics() {
return topics;
}
/**
* @return the filter of the subscriber.
*/
public Optional<Predicate<Collection<String>>> getFilter() {
return Optional.ofNullable(filter);
}
/**
* @return the group the subscriber belongs to, or an empty optional.
*/
public Optional<String> getGroup() {
return Optional.ofNullable(group);
}
@Override
public boolean equals(final Object other) {
if (other instanceof SubscriberData) {
final SubscriberData that = (SubscriberData) other;
return Objects.equals(topics, that.topics) &&
Objects.equals(filter, that.filter) &&
Objects.equals(group, that.group);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(topics, filter, group);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"topics=" + topics +
", filter=" + filter +
", group=" + group +
"]";
}
}