-
Notifications
You must be signed in to change notification settings - Fork 214
/
ImmutableArrayFunctionExpression.java
83 lines (71 loc) · 3.21 KB
/
ImmutableArrayFunctionExpression.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
/*
* Copyright (c) 2022 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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.concurrent.Immutable;
/**
* Immutable implementation of {@link ArrayFunctionExpression} defining which {@link PipelineFunction}s are supported.
*/
@Immutable
public class ImmutableArrayFunctionExpression implements ArrayFunctionExpression{
/**
* Singleton instance of the ImmutableFunctionExpression.
*/
static final ImmutableArrayFunctionExpression INSTANCE = new ImmutableArrayFunctionExpression();
private static final List<PipelineFunction> SUPPORTED = Collections.unmodifiableList(Arrays.asList(
new PipelineFunctionFilter(), // fn:filter(filterValue,rqlFunction,comparedValue)
new PipelineFunctionDefault(), // fn:default('fallback value')
new PipelineFunctionSubstringBefore(), // fn:substring-before(':')
new PipelineFunctionSubstringAfter(), // fn:substring-after(':')
new PipelineFunctionLower(), // fn:lower()
new PipelineFunctionUpper(), // fn:upper()
new PipelineFunctionDelete(), // fn:delete()
new PipelineFunctionReplace(), // fn:replace('from', 'to')
new PipelineFunctionSplit() // fn:split('separator')
));
@Override
public String getPrefix() {
return PREFIX;
}
@Override
public List<String> getSupportedNames() {
return SUPPORTED.stream()
.map(PipelineFunction::getName)
.collect(Collectors.toList());
}
@Override
public boolean supports(final String expressionName) {
// it is sufficient that the passed in name starts with the function name and an opening parentheses,
// e.g.: default('foo'). the function validates itself whether the remaining part is valid.
return SUPPORTED.stream()
.map(PipelineFunction::getName)
.anyMatch(psfName -> expressionName.startsWith(psfName + "("));
}
@Override
public Stream<PipelineElement> resolve(final String expression, final PipelineElement resolvedInputValue,
final ExpressionResolver expressionResolver) {
if (!supports(expression.replaceFirst(getPrefix() + ":", ""))) {
throw PlaceholderFunctionUnknownException.newBuilder(expression).build();
}
return SUPPORTED.stream()
.filter(pf -> expression.startsWith(getPrefix() + ":" + pf.getName() + "("))
.flatMap(pf -> pf.applyStreaming(resolvedInputValue,
expression.replaceFirst(getPrefix() + ":" + pf.getName(), "").trim(),
expressionResolver)
);
}
}