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 package gov.nist.secauto.metaschema.core.metapath;
28
29 import gov.nist.secauto.metaschema.core.metapath.function.FunctionService;
30 import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
31 import gov.nist.secauto.metaschema.core.metapath.item.IItem;
32 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
33
34 import java.util.List;
35 import java.util.Objects;
36 import java.util.stream.Collectors;
37
38 import edu.umd.cs.findbugs.annotations.NonNull;
39
40 class FunctionCall implements IExpression {
41 @NonNull
42 private final String name;
43 @NonNull
44 private final List<IExpression> arguments;
45 private IFunction function;
46
47
48
49
50
51
52
53
54
55 protected FunctionCall(@NonNull String name, @NonNull List<IExpression> arguments) {
56 this.name = Objects.requireNonNull(name, "name");
57 this.arguments = Objects.requireNonNull(arguments, "arguments");
58 }
59
60
61
62
63
64
65
66
67
68 public IFunction getFunction() {
69 synchronized (this) {
70 if (function == null) {
71 function = FunctionService.getInstance().getFunction(name, arguments);
72 }
73 return function;
74 }
75 }
76
77 @Override
78 public List<IExpression> getChildren() {
79 return arguments;
80 }
81
82 @Override
83 public Class<? extends IItem> getBaseResultType() {
84 Class<? extends IItem> retval = getFunction().getResult().getType();
85 if (retval == null) {
86 retval = IItem.class;
87 }
88 return retval;
89 }
90
91 @SuppressWarnings("null")
92 @Override
93 public String toASTString() {
94 return String.format("%s[name=%s]", getClass().getName(), getFunction().getName());
95 }
96
97 @Override
98 public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
99 return visitor.visitFunctionCall(this, context);
100 }
101
102 @Override
103 public ISequence<?> accept(DynamicContext dynamicContext, ISequence<?> focus) {
104 List<ISequence<?>> arguments = ObjectUtils.notNull(getChildren().stream().map(expression -> {
105 @NonNull ISequence<?> result = expression.accept(dynamicContext, focus);
106 return result;
107 }).collect(Collectors.toList()));
108
109 IFunction function = getFunction();
110 return function.execute(arguments, dynamicContext, focus);
111 }
112 }