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

org.eclipse.collections.impl.map.immutable.ImmutableMapFactoryImpl Maven / Gradle / Ivy

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2021 Goldman Sachs and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.map.immutable;

import java.util.Map;
import java.util.Objects;

import org.eclipse.collections.api.factory.map.ImmutableMapFactory;
import org.eclipse.collections.api.map.ImmutableMap;

public class ImmutableMapFactoryImpl implements ImmutableMapFactory
{
    public static final ImmutableMapFactory INSTANCE = new ImmutableMapFactoryImpl();

    @Override
    public  ImmutableMap empty()
    {
        return (ImmutableMap) ImmutableEmptyMap.INSTANCE;
    }

    @Override
    public  ImmutableMap of()
    {
        return this.empty();
    }

    @Override
    public  ImmutableMap with()
    {
        return this.empty();
    }

    @Override
    public  ImmutableMap of(K key, V value)
    {
        return this.with(key, value);
    }

    @Override
    public  ImmutableMap with(K key, V value)
    {
        return new ImmutableSingletonMap<>(key, value);
    }

    @Override
    public  ImmutableMap of(K key1, V value1, K key2, V value2)
    {
        return this.with(key1, value1, key2, value2);
    }

    @Override
    public  ImmutableMap with(K key1, V value1, K key2, V value2)
    {
        if (Objects.equals(key1, key2))
        {
            return this.of(key1, value2);
        }
        return new ImmutableDoubletonMap<>(key1, value1, key2, value2);
    }

    @Override
    public  ImmutableMap of(K key1, V value1, K key2, V value2, K key3, V value3)
    {
        return this.with(key1, value1, key2, value2, key3, value3);
    }

    @Override
    public  ImmutableMap with(K key1, V value1, K key2, V value2, K key3, V value3)
    {
        if (Objects.equals(key1, key2) && Objects.equals(key2, key3))
        {
            return this.of(key1, value3);
        }
        if (Objects.equals(key1, key2))
        {
            return this.of(key1, value2, key3, value3);
        }
        if (Objects.equals(key1, key3))
        {
            return this.of(key2, value2, key1, value3);
        }
        if (Objects.equals(key2, key3))
        {
            return this.of(key1, value1, key2, value3);
        }

        return new ImmutableTripletonMap<>(key1, value1, key2, value2, key3, value3);
    }

    @Override
    public  ImmutableMap of(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
    {
        return this.with(key1, value1, key2, value2, key3, value3, key4, value4);
    }

    @Override
    public  ImmutableMap with(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
    {
        if (Objects.equals(key1, key2))
        {
            return this.of(key1, value2, key3, value3, key4, value4);
        }
        if (Objects.equals(key1, key3))
        {
            return this.of(key2, value2, key1, value3, key4, value4);
        }
        if (Objects.equals(key1, key4))
        {
            return this.of(key2, value2, key3, value3, key1, value4);
        }
        if (Objects.equals(key2, key3))
        {
            return this.of(key1, value1, key2, value3, key4, value4);
        }
        if (Objects.equals(key2, key4))
        {
            return this.of(key1, value1, key3, value3, key2, value4);
        }
        if (Objects.equals(key3, key4))
        {
            return this.of(key1, value1, key2, value2, key3, value4);
        }
        return new ImmutableQuadrupletonMap<>(key1, value1, key2, value2, key3, value3, key4, value4);
    }

    /**
     * @deprecated use {@link #ofAll(Map)} instead (inlineable)
     */
    @Override
    @Deprecated
    public  ImmutableMap ofMap(Map map)
    {
        return this.ofAll(map);
    }

    @Override
    public  ImmutableMap ofAll(Map map)
    {
        return this.withAll(map);
    }

    @Override
    public  ImmutableMap withAll(Map map)
    {
        if (map.isEmpty())
        {
            return this.empty();
        }

        if (map.size() > 4)
        {
            return new ImmutableUnifiedMap<>(map);
        }

        Map.Entry[] entries = map.entrySet().toArray(new Map.Entry[map.entrySet().size()]);
        switch (entries.length)
        {
            case 1:
                return this.of(entries[0].getKey(), entries[0].getValue());
            case 2:
                return this.of(
                        entries[0].getKey(), entries[0].getValue(),
                        entries[1].getKey(), entries[1].getValue());
            case 3:
                return this.of(
                        entries[0].getKey(), entries[0].getValue(),
                        entries[1].getKey(), entries[1].getValue(),
                        entries[2].getKey(), entries[2].getValue());
            case 4:
                return this.of(
                        entries[0].getKey(), entries[0].getValue(),
                        entries[1].getKey(), entries[1].getValue(),
                        entries[2].getKey(), entries[2].getValue(),
                        entries[3].getKey(), entries[3].getValue());
            default:
                throw new AssertionError();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy