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

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

Go to download

Library providing immutable/persistent collection classes for Java. While collections are immutable they provide methods for adding and removing values by creating new modified copies of themselves. Each copy shares almost all of its structure with other copies to minimize memory consumption.

There is a newer version: 3.2.1
Show newest version
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2014, 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.Cursor;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.common.AbstractJImmutableMap;
import org.javimmutable.collections.common.Conditions;
import org.javimmutable.collections.common.MutableDelta;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Immutable
public class JImmutableTreeMap
        extends AbstractJImmutableMap
{
    @SuppressWarnings("unchecked")
    private static final JImmutableTreeMap EMPTY = new JImmutableTreeMap(new ComparableComparator());

    private final Comparator comparator;
    private final TreeNode root;
    private final int size;

    /**
     * Constructs an empty map using the specified Comparator.  Note that the Comparator MUST BE IMMUTABLE.
     * The Comparator will be retained and used throughout the life of the map and its offspring and will
     * be aggressively shared so it is imperative that the Comparator be completely immutable.
     *
     * @param comparator
     */
    private JImmutableTreeMap(Comparator comparator)
    {
        this(comparator, EmptyNode.of(), 0);
    }

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

    /**
     * Constructs an empty map using the specified Comparator.  Note that the Comparator MUST BE IMMUTABLE.
     * The Comparator will be retained and used throughout the life of the map and its offspring and will
     * be aggressively shared so it is imperative that the Comparator be completely immutable.
     *
     * @param comparator
     */
    public static  JImmutableTreeMap of(Comparator comparator)
    {
        return new JImmutableTreeMap(comparator);
    }

    /**
     * Constructs a new map containing the same key/value pairs as map using a ComparableComparator
     * to compare the keys.
     *
     * @param map
     * @param 
     * @param 
     * @return
     */
    public static , V> JImmutableTreeMap of(Map map)
    {
        JImmutableTreeMap answer = of();
        for (Map.Entry entry : map.entrySet()) {
            answer = answer.assign(entry.getKey(), entry.getValue());
        }
        return answer;
    }

    private JImmutableTreeMap(Comparator comparator,
                              TreeNode root,
                              int size)
    {
        this.comparator = comparator;
        this.root = root;
        this.size = size;
    }

    @Override
    public V getValueOr(K key,
                        V defaultValue)
    {
        if (key == null) {
            throw new NullPointerException();
        }
        return root.getValueOr(comparator, key, defaultValue);
    }

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

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

    @Nonnull
    @Override
    public JImmutableTreeMap assign(@Nonnull K key,
                                          V value)
    {
        Conditions.stopNull(key);
        MutableDelta sizeDelta = new MutableDelta();
        TreeNode newRoot = root.assign(comparator, key, value, sizeDelta);
        if (newRoot == root) {
            return this;
        } else {
            return create(newRoot, sizeDelta.getValue());
        }
    }

    @Nonnull
    @Override
    public JImmutableTreeMap delete(@Nonnull K key)
    {
        Conditions.stopNull(key);
        MutableDelta sizeDelta = new MutableDelta();
        TreeNode newRoot = root.delete(comparator, key, sizeDelta);
        if (newRoot == root) {
            return this;
        } else {
            return create(newRoot, sizeDelta.getValue());
        }
    }

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

    @Nonnull
    @Override
    public JImmutableTreeMap deleteAll()
    {
        return of(comparator);
    }

    @Override
    @Nonnull
    public Cursor> cursor()
    {
        return root.cursor();
    }

    public List getKeysList()
    {
        List keys = new LinkedList();
        for (Entry entry : this) {
            keys.add(entry.getKey());
        }
        return Collections.unmodifiableList(keys);
    }

    public void verifyDepthsMatch()
    {
        root.verifyDepthsMatch();
    }

    public Comparator getComparator()
    {
        return comparator;
    }

    private JImmutableTreeMap create(TreeNode root,
                                           int sizeDelta)
    {
        return new JImmutableTreeMap(comparator, root, size + sizeDelta);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy