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

org.javersion.util.PersistentHashMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 Samppa Saarela
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.javersion.util;

import java.util.Map;
import java.util.Objects;
import java.util.Spliterator;
import java.util.stream.Collectors;

import javax.annotation.concurrent.Immutable;


@Immutable
public class PersistentHashMap extends AbstractHashMap> implements PersistentMap {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static final PersistentHashMap EMPTY_MAP = new PersistentHashMap(EMPTY_NODE, 0);

    private final Node> root;

    private final int size;

    @SuppressWarnings("unchecked")
    public static  PersistentHashMap empty() {
        return (PersistentHashMap) EMPTY_MAP;
    }

    @SuppressWarnings("unchecked")
    public static  PersistentHashMap copyOf(Map map) {
        return ((PersistentHashMap) EMPTY_MAP).assocAll(map);
    }

    public static  PersistentHashMap of() {
        return empty();
    }

    @SuppressWarnings("unchecked")
    public static  PersistentHashMap of(K k1, V v1) {
        return (PersistentHashMap) EMPTY_MAP.assoc(k1, v1);
    }

    public static  PersistentHashMap of(K k1, V v1, K k2, V v2) {
        MutableHashMap map = new MutableHashMap(2);
        map.put(k1, v1);
        map.put(k2, v2);
        return map.toPersistentMap();
    }

    public static  PersistentHashMap of(K k1, V v1, K k2, V v2, K k3, V v3) {
        MutableHashMap map = new MutableHashMap(3);
        map.put(k1, v1);
        map.put(k2, v2);
        map.put(k3, v3);
        return map.toPersistentMap();
    }


    @SuppressWarnings("unchecked")
    static  PersistentHashMap create(Node> newRoot, int newSize) {
        return newRoot == null ? (PersistentHashMap) EMPTY_MAP : new PersistentHashMap(newRoot, newSize);
    }

    private PersistentHashMap(Node> newRoot, int newSize) {
        this.root = newRoot;
        this.size = newSize;
    }

    public MutableHashMap toMutableMap() {
        return new MutableHashMap(root, size);
    }

    @Override
    public Map asMap() {
        return new ImmutableMap<>(this);
    }

    @Override
    protected Node> root() {
        return root;
    }

    @Override
    public Spliterator> spliterator() {
        return new EntrySpliterator<>(root, size, true);
    }

    @Override
    public Spliterator keySpliterator() {
        return new KeySpliterator<>(root, size, true);
    }

    @Override
    public Spliterator valueSpliterator() {
        return new ValueSpliterator<>(root, size, true);
    }

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

    @Override
    protected PersistentHashMap doReturn(Node> newRoot, int newSize) {
        if (newRoot == root) {
            return this;
        } else {
            return create(newRoot, newSize);
        }
    }

    public String toString() {
        return stream().map(Objects::toString).collect(Collectors.joining(", ", "{", "}"));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy