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