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