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

com.jongsoft.lang.collection.Tree Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright 2016-2019 Jong Soft.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jongsoft.lang.collection;

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

import com.jongsoft.lang.control.Optional;

/**
 *
 * @param  the entity type contained in the tree
 * @since 0.0.5
 */
public interface Tree extends Traversable {

    /**
     * The node collection is a set of tree elements each containing exactly one value.
     *
     * @param  the entity type contained in the tree
     */
    interface NodeCollection extends Collection> {

        Tree get(int i);

    }

    Tree appendChild(String label, T child);

    /**
     * Fetch the collection of child tree elements contained within this tree.
     *
     * @return  the child collection
     */
    NodeCollection children();

    default Optional> getChild(String label) {
        return children().first(c -> Objects.equals(c.label(), label));
    }

    /**
     * Indicates if the tree is a leaf node.
     * This will be {@code true} when the Tree has not children.
     *
     * @return true if {@link #children()} is empty
     */
    default boolean isLeaf() {
        return children().isEmpty();
    }

    /**
     * Indicates if the current tree node is the root of the tree.
     * This call yields the same result as validating if {@linkplain #parent()} equals {@code null}.
     *
     * @return  true if this tree node is the root
     */
    default boolean isRoot() {
        return parent() == null;
    }

    /**
     * Fetch the label that belongs to the current tree.
     *
     * @return  the string label of the tree
     */
    String label();

     Tree map(Function mapper);

    @Override
    Tree orElse(Iterable other);

    @Override
    Tree orElse(Supplier> supplier);

    /**
     * Get the parent tree node for this tree node.
     *
     * @return  the parent, or {@code null} if no parent exists
     */
    Tree parent();

}