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

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

///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2023, Burton Computer Corporation
// 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 Burton Computer Corporation 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 org.javimmutable.collections.tree;

import org.javimmutable.collections.Func1;
import org.javimmutable.collections.IMap;
import org.javimmutable.collections.IMapBuilder;
import org.javimmutable.collections.IMapEntry;
import org.javimmutable.collections.Maybe;
import org.javimmutable.collections.Proc2;
import org.javimmutable.collections.Proc2Throws;
import org.javimmutable.collections.SplitableIterator;
import org.javimmutable.collections.Sum2;
import org.javimmutable.collections.Sum2Throws;
import org.javimmutable.collections.common.AbstractMap;
import org.javimmutable.collections.common.Conditions;
import org.javimmutable.collections.common.StreamConstants;
import org.javimmutable.collections.serialization.TreeMapProxy;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.io.Serializable;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Immutable
public class TreeMap
    extends AbstractMap
    implements Serializable
{
    @SuppressWarnings({"unchecked", "rawtypes"})
    private static final TreeMap EMPTY = new TreeMap(ComparableComparator.of(), FringeNode.instance());

    private static final long serialVersionUID = -121805;

    private final Comparator comparator;
    private final AbstractNode root;

    TreeMap(@Nonnull Comparator comparator,
            @Nonnull AbstractNode root)
    {
        this.comparator = comparator;
        this.root = root;
    }

    @SuppressWarnings("unchecked")
    @Nonnull
    public static , V> TreeMap of()
    {
        return EMPTY;
    }

    @Nonnull
    public static  TreeMap of(@Nonnull Comparator comparator)
    {
        return new TreeMap<>(comparator, FringeNode.instance());
    }

    @Nonnull
    public static , V> IMapBuilder builder()
    {
        return new TreeMapBuilder<>(ComparableComparator.of());
    }

    @Nonnull
    public static  IMapBuilder builder(@Nonnull Comparator comparator)
    {
        return new TreeMapBuilder<>(comparator);
    }

    @Nonnull
    @Override
    public IMapBuilder mapBuilder()
    {
        return new TreeMapBuilder<>(comparator);
    }

    @Nonnull
    public static , V> Collector, ?, IMap> createMapCollector()
    {
        return createMapCollector(ComparableComparator.of());
    }

    @Nonnull
    public static  Collector, ?, IMap> createMapCollector(@Nonnull Comparator comparator)
    {
        return Collector., IMapBuilder, IMap>of(() -> new TreeMapBuilder<>(comparator),
                                                                            (b, v) -> b.add(v),
                                                                            (b1, b2) -> b1.add(b2),
                                                                            b -> b.build(),
                                                                            Collector.Characteristics.CONCURRENT);
    }

    @Override
    public V getValueOr(K key,
                        V defaultValue)
    {
        Conditions.stopNull(key);
        return root.get(comparator, key, defaultValue);
    }

    @Nonnull
    @Override
    public Maybe find(@Nonnull K key)
    {
        Conditions.stopNull(key);
        return root.find(comparator, key);
    }

    @Nonnull
    @Override
    public Maybe> findEntry(@Nonnull K key)
    {
        Conditions.stopNull(key);
        return root.findEntry(comparator, key);
    }

    @Nonnull
    @Override
    public TreeMap assign(@Nonnull K key,
                                V value)
    {
        Conditions.stopNull(key);
        return create(root.assign(comparator, key, value));
    }

    @Nonnull
    @Override
    public TreeMap update(@Nonnull K key,
                                @Nonnull Func1, V> generator)
    {
        Conditions.stopNull(key);
        return create(root.update(comparator, key, generator));
    }

    @Nonnull
    @Override
    public TreeMap delete(@Nonnull K key)
    {
        Conditions.stopNull(key);
        final AbstractNode newRoot = root.delete(comparator, key);
        if (newRoot.isEmpty()) {
            return deleteAll();
        } else {
            return create(newRoot);
        }
    }

    @Override
    public int size()
    {
        return root.size();
    }

    @SuppressWarnings("unchecked")
    @Nonnull
    @Override
    public TreeMap deleteAll()
    {
        return (comparator == ComparableComparator.of()) ? EMPTY : new TreeMap<>(comparator, FringeNode.instance());
    }

    @Override
    public int getSpliteratorCharacteristics()
    {
        return StreamConstants.SPLITERATOR_ORDERED;
    }

    @Nonnull
    @Override
    public SplitableIterator> iterator()
    {
        return root.iterator();
    }

    @Override
    public void forEach(@Nonnull Proc2 proc)
    {
        root.forEach(proc);
    }

    @Override
    public  void forEachThrows(@Nonnull Proc2Throws proc)
        throws E
    {
        root.forEachThrows(proc);
    }

    @Override
    public  R reduce(R sum,
                        @Nonnull Sum2 proc)
    {
        return root.reduce(sum, proc);
    }

    @Override
    public  R reduceThrows(R sum,
                                                   @Nonnull Sum2Throws proc)
        throws E
    {
        return root.reduceThrows(sum, proc);
    }

    @Override
    public void checkInvariants()
    {
        root.checkInvariants(comparator);
    }

    @Nonnull
    public Comparator getComparator()
    {
        return comparator;
    }

    @Nonnull
    List getKeysList()
    {
        return keys().stream().collect(Collectors.toList());
    }

    @Nonnull
    private TreeMap create(AbstractNode newRoot)
    {
        if (newRoot == root) {
            return this;
        } else {
            return new TreeMap<>(comparator, newRoot);
        }
    }

    private Object writeReplace()
    {
        return new TreeMapProxy(this);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy