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

com.amihaiemil.eoyaml.YamlNode Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2016-2024, Mihai Emil Andronache
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
package com.amihaiemil.eoyaml;

import com.amihaiemil.eoyaml.exceptions.YamlReadingException;

import javax.json.JsonValue;
import java.util.List;

/**
 * YAML node.
 * @author Mihai Andronache ([email protected])
 * @version $Id: fac97e1a6097d37a348b4e421ea7c38a6da1e6ce $
 * @since 1.0.0
 * @see Spec1.2/node
 */
public interface YamlNode extends Comparable {

    /**
     * Is this YamlNode empty?
     * @return True or false.
     */
    boolean isEmpty();

    /**
     * Comment referring to this Node.
     * @return Comment. If there is no comment, it will return
     *  an "empty" comment (an instance of Comment with empty-string value()).
     */
    Comment comment();

    /**
     * Type of the node.
     * @return Node type.
     */
    Node type();

    /**
     * Gives a String value of the node.
     * @return String value of the node.
     * @throws YamlReadingException If the node type is not
     *  Scalar.
     * @throws ClassCastException   If the node type couldn't
     *  defined correctly.
     */
    Scalar asScalar() throws YamlReadingException, ClassCastException;

    /**
     * Gives a Mapping value of the node.
     * @return Mapping value of the node.
     * @throws YamlReadingException If the node type is not
     *  Mapping.
     * @throws ClassCastException   If the node type couldn't
     *  defined correctly.
     */
    YamlMapping asMapping() throws YamlReadingException, ClassCastException;

    /**
     * Gives a Sequence value of the node.
     * @return Sequence value of the node.
     * @throws YamlReadingException If the node type is not
     *  Sequence.
     * @throws ClassCastException   If the node type couldn't
     *  defined correctly.
     */
    YamlSequence asSequence() throws YamlReadingException, ClassCastException;

    /**
     * Gives a Stream value of the node.
     * @return Stream value of the node.
     * @throws YamlReadingException If the node type is not
     *  Stream.
     * @throws ClassCastException   If the node type couldn't
     *  defined correctly.
     */
    YamlStream asStream() throws YamlReadingException, ClassCastException;

    /**
     * Gives the {@link T} instance from node class and node type.
     * @param clazz Requested a {@link YamlNode} class.
     * @param type Requested a {@link Node}.
     * @param  Requested a {@link YamlNode} class instance.
     * @return Requested a {@link YamlNode}.
     * @throws YamlReadingException If the type is not a {@link T}.
     * @throws ClassCastException   If the node type couldn't
     *  defined correctly.
     */
     T asClass(Class clazz, Node type)
        throws YamlReadingException, ClassCastException;

    /**
     * Return the children of this node. If the list is empty, it means it's a
     * terminal node.
     * @return List of children, never null.
     */
    List children();

    /**
     * Accept a YamlVisitor.
     * @param visitor Visitor.
     * @return Result of the visit.
     * @param  Type of the result.
     */
    default  T accept(YamlVisitor visitor) {
        return visitor.visitYamlNode(this);
    }

    /**
     * Turn this YamlNode to JsonValue.
     * @return JsonValue.
     */
    default JsonValue toJsonValue() {
        return this.accept(new YamlToJsonVisitor());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy