-
Notifications
You must be signed in to change notification settings - Fork 215
/
PipelineElement.java
217 lines (192 loc) · 6.59 KB
/
PipelineElement.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
215
216
217
/*
* Copyright (c) 2021 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.placeholders;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* An element going through some pipeline of functions.
*/
public interface PipelineElement extends Iterable<String> {
/**
* Get the type of this pipeline element.
*
* @return the type.
*/
Type getType();
/**
* Advance a resolved value to the next stage. Other elements are unchanged.
*
* @param stringProcessor What to do for resolved values.
* @return the pipeline element at the next pipeline stage.
*/
PipelineElement onResolved(Function<String, PipelineElement> stringProcessor);
/**
* Advance an unresolved value to the next stage. Other elements are unchanged.
*
* @param nextPipelineElement supplier of the next pipeline element.
* @return the pipeline element at the next pipeline stage.
*/
PipelineElement onUnresolved(Supplier<PipelineElement> nextPipelineElement);
/**
* Advance a deleted value to the next stage. Other elements are unchanged.
*
* @param nextPipelineElement supplier of the next pipeline element.
* @return the pipeline element at the next pipeline stage.
*/
PipelineElement onDeleted(Supplier<PipelineElement> nextPipelineElement);
/**
* Concatenates all resolved values of the given pipelineElement with the resolved values of this pipeline element.
* In case this or the given pipeline element is a {@link Type#DELETED} pipeline element, the result will
* always be a deleted pipeline element.
*
* @param pipelineElement the pipeline element to concatenate
* @return the pipeline element which holds the concatenated resolved vales.
* @since 2.4.0
*/
PipelineElement concat(PipelineElement pipelineElement);
/**
* Evaluate this pipeline element by a visitor.
*
* @param visitor the visitor.
* @param <T> the type of results.
* @return the evaluation result.
* @deprecated Since 2.4.0. Use {@link #evaluate(PipelineElementVisitor)} instead.
*/
@Deprecated
<T> T accept(PipelineElementVisitor<T> visitor);
/**
* Evaluate this pipeline element by a visitor.
*
* @param visitor the visitor.
* @param <T> the type of results.
* @return the evaluation result.
* @since 2.4.0
*/
<T> List<T> evaluate(PipelineElementVisitor<T> visitor);
/**
* Convert a resolved value into another resolved value and leave other elements untouched.
*
* @param mapper what to do about the resolved value.
* @return the mapped resolved value.
*/
default PipelineElement map(final Function<String, String> mapper) {
return onResolved(mapper.andThen(PipelineElement::resolved));
}
/**
* Combine 2 pipeline elements such that unresolved elements are replaced by resolved elements, which is in turn
* replaced by the deletion signifier.
* <p>
* Consider all pipeline elements as a simple lattice with PipelineElementDeleted as top and
* PipelineElementUnresolved as bottom. This function computes a maximal of 2 elements in the lattice.
* </p>
*
* @param other the other element.
* @return the combined element.
*/
default PipelineElement orElse(final PipelineElement other) {
return onDeleted(() -> this)
.onResolved(s -> this)
.onUnresolved(() -> other);
}
/**
* @return an optional holding the first resolved value. Empty if no values were resolved.
* @since 2.4.0
*/
default Optional<String> findFirst() {
return toStream().findFirst();
}
/**
* @return an optional holding the first resolved value. Empty if no values were resolved.
* @deprecated Use {@link #findFirst()} instead because of more explicit naming.
*/
@Deprecated
default Optional<String> toOptional() {
return findFirst();
}
/**
* Converts this pipeline element into a stream of resolved strings.
* If the PipelineElement did not resolve any value, the stream will be empty.
* Unresolved or deleted elements will result in an empty stream.
*
* @return a stream of strings which this pipeline element did resolve.
* @since 2.4.0
*/
default Stream<String> toStream() {
return Stream.empty();
}
/**
* Create a builder of a visitor to evaluate pipeline elements.
*
* @param <T> the type of results.
* @return the visitor builder.
*/
static <T> PipelineElementVisitor.Builder<T> newVisitorBuilder() {
return ImmutablePipelineElementVisitor.newBuilder();
}
/**
* Creat a pipeline element containing the resolved values.
*
* @param values the resolved values.
* @return the pipeline element.
*/
static PipelineElement resolved(final Collection<String> values) {
return values.isEmpty() ? PipelineElement.unresolved() : PipelineElementResolved.of(values);
}
/**
* Creat a pipeline element containing a resolved value.
*
* @param value the resolved value.
* @return the pipeline element.
*/
static PipelineElement resolved(final String value) {
return resolved(Collections.singletonList(value));
}
/**
* Get the unique pipeline element signifying deletion of the whole string containing the pipeline.
*
* @return the deleted element.
*/
static PipelineElement deleted() {
return PipelineElementDeleted.INSTANCE;
}
/**
* Get the unique pipeline element signifying failed resolution.
*
* @return the unresolved element.
*/
static PipelineElement unresolved() {
return PipelineElementUnresolved.INSTANCE;
}
/**
* Types of pipeline element.
*/
enum Type {
/**
* Type of the signifier for deletion.
*/
DELETED,
/**
* Type of resolved values.
*/
RESOLVED,
/**
* Type of the signifier for resolution failure.
*/
UNRESOLVED
}
}