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.item.IItem;
9   import gov.nist.secauto.metaschema.core.metapath.item.ISequence;
10  
11  import java.util.List;
12  import java.util.Objects;
13  
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  import edu.umd.cs.findbugs.annotations.Nullable;
16  
17  /**
18   * Represents an immutable execution context for function calls in Metapath
19   * expressions.
20   * <p>
21   * This class is designed to support both named and anonymous functions by
22   * maintaining the function instance, its arguments, and the current context
23   * item. It ensures thread-safety through immutability and is primarily used
24   * during the evaluation of Metapath expressions and for caching the function
25   * results.
26   */
27  public final class CalledContext {
28    @NonNull
29    private final IFunction function;
30    @Nullable
31    private final IItem contextItem;
32    @NonNull
33    private final List<ISequence<?>> arguments;
34  
35    /**
36     * Creates an immutable execution context for a function call.
37     *
38     * @param function
39     *          the function to be executed
40     * @param arguments
41     *          the list of evaluated arguments as sequences, must match function's
42     *          arity
43     * @param contextItem
44     *          the optional context item representing the current node in scope
45     */
46    public CalledContext(
47        @NonNull IFunction function,
48        @NonNull List<ISequence<?>> arguments,
49        @Nullable IItem contextItem) {
50      this.function = function;
51      this.contextItem = contextItem;
52      this.arguments = arguments;
53    }
54  
55    /**
56     * Get the function instance associated with the calling context.
57     *
58     * @return the function instance
59     */
60    @NonNull
61    public IFunction getFunction() {
62      return function;
63    }
64  
65    /**
66     * Get the node item focus associated with the calling context.
67     *
68     * @return the context item, or null if no context is set
69     */
70    @Nullable
71    public IItem getContextItem() {
72      return contextItem;
73    }
74  
75    /**
76     * Get the arguments associated with the calling context.
77     *
78     * @return the arguments
79     */
80    @NonNull
81    public List<ISequence<?>> getArguments() {
82      return arguments;
83    }
84  
85    @Override
86    public int hashCode() {
87      final int prime = 31;
88      int result = 1;
89      result = prime * result + getFunction().hashCode();
90      return prime * result + Objects.hash(contextItem, arguments);
91    }
92  
93    @SuppressWarnings("PMD.OnlyOneReturn")
94    @Override
95    public boolean equals(Object obj) {
96      if (this == obj) {
97        return true;
98      }
99      if (obj == null || getClass() != obj.getClass()) {
100       return false;
101     }
102     CalledContext other = (CalledContext) obj;
103     if (!getFunction().equals(other.getFunction())) {
104       return false;
105     }
106     return Objects.equals(function, other.function)
107         && Objects.equals(arguments, other.arguments)
108         && Objects.equals(contextItem, other.contextItem);
109   }
110 }