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

org.javimmutable.collections.tree.LeafNode Maven / Gradle / Ivy

package org.javimmutable.collections.tree;

import org.javimmutable.collections.Func1;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.Holders;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.Proc2;
import org.javimmutable.collections.Proc2Throws;
import org.javimmutable.collections.Sum2;
import org.javimmutable.collections.Sum2Throws;
import org.javimmutable.collections.iterators.GenericIterator;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Comparator;

import static org.javimmutable.collections.MapEntry.entry;

/**
 * ¬ * A Node containing one value and no children.
 */
public class LeafNode
    extends AbstractNode
{
    private final K key;
    private final V value;

    LeafNode(@Nonnull K key,
             @Nullable V value)
    {
        this.key = key;
        this.value = value;
    }

    @Override
    boolean containsKey(@Nonnull Comparator comp,
                        @Nonnull K key)
    {
        return isMatch(comp, key);
    }

    @Override
    V get(@Nonnull Comparator comp,
          @Nonnull K key,
          V defaultValue)
    {
        return isMatch(comp, key) ? value : defaultValue;
    }

    @Nonnull
    @Override
    Holder find(@Nonnull Comparator comp,
                   @Nonnull K key)
    {
        if (isMatch(comp, key)) {
            return Holders.of(value);
        } else {
            return Holders.of();
        }
    }

    @Nonnull
    @Override
    Holder> findEntry(@Nonnull Comparator comp,
                                                @Nonnull K key)
    {
        if (isMatch(comp, key)) {
            return Holders.of(asEntry());
        } else {
            return Holders.of();
        }
    }

    @Override
    boolean isEmpty()
    {
        return false;
    }

    @Override
    int size()
    {
        return 1;
    }

    @Nonnull
    @Override
    AbstractNode assign(@Nonnull Comparator comp,
                              @Nonnull K key,
                              @Nullable V value)
    {
        if (isMatch(comp, key)) {
            if (this.value == value) {
                return this;
            } else {
                return new LeafNode<>(key, value);
            }
        } else {
            return ValueNode.instance(comp, this.key, this.value, key, value);
        }
    }

    @Nonnull
    @Override
    AbstractNode delete(@Nonnull Comparator comp,
                              @Nonnull K key)
    {
        if (isMatch(comp, key)) {
            return FringeNode.instance();
        } else {
            return this;
        }
    }

    @Nonnull
    @Override
    AbstractNode update(@Nonnull Comparator comp,
                              @Nonnull K key,
                              @Nonnull Func1, V> generator)
    {
        if (isMatch(comp, key)) {
            final V value = generator.apply(Holders.of(this.value));
            if (this.value == value) {
                return this;
            } else {
                return new LeafNode<>(key, value);
            }
        } else {
            final V value = generator.apply(Holders.of());
            return ValueNode.instance(comp, this.key, this.value, key, value);
        }
    }

    @Nonnull
    @Override
    DeleteResult deleteLeftmost()
    {
        return new DeleteResult<>(key, value, FringeNode.instance());
    }

    @Nonnull
    @Override
    DeleteResult deleteRightmost()
    {
        return new DeleteResult<>(key, value, FringeNode.instance());
    }

    @Override
    int depth()
    {
        return 1;
    }

    @Nonnull
    @Override
    K key()
    {
        return key;
    }

    @Nullable
    @Override
    V value()
    {
        return value;
    }

    @Nonnull
    @Override
    AbstractNode left()
    {
        return FringeNode.instance();
    }

    @Nonnull
    @Override
    AbstractNode right()
    {
        return FringeNode.instance();
    }

    @Override
    void checkInvariants(@Nonnull Comparator comp)
    {
    }

    @Override
    void forEach(@Nonnull Proc2 proc)
    {
        proc.apply(key, value);
    }

    @Override
     void forEachThrows(@Nonnull Proc2Throws proc)
        throws E
    {
        proc.apply(key, value);
    }

    @Override
     R reduce(R sum,
                 @Nonnull Sum2 proc)
    {
        return proc.apply(sum, key, value);
    }

    @Override
     R reduceThrows(R sum,
                                            @Nonnull Sum2Throws proc)
        throws E
    {
        return proc.apply(sum, key, value);
    }

    @Nullable
    @Override
    public GenericIterator.State> iterateOverRange(@Nullable GenericIterator.State> parent,
                                                                             int offset,
                                                                             int limit)
    {
        return GenericIterator.valueState(parent, asEntry());
    }

    @Override
    public int iterableSize()
    {
        return 1;
    }

    private boolean isMatch(@Nonnull Comparator comp,
                            @Nonnull K key)
    {
        return comp.compare(key, this.key) == 0;
    }

    private JImmutableMap.Entry asEntry()
    {
        return entry(key, value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy