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) 2013, 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.Holders;
import org.javimmutable.collections.Insertable;
import org.javimmutable.collections.common.AbstractJImmutableMap;
import org.javimmutable.collections.common.IteratorAdaptor;
import org.javimmutable.collections.common.MapAdaptor;
import org.javimmutable.collections.cursors.EmptyCursor;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

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;

    public JImmutableTreeMap(Comparator comparator)
    {
        this(comparator, null, 0);
    }

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

    public static  JImmutableTreeMap of(Comparator comparator)
    {
        return new JImmutableTreeMap(comparator);
    }

    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 get(K key)
    {
        return find(key).getValueOrNull();
    }

    @Override
    public Holder find(K key)
    {
        if (key == null) {
            throw new NullPointerException();
        }
        if (root == null) {
            return Holders.of();
        } else {
            return root.find(comparator, key);
        }
    }

    @Override
    public Holder> findEntry(K key)
    {
        if (key == null) {
            throw new NullPointerException();
        }
        if (root == null) {
            return Holders.of();
        } else {
            return root.findEntry(comparator, key);
        }
    }

    @Override
    public JImmutableTreeMap assign(K key,
                                          V value)
    {
        if (key == null) {
            throw new NullPointerException();
        }
        if (root == null) {
            return create(new LeafNode(key, value), 1);
        } else {
            UpdateResult result = root.update(comparator, key, value);
            switch (result.type) {
            case UNCHANGED:
                return this;

            case INPLACE:
                return create(result.newNode, result.sizeDelta);

            case SPLIT:
                return create(new TwoNode(result.newNode,
                                                result.extraNode,
                                                result.newNode.getMaxKey(),
                                                result.extraNode.getMaxKey()),
                              result.sizeDelta);
            }
            throw new RuntimeException();
        }
    }

    @Override
    public JImmutableTreeMap delete(K key)
    {
        if (key == null) {
            throw new NullPointerException();
        }

        if (root == null) {
            return this;
        }

        DeleteResult result = root.delete(comparator, key);
        if (result.type == DeleteResult.Type.UNCHANGED) {
            return this;
        } else {
            return create(result.node, -1);
        }
    }

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

    @Override
    public Map getMap()
    {
        return MapAdaptor.of(this);
    }

    /**
     * Adds the key/value pair to this map.  Any value already existing for the specified key
     * is replaced with the new value.
     *
     * @param e
     * @return
     */
    @Override
    public Insertable> insert(Entry e)
    {
        return assign(e.getKey(), e.getValue());
    }

    @Override
    public Iterator> iterator()
    {
        return IteratorAdaptor.of(cursor());
    }

    @Override
    public Cursor> cursor()
    {
        return (root == null) ? EmptyCursor.>of() : root.cursor();
    }

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

    public void verifyDepthsMatch()
    {
        if (root != null) {
            root.verifyDepthsMatch();
        }
    }

    public Class getComparatorClass()
    {
        return comparator.getClass();
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy