1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.function;
7   
8   import java.util.Collections;
9   import java.util.EnumSet;
10  import java.util.List;
11  import java.util.Set;
12  
13  import dev.metaschema.core.metapath.DynamicContext;
14  import dev.metaschema.core.metapath.MetapathException;
15  import dev.metaschema.core.metapath.function.impl.AbstractFunction;
16  import dev.metaschema.core.metapath.item.IItem;
17  import dev.metaschema.core.metapath.item.ISequence;
18  import dev.metaschema.core.metapath.type.ISequenceType;
19  import edu.umd.cs.findbugs.annotations.NonNull;
20  import edu.umd.cs.findbugs.annotations.Nullable;
21  
22  /**
23   * Provides a concrete implementation of a function call executor.
24   */
25  public class DefaultFunction
26      extends AbstractFunction {
27    // private static final Logger logger =
28    // LogManager.getLogger(AbstractFunction.class);
29  
30    @NonNull
31    private final Set<FunctionProperty> properties;
32    @NonNull
33    private final ISequenceType result;
34    @NonNull
35    private final IFunctionExecutor handler;
36  
37    /**
38     * Construct a new function signature.
39     *
40     * @param name
41     *          the name of the function
42     * @param properties
43     *          the characteristics of the function
44     * @param arguments
45     *          the argument signatures or an empty list
46     * @param result
47     *          the type of the result
48     * @param handler
49     *          the handler to call to execute the function
50     */
51    @SuppressWarnings({ "null", "PMD.LooseCoupling" })
52    DefaultFunction(
53        @NonNull String name,
54        @NonNull String namespace,
55        @NonNull EnumSet<FunctionProperty> properties,
56        @NonNull List<IArgument> arguments,
57        @NonNull ISequenceType result,
58        @NonNull IFunctionExecutor handler) {
59      super(name, namespace, arguments);
60      this.properties = Collections.unmodifiableSet(properties);
61      this.result = result;
62      this.handler = handler;
63    }
64  
65    @Override
66    public Set<FunctionProperty> getProperties() {
67      return properties;
68    }
69  
70    @Override
71    public ISequenceType getResult() {
72      return result;
73    }
74  
75    @Override
76    public boolean isNamedFunction() {
77      return true;
78    }
79  
80    /**
81     * Execute the provided function using the provided arguments, dynamic context,
82     * and focus.
83     *
84     * @param arguments
85     *          the function arguments
86     * @param dynamicContext
87     *          the dynamic evaluation context
88     * @param focus
89     *          the current focus item in the evaluation context. This represents
90     *          the context item for anonymous function evaluation. May be null for
91     *          functions that don't require context item access.
92     * @return a sequence containing the result of the execution
93     * @throws MetapathException
94     *           if an error occurred while executing the function
95     */
96    @Override
97    @NonNull
98    protected ISequence<?> executeInternal(
99        @NonNull List<ISequence<?>> arguments,
100       @NonNull DynamicContext dynamicContext,
101       @Nullable IItem focus) {
102     return handler.execute(this, arguments, dynamicContext, focus);
103   }
104 }