Debian Codegraph @

net/sf/saxon/expr/instruct/ParameterSet.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.instruct;
9
10 import net.sf.saxon.om.Sequence;
11 import net.sf.saxon.om.StructuredQName;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.value.Closure;
14
15 import java.util.Arrays;
16 import java.util.Map;
17
18 /**
19 * A ParameterSet is a set of parameters supplied when calling a template.
20 * It is a collection of name-value pairs.
21 * (Use of numeric IDs dropped in 9.6 to support separate compilation of packages)
22 */
23
24 public class ParameterSet {
25 private StructuredQName[] keys;
26 private Sequence<?>[] values;
27 private boolean[] typeChecked;
28 private int used = 0;
29
30 public static ParameterSet EMPTY_PARAMETER_SET = new ParameterSet(0);
31
32 /**
33 * Create an empty parameter set
34 */
35
36 public ParameterSet() {
37 this(10);
38 }
39
40 /**
41 * Create a parameter set specifying the initial capacity
42 *
43 * @param capacity the nominal number of entries in the parameter set
44 */
45
46 public ParameterSet(int capacity) {
47 keys = new StructuredQName[capacity];
48 values = (Sequence<?>[])new Sequence[capacity];
49 typeChecked = new boolean[capacity];
50 }
51
52 /**
53 * Create a parameter set from a name/value map
54 * @param map the supplied map
55 */
56
57 public ParameterSet(Map<StructuredQName, Sequence<?>> map) {
58 this(map.size());
59 int i = 0;
60 for (Map.Entry<StructuredQName, Sequence<?>> entry : map.entrySet()) {
61 keys[i] = entry.getKey();
62 values[i] = entry.getValue();
63 typeChecked[i++] = false;
64 }
65 used = i;
66 }
67
68 /**
69 * Create a parameter set as a copy of an existing parameter set
70 *
71 * @param existing the parameter set to be copied
72 * @param extra the space to be allocated for additional entries
73 */
74
75 public ParameterSet(ParameterSet existing, int extra) {
76 this(existing.used + extra);
77 for (int i = 0; i < existing.used; i++) {
78 put(existing.keys[i], existing.values[i], existing.typeChecked[i]);
79 }
80 }
81
82 /**
83 * Get the number of parameters in the parameter set
84 * @return the number of parameters
85 */
86
87 public int size() {
88 return used;
89 }
90
91 /**
92 * Add a parameter to the ParameterSet
93 *
94 * @param id The parameter id, representing its name.
95 * @param value The value of the parameter
96 * @param checked True if the caller has done static type checking against the required type
97 */
98
99 public void put(StructuredQName id, Sequence<?> value, boolean checked) {
100 for (int i = 0; i < used; i++) {
101 if (keys[i].equals(id)) {
102 values[i] = value;
103 typeChecked[i] = checked;
104 return;
105 }
106 }
107 if (used + 1 > keys.length) {
108 int newLength = used <= 5 ? 10 : used * 2;
109 values = Arrays.copyOf(values, newLength);
110 keys = Arrays.copyOf(keys, newLength);
111 typeChecked = Arrays.copyOf(typeChecked, newLength);
112 }
113 keys[used] = id;
114 typeChecked[used] = checked;
115 values[used++] = value;
116 }
117
118 /**
119 * Get the set of key names
120 * @return the key names
121 */
122
123 public StructuredQName[] getParameterNames() {
124 return keys;
125 }
126
127 /**
128 * Get the index position of a parameter
129 *
130 * @param id The numeric parameter id, representing its name.
131 * @return The index position of the parameter, or -1 if not defined
132 */
133
134 public int getIndex(StructuredQName id) {
135 for (int i = 0; i < used; i++) {
136 if (keys[i].equals(id)) {
137 return i;
138 }
139 }
140 return -1;
141 }
142
143 /**
144 * Get the value of the parameter at a given index
145 *
146 * @param index the position of the entry required
147 * @return the value of the parameter at that position
148 */
149
150 public Sequence<?> getValue(int index) {
151 return values[index];
152 }
153
154 /**
155 * Determine whether the parameter at a given index has been type-checked
156 *
157 * @param index the position of the entry required
158 * @return true if the parameter at that position has been type-checked
159 */
160
161 public boolean isTypeChecked(int index) {
162 return typeChecked[index];
163 }
164
165 /**
166 * Clear all values
167 */
168
169 public void clear() {
170 used = 0;
171 }
172
173 /**
174 * If any values are non-memo closures, expand them
175 *
176 * @throws net.sf.saxon.trans.XPathException
177 * if an error occurs evaluating any closures
178 */
179
180 public void materializeValues() throws XPathException {
181 for (int i = 0; i < used; i++) {
182 if (values[i] instanceof Closure) {
183 values[i] = ((Closure) values[i]).reduce();
184 }
185 }
186 }
187
188 public static final int NOT_SUPPLIED = 0;
189 public static final int SUPPLIED = 1;
190 public static final int SUPPLIED_AND_CHECKED = 2;
191
192 }