/
ImmutableExpressionResolver.java
145 lines (113 loc) · 5.49 KB
/
ImmutableExpressionResolver.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
/*
* Copyright (c) 2017-2018 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.placeholders;
import static org.eclipse.ditto.model.placeholders.Expression.SEPARATOR;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.base.common.Placeholders;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.connectivity.UnresolvedPlaceholderException;
/**
* Immutable implementation of {@link ExpressionResolver} containing the logic of how an expression is resolved.
*/
@Immutable
final class ImmutableExpressionResolver implements ExpressionResolver {
private static final String PIPE_PATTERN_STR = "(\\s+\\|\\s+)";
private static final Pattern PIPE_PATTERN = Pattern.compile(PIPE_PATTERN_STR);
private static final Function<String, DittoRuntimeException> UNRESOLVED_INPUT_HANDLER = unresolvedInput ->
UnresolvedPlaceholderException.newBuilder(unresolvedInput).build();
private final List<PlaceholderResolver<?>> placeholderResolvers;
ImmutableExpressionResolver(final List<PlaceholderResolver<?>> placeholderResolvers) {
this.placeholderResolvers = Collections.unmodifiableList(new ArrayList<>(placeholderResolvers));
}
@Override
public String resolve(final String expressionTemplate, final boolean allowUnresolved) {
String templateInWork = expressionTemplate;
int placeholdersIdx = 0;
while (Placeholders.containsAnyPlaceholder(templateInWork) && placeholdersIdx < placeholderResolvers.size()) {
final PlaceholderResolver<?> placeholderResolver = placeholderResolvers.get(placeholdersIdx);
final Function<String, Optional<String>> placeholderReplacerFunction =
makePlaceholderReplacerFunction(placeholderResolver);
placeholdersIdx++;
final boolean isLastPlaceholderResolver = placeholdersIdx == placeholderResolvers.size();
templateInWork = Placeholders.substitute(templateInWork, placeholderReplacerFunction,
UNRESOLVED_INPUT_HANDLER, !isLastPlaceholderResolver || allowUnresolved);
}
return templateInWork;
}
@Override
public Optional<String> resolveSinglePlaceholder(final String placeholder) {
for (final PlaceholderResolver<?> resolver : placeholderResolvers) {
final Optional<String> resolvedOpt = makePlaceholderReplacerFunction(resolver).apply(placeholder);
if (resolvedOpt.isPresent()) {
return resolvedOpt;
}
}
return Optional.empty();
}
private Function<String, Optional<String>> makePlaceholderReplacerFunction(
final PlaceholderResolver<?> placeholderResolver) {
return template -> {
final List<String> pipelineStagesExpressions = PIPE_PATTERN.splitAsStream(template)
.map(String::trim)
.collect(Collectors.toList());
final String placeholderTemplate =
pipelineStagesExpressions.get(0); // the first pipeline stage has to start with a placeholder
final List<String> pipelineStages = pipelineStagesExpressions.stream()
.skip(1)
.map(String::trim)
.collect(Collectors.toList());
final Pipeline pipeline = new ImmutablePipeline(ImmutableFunctionExpression.INSTANCE, pipelineStages);
final Optional<String> pipelineInput = resolvePlaceholder(placeholderResolver, placeholderTemplate);
return pipeline.execute(pipelineInput, this);
};
}
private Optional<String> resolvePlaceholder(final PlaceholderResolver<?> prefixed, final String placeholder) {
final int separatorIndex = placeholder.indexOf(SEPARATOR);
if (separatorIndex == -1) {
throw UnresolvedPlaceholderException.newBuilder(placeholder).build();
}
final String prefix = placeholder.substring(0, separatorIndex).trim();
final String placeholderWithoutPrefix = placeholder.substring(separatorIndex + 1).trim();
return Optional.of(prefixed)
.filter(p -> prefix.equals(p.getPrefix()))
.filter(p -> p.supports(placeholderWithoutPrefix))
.flatMap(p -> prefixed.resolve(placeholderWithoutPrefix));
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ImmutableExpressionResolver)) {
return false;
}
final ImmutableExpressionResolver that = (ImmutableExpressionResolver) o;
return Objects.equals(placeholderResolvers, that.placeholderResolvers);
}
@Override
public int hashCode() {
return Objects.hash(placeholderResolvers);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"placeholderResolvers=" + placeholderResolvers +
"]";
}
}