| 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.om; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 11 | import net.sf.saxon.trans.XPathException; |
| 12 | import net.sf.saxon.tree.iter.SingletonIterator; |
| 13 | import net.sf.saxon.tree.iter.UnfailingIterator; |
| 14 | import net.sf.saxon.value.EmptySequence; |
| 15 | |
| 16 | /** |
| 17 | * A value that is a sequence containing zero or one items. |
| 18 | */ |
| 19 | |
| 20 | public class ZeroOrOne<T extends Item<?>> implements GroundedValue<T> { |
| 21 | |
| 22 | private T item; // may be null, to represent an empty sequence |
| 23 | |
| 24 | private static ZeroOrOne<Item<?>> EMPTY = new ZeroOrOne<>(null); |
| 25 | |
| 26 | /** |
| 27 | * Return the instance of ZeroOrOne that represents the empty sequence |
| 28 | * @return a representation of the empty sequence that satisfies the type ZeroOrOne |
| 29 | */ |
| 30 | |
| 31 | public static <T extends Item<?>> ZeroOrOne<T> empty() { |
| 32 | return (ZeroOrOne<T>) EMPTY; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Create a sequence containing zero or one items |
| 37 | * |
| 38 | * @param item The item to be contained in the sequence, or null if the sequence |
| 39 | * is to be empty |
| 40 | */ |
| 41 | |
| 42 | public ZeroOrOne(T item) { |
| 43 | this.item = item; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get the string value of this sequence. The string value of an item is the result of applying the string() |
| 48 | * function. The string value of an empty sequence is the zero-length string. |
| 49 | * |
| 50 | * @return the string value of the sequence. |
| 51 | */ |
| 52 | |
| 53 | public CharSequence getStringValueCS() { |
| 54 | return item == null ? "" : item.getStringValueCS(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Convert the value to a string, using the serialization rules. |
| 59 | * For atomic values this is the same as a cast; for sequence values |
| 60 | * it gives a space-separated list. For QNames and NOTATIONS, or lists |
| 61 | * containing them, it fails. |
| 62 | */ |
| 63 | |
| 64 | /*@NotNull*/ |
| 65 | public String getStringValue() { |
| 66 | return item == null ? "" : item.getStringValue(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the first item in the sequence. |
| 71 | * |
| 72 | * @return the first item in the sequence if there is one, or null if the sequence |
| 73 | * is empty |
| 74 | */ |
| 75 | public T head() { |
| 76 | return item; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the length of the sequence |
| 81 | */ |
| 82 | |
| 83 | public int getLength() { |
| 84 | return item == null ? 0 : 1; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the n'th item in the sequence (starting from 0). This is defined for all |
| 89 | * SequenceValues, but its real benefits come for a SequenceValue stored extensionally |
| 90 | * (or for a MemoClosure, once all the values have been read) |
| 91 | * @param n the index of the required item, with 0 representing the first item in the sequence |
| 92 | * @return the n'th item if it exists, or null otherwise |
| 93 | */ |
| 94 | |
| 95 | /*@Nullable*/ |
| 96 | public T itemAt(int n) { |
| 97 | if (n == 0 && item != null) { |
| 98 | return item; |
| 99 | } else { |
| 100 | return null; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Get a subsequence of the value |
| 107 | * |
| 108 | * @param start the index of the first item to be included in the result, counting from zero. |
| 109 | * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty |
| 110 | * sequence is returned |
| 111 | * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to |
| 112 | * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence |
| 113 | * is returned. If the value goes off the end of the sequence, the result returns items up to the end |
| 114 | * of the sequence |
| 115 | * @return the required subsequence. If min is |
| 116 | */ |
| 117 | |
| 118 | /*@NotNull*/ |
| 119 | public GroundedValue<T> subsequence(int start, int length) { |
| 120 | if (item != null && start <= 0 && start + length > 0) { |
| 121 | return this; |
| 122 | } else { |
| 123 | return EmptySequence.getInstance(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Return an enumeration of this nodeset value. |
| 129 | */ |
| 130 | |
| 131 | /*@NotNull*/ |
| 132 | public UnfailingIterator<T> iterate() { |
| 133 | return SingletonIterator.makeIterator(item); // handles null properly |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get the effective boolean value |
| 138 | */ |
| 139 | |
| 140 | public boolean effectiveBooleanValue() throws XPathException { |
| 141 | return ExpressionTool.effectiveBooleanValue(item); // handles null properly |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns a string representation of the object (used only for diagnostics). |
| 146 | * |
| 147 | * @return a string representation of the object. |
| 148 | */ |
| 149 | @Override |
| 150 | public String toString() { |
| 151 | return item==null ? "null" : item.toString(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be |
| 156 | * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance |
| 157 | * of AtomicValue. If the value is a single item of any other kind, the result will be an instance |
| 158 | * of SingletonItem. Otherwise, the result will typically be unchanged. |
| 159 | * |
| 160 | * @return the simplified sequence |
| 161 | */ |
| 162 | public GroundedValue<T> reduce() { |
| 163 | if (item == null) { |
| 164 | return EmptySequence.getInstance(); |
| 165 | } else { |
| 166 | return (Item<T>) item; |
| 167 | } |
| 168 | } |
| 169 | } |