| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.expr.StaticProperty; |
| 11 | import net.sf.saxon.value.Cardinality; |
| 12 | |
| 13 | /** |
| 14 | * Represents one of the possible occurrence indicators in a SequenceType. The four standard values are |
| 15 | * ONE (no occurrence indicator), ZERO_OR_ONE (?), ZERO_OR_MORE (*), ONE_OR_MORE (+). In addition the |
| 16 | * value ZERO is supported: this is used only in the type empty-sequence() which matches an empty sequence |
| 17 | * and nothing else. |
| 18 | */ |
| 19 | public enum OccurrenceIndicator { |
| 20 | ZERO, ZERO_OR_ONE, ZERO_OR_MORE, ONE, ONE_OR_MORE; |
| 21 | |
| 22 | protected int getCardinality() { |
| 23 | switch (this) { |
| 24 | case ZERO: |
| 25 | return StaticProperty.EMPTY; |
| 26 | case ZERO_OR_ONE: |
| 27 | return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 28 | case ZERO_OR_MORE: |
| 29 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 30 | case ONE: |
| 31 | return StaticProperty.ALLOWS_ONE; |
| 32 | case ONE_OR_MORE: |
| 33 | return StaticProperty.ALLOWS_ONE_OR_MORE; |
| 34 | default: |
| 35 | return StaticProperty.EMPTY; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | protected static OccurrenceIndicator getOccurrenceIndicator(int cardinality) { |
| 40 | switch (cardinality) { |
| 41 | case StaticProperty.EMPTY: |
| 42 | return ZERO; |
| 43 | case StaticProperty.ALLOWS_ZERO_OR_ONE: |
| 44 | return ZERO_OR_ONE; |
| 45 | case StaticProperty.ALLOWS_ZERO_OR_MORE: |
| 46 | return ZERO_OR_MORE; |
| 47 | case StaticProperty.ALLOWS_ONE: |
| 48 | return ONE; |
| 49 | case StaticProperty.ALLOWS_ONE_OR_MORE: |
| 50 | return ONE_OR_MORE; |
| 51 | default: |
| 52 | return ZERO_OR_MORE; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Ask whether this occurrence indicator permits an empty sequence. |
| 58 | * |
| 59 | * @return true if the occurrence indicator is one of {@link #ZERO}, {@link #ZERO_OR_ONE}, |
| 60 | * or {@link #ZERO_OR_MORE} |
| 61 | * @since 9.2 |
| 62 | */ |
| 63 | |
| 64 | public boolean allowsZero() { |
| 65 | return Cardinality.allowsZero(getCardinality()); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Ask whether this occurrence indicator permits a sequence containing more than one item. |
| 70 | * |
| 71 | * @return true if the occurrence indicator is one of {@link #ZERO_OR_MORE} or {@link #ONE_OR_MORE} |
| 72 | * @since 9.2 |
| 73 | */ |
| 74 | |
| 75 | public boolean allowsMany() { |
| 76 | return Cardinality.allowsMany(getCardinality()); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Ask whether one occurrence indicator subsumes another. Specifically, |
| 81 | * <code>A.subsumes(B)</code> is true if every sequence that satisfies the occurrence |
| 82 | * indicator B also satisfies the occurrence indicator A. |
| 83 | * |
| 84 | * @param other The other occurrence indicator |
| 85 | * @return true if this occurrence indicator subsumes the other occurrence indicator |
| 86 | * @since 9.1 |
| 87 | */ |
| 88 | |
| 89 | public boolean subsumes(/*@NotNull*/ OccurrenceIndicator other) { |
| 90 | return Cardinality.subsumes(getCardinality(), other.getCardinality()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return a string representation of the occurrence indicator: one of "*", "+", "?", "0" (exactly zero) |
| 95 | * or empty string (exactly one) |
| 96 | * @return a string representation of the occurrence indicator |
| 97 | * @since 9.5 |
| 98 | */ |
| 99 | |
| 100 | public String toString() { |
| 101 | switch (this) { |
| 102 | case ZERO: |
| 103 | return "0"; |
| 104 | case ZERO_OR_ONE: |
| 105 | return "?"; |
| 106 | case ZERO_OR_MORE: |
| 107 | return "*"; |
| 108 | case ONE: |
| 109 | return ""; |
| 110 | case ONE_OR_MORE: |
| 111 | return "+"; |
| 112 | default: |
| 113 | return "!!!"; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |
| 118 |