-
Notifications
You must be signed in to change notification settings - Fork 23
/
Classifiers.java
130 lines (113 loc) · 4.72 KB
/
Classifiers.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
/*
* 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.client.internal.bus;
import java.util.EnumSet;
import java.util.Optional;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.TopicPath;
import org.eclipse.ditto.signals.events.thingsearch.SubscriptionEvent;
/**
* Factory class for classifiers.
*/
public final class Classifiers {
private Classifiers() {}
/**
* Create an identity classifier.
*
* @param <T> type of objects being classified.
* @return classifier that classifies each object as itself.
*/
public static <T> Classifier<T> identity() {
return Classification.Identity::of;
}
/**
* Classify adaptables according to their correlation ID.
*
* @return the correlation ID classifier.
*/
public static Classifier<Adaptable> correlationId() {
return Instances.CORRELATION_ID_CLASSIFIER;
}
/**
* Classify adaptables according to their streaming type: live commands, live events, live messages
* and twin events. Live command responses are classified as live commands but should be handled by
* a correlation-ID-based one-time subscriber in the normal case.
*
* @return the streaming type classifier.
*/
public static Classifier<Adaptable> streamingType() {
return Instances.STREAMING_TYPE_CLASSIFIER;
}
/**
* Classify thing-search events.
*
* @return classifier for thing-search events.
*/
public static Classifier<Adaptable> thingsSearch() {
return Instances.THINGS_SEARCH_CLASSIFIER;
}
private static final class StreamingTypeClassifier implements Classifier<Adaptable> {
@Override
public Optional<Classification> classify(final Adaptable message) {
final TopicPath topicPath = message.getTopicPath();
if (topicPath.getGroup() == TopicPath.Group.THINGS) {
switch (topicPath.getChannel()) {
case LIVE:
switch (topicPath.getCriterion()) {
case COMMANDS:
return Optional.of(Classification.StreamingType.LIVE_COMMAND);
case EVENTS:
return Optional.of(Classification.StreamingType.LIVE_EVENT);
case MESSAGES:
return Optional.of(Classification.StreamingType.LIVE_MESSAGE);
default:
return Optional.empty();
}
case TWIN:
if (topicPath.getCriterion() == TopicPath.Criterion.EVENTS) {
return Optional.of(Classification.StreamingType.TWIN_EVENT);
}
break;
default:
return Optional.empty();
}
}
return Optional.empty();
}
}
private static final class ThingsSearchClassifier implements Classifier<Adaptable> {
private static final EnumSet<TopicPath.SearchAction> SEARCH_EVENTS = EnumSet.of(
TopicPath.SearchAction.NEXT,
TopicPath.SearchAction.COMPLETE,
TopicPath.SearchAction.FAILED
);
@Override
public Optional<Classification> classify(final Adaptable message) {
return message.getTopicPath().getSearchAction()
.filter(SEARCH_EVENTS::contains)
.flatMap(action -> message.getPayload().getValue())
.filter(JsonValue::isObject)
.flatMap(jsonValue -> jsonValue.asObject().getValue(SubscriptionEvent.JsonFields.SUBSCRIPTION_ID))
.map(Classification::forThingsSearch);
}
}
private static final class Instances {
private static final Classifier<Adaptable> CORRELATION_ID_CLASSIFIER = adaptable ->
adaptable.getDittoHeaders()
.getCorrelationId()
.map(Classification::forCorrelationId);
private static final Classifier<Adaptable> STREAMING_TYPE_CLASSIFIER = new StreamingTypeClassifier();
private static final Classifier<Adaptable> THINGS_SEARCH_CLASSIFIER = new ThingsSearchClassifier();
}
}