/
ImmutableTopicPath.java
executable file
·214 lines (187 loc) · 7.35 KB
/
ImmutableTopicPath.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.protocoladapter;
import static java.util.Objects.requireNonNull;
import java.text.MessageFormat;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.concurrent.Immutable;
/**
* Immutable implementation of {@link TopicPath}.
*/
@Immutable
final class ImmutableTopicPath implements TopicPath {
static final String PROP_NAME_NAMESPACE = "namespace";
static final String PROP_NAME_ID = "id";
private static final String PROP_NAME_GROUP = "group";
private static final String PROP_NAME_CHANNEL = "channel";
private static final String PROP_NAME_CRITERION = "criterion";
private static final String PROP_NAME_ACTION = "action";
private static final String PROP_NAME_SUBJECT = "subject";
private final String namespace;
private final String id;
private final Group group;
private final Channel channel;
private final Criterion criterion;
private final Action action;
private final String path;
private final String subject;
private ImmutableTopicPath(final String namespace, final String id, final Group group, final Channel channel,
final Criterion criterion, final Action action, final String subject) {
this.namespace = namespace;
this.id = id;
this.group = group;
this.channel = channel;
this.criterion = criterion;
this.action = action;
this.subject = subject;
this.path = buildPath();
}
/**
* Returns a new ImmutableTopicPath for the specified {@code namespace}, {@code id}, {@code group} and
* {@code criterion}.
*
* @param namespace the namespace.
* @param id the id.
* @param group the group.
* @param channel the channel.
* @param criterion the criterion.
* @return the TopicPath.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ImmutableTopicPath of(final String namespace, final String id, final Group group,
final Channel channel, final Criterion criterion) {
requireNonNull(namespace, PROP_NAME_NAMESPACE);
requireNonNull(id, PROP_NAME_ID);
requireNonNull(group, PROP_NAME_GROUP);
requireNonNull(channel, PROP_NAME_CHANNEL);
requireNonNull(criterion, PROP_NAME_CRITERION);
return new ImmutableTopicPath(namespace, id, group, channel, criterion, null, null);
}
/**
* Returns a new ImmutableTopicPath for the specified {@code namespace}, {@code id}, {@code group},
* {@code criterion} and {@code action}.
*
* @param namespace the namespace.
* @param id the id.
* @param group the group.
* @param channel the channel.
* @param criterion the criterion.
* @param action the action.
* @return the TopicPath.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ImmutableTopicPath of(final String namespace, final String id, final Group group,
final Channel channel,
final Criterion criterion, final Action action) {
requireNonNull(namespace, PROP_NAME_NAMESPACE);
requireNonNull(id, PROP_NAME_ID);
requireNonNull(group, PROP_NAME_GROUP);
requireNonNull(channel, PROP_NAME_CHANNEL);
requireNonNull(criterion, PROP_NAME_CRITERION);
requireNonNull(action, PROP_NAME_ACTION);
return new ImmutableTopicPath(namespace, id, group, channel, criterion, action, null);
}
/**
* Returns a new ImmutableTopicPath for the specified {@code namespace}, {@code id}, {@code group},
* {@code criterion} and {@code action}
*
* @param namespace the namespace.
* @param id the id.
* @param group the group.
* @param channel the channel.
* @param criterion the criterion.
* @param subject the subject of the path.
* @return the TopicPath.
* @throws NullPointerException if any argument is {@code null}.
*/
public static ImmutableTopicPath of(final String namespace, final String id, final Group group,
final Channel channel,
final Criterion criterion, final String subject) {
requireNonNull(namespace, PROP_NAME_NAMESPACE);
requireNonNull(id, PROP_NAME_ID);
requireNonNull(group, PROP_NAME_GROUP);
requireNonNull(channel, PROP_NAME_CHANNEL);
requireNonNull(criterion, PROP_NAME_CRITERION);
requireNonNull(subject, PROP_NAME_SUBJECT);
return new ImmutableTopicPath(namespace, id, group, channel, criterion, null, subject);
}
@Override
public String getNamespace() {
return namespace;
}
@Override
public Group getGroup() {
return group;
}
@Override
public Channel getChannel() {
return channel;
}
@Override
public Criterion getCriterion() {
return criterion;
}
@Override
public Optional<Action> getAction() {
return Optional.ofNullable(action);
}
@Override
public Optional<String> getSubject() {
return Optional.ofNullable(subject);
}
@Override
public String getId() {
return id;
}
@Override
public String getPath() {
return path;
}
@SuppressWarnings({"squid:MethodCyclomaticComplexity", "squid:S1067", "OverlyComplexMethod"})
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ImmutableTopicPath that = (ImmutableTopicPath) o;
return Objects.equals(namespace, that.namespace) && Objects.equals(id, that.id) && group == that.group
&& channel == that.channel && criterion == that.criterion && Objects.equals(action, that.action) &&
Objects
.equals(subject, that.subject) && Objects.equals(path, that.path);
}
@Override
public int hashCode() {
return Objects.hash(namespace, id, group, channel, criterion, action, path, subject);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" + "namespace=" + namespace + ", id=" + id + ", group=" + group +
", channel=" + channel
+ ", criterion=" + criterion + ", action=" + action + ", subject=" + subject + ", path=" + path + ']';
}
private String buildPath() {
if (action != null) {
// e.g.: <ns>/<id>/things/twin/commands/modify
return MessageFormat.format("{0}/{1}/{2}/{3}/{4}/{5}", namespace, id, group, channel, criterion, action);
} else if (subject != null) {
// e.g.: <ns>/<id>/things/live/messages/<msgSubject>
return MessageFormat.format("{0}/{1}/{2}/{3}/{4}/{5}", namespace, id, group, channel, criterion, subject);
} else {
// e.g.: <ns>/<id>/things/twin/search
return MessageFormat.format("{0}/{1}/{2}/{3}/{4}", namespace, id, group, channel, criterion);
}
}
}