-
Notifications
You must be signed in to change notification settings - Fork 215
/
Command.java
executable file
·180 lines (155 loc) · 5.41 KB
/
Command.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
/*
* Copyright (c) 2017 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.base.model.signals.commands;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.atteo.classindex.IndexSubclasses;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.FieldType;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithType;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
/**
* Base Interface for all commands which are understood by Ditto.
*
* @param <T> the type of the implementing class.
*/
@IndexSubclasses
public interface Command<T extends Command<T>> extends Signal<T> {
/**
* Type qualifier of commands.
*/
String TYPE_QUALIFIER = "commands";
/**
* Returns the type prefix of this command.
*
* @return the prefix.
*/
String getTypePrefix();
/**
* Returns the category this command belongs to.
*
* @return the command category.
*/
Category getCategory();
@Override
T setDittoHeaders(DittoHeaders dittoHeaders);
@Override
default JsonSchemaVersion getImplementedSchemaVersion() {
return getDittoHeaders().getSchemaVersion().orElse(getLatestSchemaVersion());
}
/**
* Returns all non-hidden marked fields of this command.
*
* @return a JSON object representation of this command including only non-hidden marked fields.
*/
@Override
default JsonObject toJson() {
return toJson(FieldType.notHidden());
}
@Override
JsonObject toJson(JsonSchemaVersion schemaVersion, Predicate<JsonField> predicate);
/**
* Indicates whether the specified signal argument is a live command.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is an instance of {@link Command} with channel
* {@value CHANNEL_LIVE} in its headers, {@code false} else.
* @since 3.0.0
*/
static boolean isLiveCommand(@Nullable final Signal<?> signal) {
return isMessageCommand(signal) || Command.isCommand(signal) && Signal.isChannelLive(signal);
}
/**
* Indicates whether the specified signal argument is a {@code ThingCommand} without requiring a direct dependency
* to the things-model.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is a {@code ThingCommand}, {@code false} else.
* @since 3.0.0
*/
static boolean isThingCommand(@Nullable final WithType signal) {
return WithType.hasTypePrefix(signal, WithType.THINGS_COMMANDS_PREFIX);
}
/**
* Indicates whether the specified signal argument is a {@code MessageCommand} without requiring a direct dependency
* to the messages-model.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is a {@code MessageCommand}, {@code false} else.
* @since 3.0.0
*/
static boolean isMessageCommand(@Nullable final WithType signal) {
return WithType.hasTypePrefix(signal, WithType.MESSAGES_COMMANDS_PREFIX);
}
/**
* Indicates whether the specified signal argument is an instance of {@code Command}.
*
* @param signal the signal to be checked.
* @return {@code true} if {@code signal} is an instance of {@link Command}, {@code false} else.
* @since 3.0.0
*/
static boolean isCommand(@Nullable final Signal<?> signal) {
return signal instanceof Command;
}
/**
* Categories every command is classified into.
*/
@Immutable
enum Category {
/**
* Category of commands that do not change the state of any entity.
*/
QUERY,
/**
* Category of commands that change the state of entities.
*/
MODIFY,
/**
* Category of commands that change the state of entities.
*/
MERGE,
/**
* Category of commands that delete entities.
*/
DELETE,
/**
* Category of commands that are neither of the above 3 (query, modify, delete) but perform an action on the
* entity.
*/
ACTION
}
/**
* This class contains common definitions for all fields of a {@code Command}'s JSON representation.
* Implementation of {@code Command} may add additional fields by extending this class.
*/
@Immutable
abstract class JsonFields {
/**
* JSON field containing the command's type as String.
*/
public static final JsonFieldDefinition<String> TYPE =
JsonFactory.newStringFieldDefinition("type", FieldType.REGULAR, JsonSchemaVersion.V_2);
/**
* Constructs a new {@code JsonFields} object.
*/
protected JsonFields() {
super();
}
}
}