| 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.sort; |
| 9 | |
| 10 | import net.sf.saxon.expr.Operand; |
| 11 | import net.sf.saxon.expr.OperandRole; |
| 12 | import net.sf.saxon.expr.PseudoExpression; |
| 13 | import net.sf.saxon.expr.parser.RebindingMap; |
| 14 | import net.sf.saxon.trace.ExpressionPresenter; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Iterator; |
| 20 | import java.util.List; |
| 21 | |
| 22 | /** |
| 23 | * The class represents a list of sort key definitions in major-to-minor sort key order. It is not a true |
| 24 | * expression, because it cannot be evaluated, but it acts as a node in the expression tree, and is therefore |
| 25 | * classified as a pseudo-expression. |
| 26 | */ |
| 27 | public class SortKeyDefinitionList extends PseudoExpression implements Iterable<SortKeyDefinition> { |
| 28 | |
| 29 | private SortKeyDefinition[] sortKeyDefinitions; |
| 30 | |
| 31 | /** |
| 32 | * Create a list of sort key definitions |
| 33 | * @param sortKeyDefinitions the sort key definitions, supplied as an array |
| 34 | */ |
| 35 | |
| 36 | public SortKeyDefinitionList(SortKeyDefinition[] sortKeyDefinitions) { |
| 37 | this.sortKeyDefinitions = sortKeyDefinitions; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the operands of this SortKeyDefinitionList, treating it as a pseudo-expression. |
| 42 | * @return the operands: specifically, the contained sort key definitions, each of which |
| 43 | * is also treated as a pseudo-expression. |
| 44 | */ |
| 45 | |
| 46 | @Override |
| 47 | public Iterable<Operand> operands() { |
| 48 | List<Operand> list = new ArrayList<Operand>(size()); |
| 49 | for (SortKeyDefinition skd : sortKeyDefinitions) { |
| 50 | list.add(new Operand(this, skd, OperandRole.INSPECT)); |
| 51 | } |
| 52 | return list; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Ask whether the expression can be lifted out of a loop, assuming it has no dependencies |
| 57 | * on the controlling variable/focus of the loop |
| 58 | * @param forStreaming |
| 59 | */ |
| 60 | |
| 61 | public boolean isLiftable(boolean forStreaming) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Ask how many sort key definitions there are |
| 68 | * @return the number of sort key definitions in the list (always one or more) |
| 69 | */ |
| 70 | |
| 71 | public int size() { |
| 72 | return sortKeyDefinitions.length; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the i'th sort key definition, counting from zero |
| 77 | * @param i the index of the required sort key definition |
| 78 | * @return the required sort key definition |
| 79 | */ |
| 80 | |
| 81 | public SortKeyDefinition getSortKeyDefinition(int i) { |
| 82 | return sortKeyDefinitions[i]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get an iterator over the sort key definitions |
| 87 | * @return an iterator over the sort key definitions |
| 88 | */ |
| 89 | |
| 90 | public Iterator<SortKeyDefinition> iterator() { |
| 91 | return Arrays.asList(sortKeyDefinitions).iterator(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Copy this pseudo-expression |
| 96 | * @return a deep copy |
| 97 | * @param rebindings |
| 98 | */ |
| 99 | |
| 100 | @Override |
| 101 | public SortKeyDefinitionList copy(RebindingMap rebindings) { |
| 102 | SortKeyDefinition[] s2 = new SortKeyDefinition[sortKeyDefinitions.length]; |
| 103 | for (int i=0; i< sortKeyDefinitions.length; i++) { |
| 104 | s2[i] = sortKeyDefinitions[i].copy(rebindings); |
| 105 | } |
| 106 | return new SortKeyDefinitionList(s2); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 111 | * This method indicates which of these methods is provided directly. The other methods will always be available |
| 112 | * indirectly, using an implementation that relies on one of the other methods. |
| 113 | * |
| 114 | * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or |
| 115 | * {@link #PROCESS_METHOD} |
| 116 | */ |
| 117 | @Override |
| 118 | public int getImplementationMethod() { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void export(ExpressionPresenter out) throws XPathException { |
| 124 | for (SortKeyDefinition skd : sortKeyDefinitions) { |
| 125 | skd.export(out); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 |