net.sf.saxon.s9api.OccurrenceIndicator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Saxon-HE Show documentation
Show all versions of Saxon-HE Show documentation
The XSLT and XQuery Processor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2023 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.s9api;
import net.sf.saxon.expr.StaticProperty;
import net.sf.saxon.transpile.CSharpModifiers;
import net.sf.saxon.value.Cardinality;
/**
* Represents one of the possible occurrence indicators in a SequenceType. The four standard values are
* ONE (no occurrence indicator), ZERO_OR_ONE (?), ZERO_OR_MORE (*), ONE_OR_MORE (+). In addition the
* value ZERO is supported: this is used only in the type empty-sequence() which matches an empty sequence
* and nothing else.
*/
public enum OccurrenceIndicator {
ZERO, ZERO_OR_ONE, ZERO_OR_MORE, ONE, ONE_OR_MORE;
protected int getCardinality() {
OccurrenceIndicator indicator = this;
switch (indicator) {
case ZERO:
return StaticProperty.EMPTY;
case ZERO_OR_ONE:
return StaticProperty.ALLOWS_ZERO_OR_ONE;
case ZERO_OR_MORE:
return StaticProperty.ALLOWS_ZERO_OR_MORE;
case ONE:
return StaticProperty.ALLOWS_ONE;
case ONE_OR_MORE:
return StaticProperty.ALLOWS_ONE_OR_MORE;
default:
return StaticProperty.EMPTY;
}
}
protected static OccurrenceIndicator getOccurrenceIndicator(int cardinality) {
switch (cardinality) {
case StaticProperty.EMPTY:
return ZERO;
case StaticProperty.ALLOWS_ZERO_OR_ONE:
return ZERO_OR_ONE;
case StaticProperty.ALLOWS_ZERO_OR_MORE:
return ZERO_OR_MORE;
case StaticProperty.ALLOWS_ONE:
return ONE;
case StaticProperty.ALLOWS_ONE_OR_MORE:
return ONE_OR_MORE;
default:
return ZERO_OR_MORE;
}
}
/**
* Ask whether this occurrence indicator permits an empty sequence.
*
* @return true if the occurrence indicator is one of {@link #ZERO}, {@link #ZERO_OR_ONE},
* or {@link #ZERO_OR_MORE}
* @since 9.2
*/
public boolean allowsZero() {
return Cardinality.allowsZero(getCardinality());
}
/**
* Ask whether this occurrence indicator permits a sequence containing more than one item.
*
* @return true if the occurrence indicator is one of {@link #ZERO_OR_MORE} or {@link #ONE_OR_MORE}
* @since 9.2
*/
public boolean allowsMany() {
return Cardinality.allowsMany(getCardinality());
}
/**
* Ask whether this occurrence indicator permits a sequence of a given size
* @param size the number of items in the sequence
* @return true if this number of items is permitted by the occurrence indicator
* @since 12.0
*/
public boolean allows(int size) {
OccurrenceIndicator indicator = this;
switch (indicator) {
case ZERO:
return size == 0;
case ZERO_OR_ONE:
return size <= 1;
case ZERO_OR_MORE:
return true;
case ONE:
return size == 1;
case ONE_OR_MORE:
return size > 0;
default:
return false;
}
}
/**
* Ask whether one occurrence indicator subsumes another. Specifically,
* A.subsumes(B)
is true if every sequence that satisfies the occurrence
* indicator B also satisfies the occurrence indicator A.
*
* @param other The other occurrence indicator
* @return true if this occurrence indicator subsumes the other occurrence indicator
* @since 9.1
*/
public boolean subsumes(/*@NotNull*/ OccurrenceIndicator other) {
return Cardinality.subsumes(getCardinality(), other.getCardinality());
}
/**
* Return a string representation of the occurrence indicator: one of "*", "+", "?", "0" (exactly zero)
* or empty string (exactly one)
* @return a string representation of the occurrence indicator
* @since 9.5
*/
@CSharpModifiers(code={"public", "override"})
public String toString() {
OccurrenceIndicator indicator = this;
switch (indicator) {
case ZERO:
return "0";
case ZERO_OR_ONE:
return "?";
case ZERO_OR_MORE:
return "*";
case ONE:
return "";
case ONE_OR_MORE:
return "+";
default:
return "!!!";
}
}
}