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

de.mrapp.tries.datastructure.node.UnmodifiableNode Maven / Gradle / Ivy

/*
 * Copyright 2017 - 2018 Michael Rapp
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package de.mrapp.tries.datastructure.node;

import de.mrapp.tries.Node;
import de.mrapp.tries.NodeValue;
import de.mrapp.tries.Sequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map;

import static de.mrapp.util.Condition.ensureNotNull;

/**
 * An implementation of the interface {@link Node}, which forwards read-only method calls to an
 * encapsulated node and throws {@link UnsupportedOperationException}s when calling a method, which
 * attempts to change the node's state.
 *
 * @param    The type of the sequences, which correspond to the node's successors
 * @param  The type of the node's value
 * @author Michael Rapp
 * @since 1.0.0
 */
public class UnmodifiableNode implements
        Node {

    /**
     * The constant serial version UID.
     */
    private static final long serialVersionUID = -821100483227210297L;

    /**
     * The encapsulated node.
     */
    private final Node node;

    /**
     * Creates a new immutable node.
     *
     * @param node The node, which should be encapsulated, as an instance of the type {@link Node}.
     *             The node may not be null
     */
    public UnmodifiableNode(@NotNull final Node node) {
        ensureNotNull(node, "The node may not be null");
        this.node = node;
    }

    @Nullable
    @Override
    public final NodeValue getNodeValue() {
        return node.getNodeValue();
    }

    @Nullable
    @Override
    public final NodeValue setNodeValue(@Nullable final NodeValue nodeValue) {
        throw new UnsupportedOperationException();
    }

    @Override
    public final int getSuccessorCount() {

        return node.getSuccessorCount();
    }

    @Nullable
    @Override
    public final Node getSuccessor(@NotNull final KeyType key) {
        Node successor = node.getSuccessor(key);
        return successor != null ? new UnmodifiableNode<>(successor) : null;
    }

    @NotNull
    @Override
    public final KeyType getSuccessorKey(final int index) {
        return node.getSuccessorKey(index);
    }

    @NotNull
    @Override
    public final Node getSuccessor(final int index) {
        return node.getSuccessor(index);
    }

    @Override
    public final int indexOf(@NotNull final KeyType key) {
        return node.indexOf(key);
    }

    @NotNull
    @Override
    public final Node addSuccessor(@NotNull final KeyType key,
                                                       @Nullable final Node successor) {
        throw new UnsupportedOperationException();
    }

    @Override
    public final void removeSuccessor(@NotNull final KeyType key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public final int getSuccessorValueCount() {
        return node.getSuccessorValueCount();
    }

    @Override
    public final void increaseSuccessorValueCount(final int by) {
        throw new UnsupportedOperationException();
    }

    @Override
    public final void decreaseSuccessorValueCount(final int by) {
        throw new UnsupportedOperationException();
    }

    @Nullable
    @Override
    public final Map.Entry> getPredecessor() {
        Map.Entry> entry = node.getPredecessor();
        return entry != null ?
                new AbstractMap.SimpleImmutableEntry<>(entry.getKey(),
                        new UnmodifiableNode<>(entry.getValue())) : null;
    }

    @Override
    public final void setPredecessor(
            @Nullable final Map.Entry> predecessor) {
        throw new UnsupportedOperationException();
    }

    @NotNull
    @Override
    public final Iterator iterator() {
        return node.iterator();
    }

    @SuppressWarnings("unchecked")
    @Override
    public final Node clone() {
        try {
            return (Node) super.clone();
        } catch (CloneNotSupportedException e) {
            // Should never happen
            return null;
        }
    }

    @Override
    public final String toString() {
        return node.toString();
    }

    @Override
    public final int hashCode() {
        return node.hashCode();
    }

    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    @Override
    public final boolean equals(final Object obj) {
        return node.equals(obj);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy