Debian Codegraph @

net/sf/saxon/expr/oper/OperandArray.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2013 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.oper;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.Operand;
12 import net.sf.saxon.expr.OperandRole;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.function.Predicate;
19
20 /**
21 * Defines an operand set comprising an array of operands numbered zero to N. All operands must be present.
22 * Typically (but not exclusively) used for the arguments of a function call. The operands may all have the
23 * same operand roles, or have different operand roles.
24 */
25
26 public class OperandArray implements Iterable<Operand> {
27
28 private Operand[] operandArray;
29
30 // operand role is currently always NAVIGATE, but this could change in the future
31
32 public OperandArray(Expression parent, Expression[] args) {
33 this.operandArray = new Operand[args.length];
34 for (int i=0; i<args.length; i++) {
35 operandArray[i] = new Operand(parent, args[i], OperandRole.NAVIGATE);
36 }
37 }
38
39 public OperandArray(Expression parent, Expression[] args, OperandRole[] roles) {
40 this.operandArray = new Operand[args.length];
41 for (int i=0; i<args.length; i++) {
42 operandArray[i] = new Operand(parent, args[i], roles[i]);
43 }
44 }
45
46 public OperandArray(Expression parent, Expression[] args, OperandRole role) {
47 this.operandArray = new Operand[args.length];
48 for (int i=0; i<args.length; i++) {
49 operandArray[i] = new Operand(parent, args[i], role);
50 }
51 }
52
53 private OperandArray(Operand[] operands) {
54 this.operandArray = operands;
55 }
56
57 /**
58 * Returns an iterator over elements of type {@code T}.
59 *
60 * @return an Iterator.
61 */
62
63 @Override
64 public Iterator<Operand> iterator() {
65 return Arrays.asList(operandArray).iterator();
66 }
67
68 public Operand[] copy() {
69 return Arrays.copyOf(operandArray, operandArray.length);
70 }
71
72 /**
73 * Get the operand roles
74 * @return the roles of the operands as an array, one per operand
75 */
76
77 public OperandRole[] getRoles() {
78 OperandRole[] or = new OperandRole[operandArray.length];
79 for (int i = 0; i < or.length; i++) {
80 or[i] = operandArray[i].getOperandRole();
81 }
82 return or;
83 }
84
85 /**
86 * Get the operand whose identifying number is n.
87 * @param n the identifier of the operand (counting from zero)
88 * @return the operand, or null in the case of an optional operand that is absent in the case
89 * of this particular expression
90 * @throws IllegalArgumentException if there cannot be an operand at this position
91 */
92
93 public Operand getOperand(int n) {
94 try {
95 return operandArray[n];
96 } catch (ArrayIndexOutOfBoundsException a) {
97 throw new IllegalArgumentException();
98 }
99 }
100
101 /**
102 * Get the child expression associated with the operand whose identifying number is n.
103 * @param n the identifier of the operand
104 * @return the expression associated with the operand, or null in the case of an optional operand that is absent in the case
105 * of this particular expression
106 * @throws IllegalArgumentException if there cannot be an operand at this position
107 */
108
109 public Expression getOperandExpression(int n) {
110 try {
111 return operandArray[n].getChildExpression();
112 } catch (ArrayIndexOutOfBoundsException a) {
113 throw new IllegalArgumentException(a);
114 }
115 }
116
117 /**
118 * Return a collection containing all the operands. Generally there is a significance to the order
119 * of operands, which usually reflects the order in the raw textual expression.
120 * @return the collection of operands
121 */
122
123 public Iterable<Operand> operands() {
124 return Arrays.asList(operandArray);
125 }
126
127 public Iterable<Expression> operandExpressions() {
128 List<Expression> list = new ArrayList<Expression>(operandArray.length);
129 for (Operand o : operands()) {
130 list.add(o.getChildExpression());
131 }
132 return list;
133 }
134
135 /**
136 * Set the value of the operand with integer n.
137 * The method should implement a fast path for the case where the operand has
138 * not actually been changed.
139 *
140 * @param n identifies the expression to be set/replaced
141 * @param child the new subexpression
142 * @throws IllegalArgumentException if the value of n identifies no operand
143 */
144
145 public void setOperand(int n, Expression child) {
146 try {
147 if (operandArray[n].getChildExpression() != child) {
148 operandArray[n].setChildExpression(child);
149 }
150 } catch (ArrayIndexOutOfBoundsException a) {
151 throw new IllegalArgumentException();
152 }
153 }
154
155 /**
156 * Get the number of operands in the operand array
157 * @return the number of operands
158 */
159
160 public int getNumberOfOperands() {
161 return operandArray.length;
162 }
163
164 /**
165 * Utility method (doesn't really belong here) to test if every element of an array satisfies
166 * some condition
167 * @param args the array
168 * @param condition the condition
169 * @param <T> the type of the items in the array
170 * @return true if the condition is true for every item in the array
171 */
172
173 public static <T> boolean every(T[] args, Predicate<T> condition) {
174 for (T arg : args) {
175 if (!condition.test(arg)) {
176 return false;
177 }
178 }
179 return true;
180 }
181
182 /**
183 * Utility method (doesn't really belong here) to test if some element of an array satisfies
184 * some condition
185 *
186 * @param args the array
187 * @param condition the condition
188 * @param <T> the type of the items in the array
189 * @return true if the condition is true for every item in the array
190 */
191
192 public static <T> boolean some(T[] args, Predicate<T> condition) {
193 for (T arg : args) {
194 if (condition.test(arg)) {
195 return true;
196 }
197 }
198 return false;
199 }
200 }
201