| 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.value; |
| 9 | |
| 10 | import net.sf.saxon.om.GroundedValue; |
| 11 | import net.sf.saxon.om.Item; |
| 12 | import net.sf.saxon.tree.iter.EmptyIterator; |
| 13 | import net.sf.saxon.tree.iter.UnfailingIterator; |
| 14 | |
| 15 | import java.util.Collections; |
| 16 | |
| 17 | /** |
| 18 | * An EmptySequence object represents a sequence containing no members. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | public final class EmptySequence<T extends Item<?>> implements GroundedValue<T> { |
| 23 | |
| 24 | // This class has a single instance |
| 25 | /*@NotNull*/ private static EmptySequence THE_INSTANCE = new EmptySequence(); |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Private constructor: only the predefined instances of this class can be used |
| 30 | */ |
| 31 | |
| 32 | private EmptySequence() { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the implicit instance of this class |
| 37 | * |
| 38 | * @return the singular instances of this class: an empty sequence |
| 39 | */ |
| 40 | |
| 41 | @SuppressWarnings("unchecked") |
| 42 | public static <T extends Item<?>> EmptySequence<T> getInstance() { |
| 43 | return (EmptySequence<T>)THE_INSTANCE; |
| 44 | } |
| 45 | |
| 46 | public String getStringValue() { |
| 47 | return ""; |
| 48 | } |
| 49 | |
| 50 | public Collections a; |
| 51 | |
| 52 | public CharSequence getStringValueCS() { |
| 53 | return ""; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the first item in the sequence. |
| 58 | * |
| 59 | * @return the first item in the sequence if there is one, or null if the sequence |
| 60 | * is empty |
| 61 | */ |
| 62 | public T head() { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Return an iteration over the sequence |
| 68 | */ |
| 69 | |
| 70 | /*@NotNull*/ |
| 71 | public UnfailingIterator<T> iterate() { |
| 72 | return EmptyIterator.emptyIterator(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Return the value in the form of an Item |
| 77 | * |
| 78 | * @return the value in the form of an Item |
| 79 | */ |
| 80 | |
| 81 | /*@Nullable*/ |
| 82 | public Item asItem() { |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the length of the sequence |
| 88 | * |
| 89 | * @return always 0 for an empty sequence |
| 90 | */ |
| 91 | |
| 92 | public final int getLength() { |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Is this expression the same as another expression? |
| 98 | * |
| 99 | * @throws ClassCastException if the values are not comparable |
| 100 | */ |
| 101 | |
| 102 | public boolean equals(/*@NotNull*/ Object other) { |
| 103 | if (!(other instanceof GroundedValue && ((GroundedValue)other).getLength() == 0)) { |
| 104 | throw new ClassCastException("Cannot compare " + other.getClass() + " to empty sequence"); |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | public int hashCode() { |
| 110 | return 42; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the effective boolean value - always false |
| 115 | */ |
| 116 | |
| 117 | public boolean effectiveBooleanValue() { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Get the n'th item in the sequence (starting from 0). This is defined for all |
| 124 | * Values, but its real benefits come for a sequence Value stored extensionally |
| 125 | * (or for a MemoClosure, once all the values have been read) |
| 126 | * |
| 127 | * @param n position of the required item, counting from zero. |
| 128 | * @return the n'th item in the sequence, where the first item in the sequence is |
| 129 | * numbered zero. If n is negative or >= the length of the sequence, returns null. |
| 130 | */ |
| 131 | |
| 132 | /*@Nullable*/ |
| 133 | public T itemAt(int n) { |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get a subsequence of the value |
| 139 | * |
| 140 | * @param min the index of the first item to be included in the result, counting from zero. |
| 141 | * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty |
| 142 | * sequence is returned |
| 143 | * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to |
| 144 | * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence |
| 145 | * is returned. If the value goes off the end of the sequence, the result returns items up to the end |
| 146 | * of the sequence |
| 147 | * @return the required subsequence. If min is |
| 148 | */ |
| 149 | |
| 150 | /*@NotNull*/ |
| 151 | public GroundedValue<T> subsequence(int min, int length) { |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns a string representation of the object. |
| 157 | */ |
| 158 | |
| 159 | /*@NotNull*/ |
| 160 | public String toString() { |
| 161 | return "()"; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be |
| 166 | * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance |
| 167 | * of AtomicValue. If the value is a single item of any other kind, the result will be an instance |
| 168 | * of SingletonItem. Otherwise, the result will typically be unchanged. |
| 169 | * |
| 170 | * @return the simplified sequence |
| 171 | */ |
| 172 | public GroundedValue<T> reduce() { |
| 173 | return this; |
| 174 | } |
| 175 | } |
| 176 |