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

org.javimmutable.collections.hash.JImmutableHashMap Maven / Gradle / Ivy

///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2019, 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.hash;

import org.javimmutable.collections.Func1;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.Holders;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.MapEntry;
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.AbstractJImmutableMap;
import org.javimmutable.collections.common.CollisionMap;
import org.javimmutable.collections.hash.map.MapBuilder;
import org.javimmutable.collections.hash.map.MapEmptyNode;
import org.javimmutable.collections.hash.map.MapNode;
import org.javimmutable.collections.list.ListCollisionMap;
import org.javimmutable.collections.serialization.JImmutableHashMapProxy;
import org.javimmutable.collections.tree.TreeCollisionMap;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;
import java.io.Serializable;
import java.util.stream.Collector;

@Immutable
public class JImmutableHashMap
    extends AbstractJImmutableMap
    implements Serializable
{
    // we only need one instance of the transformations object
    @SuppressWarnings({"rawtypes"})
    static final CollisionMap LIST_COLLISION_MAP = ListCollisionMap.instance();

    // we only need one instance of the transformations object
    @SuppressWarnings({"rawtypes"})
    static final CollisionMap TREE_COLLISION_MAP = TreeCollisionMap.instance();

    // this is safe since the transformations object works for any possible K and V
    @SuppressWarnings({"rawtypes", "unchecked"})
    static final JImmutableHashMap LIST_EMPTY = new JImmutableHashMap(MapEmptyNode.of(), LIST_COLLISION_MAP);

    // this is safe since the transformations object works for any possible K and V
    @SuppressWarnings({"rawtypes", "unchecked"})
    static final JImmutableHashMap TREE_EMPTY = new JImmutableHashMap(MapEmptyNode.of(), TREE_COLLISION_MAP);

    private static final long serialVersionUID = -121805;

    private final MapNode root;
    private final CollisionMap collisionMap;

    private JImmutableHashMap(MapNode root,
                              CollisionMap collisionMap)
    {
        this.root = root;
        this.collisionMap = collisionMap;
    }

    /**
     * Returns an empty hash map.  The empty map will automatically select a collision handling strategy
     * on the first call to assign() based on the key for that call.  For this reason all keys used for a
     * given map must either implement or not implement Comparable.  If some keys implement it and some do
     * not the collision handling code will likely fail due to a class cast exception or a method
     * not defined exception.
     */
    @SuppressWarnings("unchecked")
    public static  EmptyHashMap of()
    {
        return EmptyHashMap.INSTANCE;
    }

    /**
     * Returns an empty map using the appropriate collision handling strategy for keys of the given
     * class.  All keys used with that map should derive from the specified class to avoid runtime
     * problems with incompatible keys.
     */
    @SuppressWarnings("unchecked")
    public static  JImmutableMap of(Class klass)
    {
        return klass.isAssignableFrom(Comparable.class) ? TREE_EMPTY : LIST_EMPTY;
    }

    /**
     * Returns an empty map using the appropriate collision handling strategy for the given key's
     * class.  All keys used with that map should derive from the specified key's class to avoid runtime
     * problems with incompatible keys.
     */
    @SuppressWarnings("unchecked")
    public static  JImmutableMap forKey(K key)
    {
        return (key instanceof Comparable) ? TREE_EMPTY : LIST_EMPTY;
    }

    /**
     * Returns an empty map using linked lists for handling hash code collisions.  This is safe
     * for any type of key but is slower when many keys have the same hash code.
     */
    @SuppressWarnings("unchecked")
    public static  JImmutableMap usingList()
    {
        return (JImmutableMap)LIST_EMPTY;
    }

    /**
     * Returns an empty map using linked lists for handling hash code collisions.  This is faster
     * than the list based collision handling but depends on all keys implementing Comparable and
     * being able to compare themselves to all other keys.
     */
    @SuppressWarnings("unchecked")
    public static , V> JImmutableMap usingTree()
    {
        return (JImmutableMap)TREE_EMPTY;
    }

    public static  JImmutableMap.Builder builder()
    {
        return new Builder<>();
    }

    @Nonnull
    @Override
    public JImmutableMap.Builder mapBuilder()
    {
        return builder();
    }

    @Nonnull
    public static  Collector, ?, JImmutableMap> createMapCollector()
    {
        return Collector., JImmutableMap.Builder, JImmutableMap>of(JImmutableHashMap::builder,
                                                                                           (b, v) -> b.add(v),
                                                                                           (b1, b2) -> b1.add(b2),
                                                                                           b -> b.build(),
                                                                                           Collector.Characteristics.UNORDERED,
                                                                                           Collector.Characteristics.CONCURRENT);
    }

    @Override
    public V getValueOr(K key,
                        V defaultValue)
    {
        return root.getValueOr(collisionMap, key.hashCode(), key, defaultValue);
    }

    @Nonnull
    @Override
    public Holder find(@Nonnull K key)
    {
        return root.find(collisionMap, key.hashCode(), key);
    }

    @Nonnull
    @Override
    public Holder> findEntry(@Nonnull K key)
    {
        final Holder value = find(key);
        if (value.isEmpty()) {
            return Holders.of();
        } else {
            return Holders.of(MapEntry.of(key, value.getValue()));
        }
    }

    @Nonnull
    @Override
    public JImmutableMap assign(@Nonnull K key,
                                      V value)
    {
        final MapNode newRoot = root.assign(collisionMap, key.hashCode(), key, value);
        if (newRoot == root) {
            return this;
        } else {
            return new JImmutableHashMap<>(newRoot, collisionMap);
        }
    }

    @Nonnull
    @Override
    public JImmutableMap update(@Nonnull K key,
                                      @Nonnull Func1, V> generator)
    {
        final MapNode newRoot = root.update(collisionMap, key.hashCode(), key, generator);
        if (newRoot == root) {
            return this;
        } else {
            return new JImmutableHashMap<>(newRoot, collisionMap);
        }
    }

    @Nonnull
    @Override
    public JImmutableMap delete(@Nonnull K key)
    {
        final MapNode newRoot = root.delete(collisionMap, key.hashCode(), key);
        if (newRoot == root) {
            return this;
        } else if (newRoot.isEmpty(collisionMap)) {
            return of();
        } else {
            return new JImmutableHashMap<>(newRoot, collisionMap);
        }
    }

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

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

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

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

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

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

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

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

    // for unit test to verify proper transforms selected
    CollisionMap getCollisionMap()
    {
        return collisionMap;
    }

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

    @ThreadSafe
    public static class Builder
        implements JImmutableMap.Builder
    {
        private final MapBuilder builder = new MapBuilder<>();

        @Nonnull
        @Override
        public synchronized JImmutableMap build()
        {
            final MapNode root = builder.build();
            final CollisionMap collisionMap = builder.getCollisionMap();
            if (root.isEmpty(collisionMap)) {
                return of();
            } else {
                return new JImmutableHashMap<>(root, collisionMap);
            }
        }

        @Nonnull
        @Override
        public synchronized JImmutableMap.Builder clear()
        {
            builder.clear();
            return this;
        }

        @Nonnull
        @Override
        public synchronized JImmutableMap.Builder add(@Nonnull K key,
                                                            V value)
        {
            builder.add(key, value);
            return this;
        }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy