All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.value.EmptySequence Maven / Gradle / Ivy

There is a newer version: 12.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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.value;

import net.sf.saxon.om.GroundedValue;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.str.EmptyUnicodeString;
import net.sf.saxon.str.UnicodeString;
import net.sf.saxon.tree.iter.EmptyIterator;

/**
 * An EmptySequence object represents a sequence containing no members.
 * @since 9.5.  Generified in 9.9.  Generification reverted in 11.0.
 */


public final class EmptySequence implements GroundedValue {

    // This class has a single instance
    private static final EmptySequence THE_INSTANCE = new EmptySequence();


    /**
     * Private constructor: only the predefined instances of this class can be used
     */

    private EmptySequence() {
    }

    /**
     * Get the implicit instance of this class
     *
     * @return the singular instances of this class: an empty sequence
     */

    public static EmptySequence getInstance() {
        return THE_INSTANCE;
    }

    @Override
    public UnicodeString getUnicodeStringValue() {
        return EmptyUnicodeString.getInstance();
    }

    @Override
    public String getStringValue() {
        return "";
    }

    /**
     * Get the first item in the sequence.
     *
     * @return the first item in the sequence if there is one, or null if the sequence
     *         is empty
     */
    @Override
    public Item head() {
        return null;
    }

    /**
     * Return an iteration over the sequence
     * @return an empty iterator
     */

    /*@NotNull*/
    @Override
    public SequenceIterator iterate() {
        return EmptyIterator.getInstance();
    }

    /**
     * Get the length of the sequence
     *
     * @return always 0 for an empty sequence
     */

    @Override
    public final int getLength() {
        return 0;
    }

    /**
     * Is this expression the same as another expression?
     *
     * @throws ClassCastException if the values are not comparable
     */

    public boolean equals(/*@NotNull*/ Object other) {
        if (!(other instanceof GroundedValue && ((GroundedValue)other).getLength() == 0)) {
            throw new ClassCastException("Cannot compare " + other.getClass() + " to empty sequence");
        }
        return true;
    }

    public int hashCode() {
        return 42;
    }

    /**
     * Get the effective boolean value - always false
     */

    @Override
    public boolean effectiveBooleanValue() {
        return false;
    }


    /**
     * Get the n'th item in the sequence (starting from 0). This is defined for all
     * Values, but its real benefits come for a sequence Value stored extensionally
     * (or for a MemoClosure, once all the values have been read)
     *
     * @param n position of the required item, counting from zero.
     * @return the n'th item in the sequence, where the first item in the sequence is
     *         numbered zero. If n is negative or >= the length of the sequence, returns null.
     */

    /*@Nullable*/
    @Override
    public Item itemAt(int n) {
        return null;
    }

    /**
     * Get a subsequence of the value
     *
     * @param min    the index of the first item to be included in the result, counting from zero.
     *               A negative value is taken as zero. If the value is beyond the end of the sequence, an empty
     *               sequence is returned
     * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to
     *               get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence
     *               is returned. If the value goes off the end of the sequence, the result returns items up to the end
     *               of the sequence
     * @return the required subsequence. If min is
     */

    /*@NotNull*/
    @Override
    public GroundedValue subsequence(int min, int length) {
        return this;
    }

    /**
     * Returns a string representation of the object.
     */

    /*@NotNull*/
    public String toString() {
        return "()";
    }

    /**
     * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be
     * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance
     * of AtomicValue. If the value is a single item of any other kind, the result will be an instance
     * of SingletonItem. Otherwise, the result will typically be unchanged.
     *
     * @return the simplified sequence
     */
    @Override
    public GroundedValue reduce() {
        return this;
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy