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

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

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2016 Goldman Sachs.
 * 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.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Maps;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.tuple.Tuples;

final class ImmutableQuadrupletonMap
        extends AbstractImmutableMap
        implements Serializable
{
    private static final long serialVersionUID = 1L;

    private final K key1;
    private final V value1;
    private final K key2;
    private final V value2;
    private final K key3;
    private final V value3;
    private final K key4;
    private final V value4;

    ImmutableQuadrupletonMap(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
    {
        this.key1 = key1;
        this.value1 = value1;
        this.key2 = key2;
        this.value2 = value2;
        this.key3 = key3;
        this.value3 = value3;
        this.key4 = key4;
        this.value4 = value4;
    }

    @Override
    public RichIterable keysView()
    {
        return Lists.immutable.with(this.key1, this.key2, this.key3, this.key4).asLazy();
    }

    @Override
    public RichIterable valuesView()
    {
        return Lists.immutable.with(this.value1, this.value2, this.value3, this.value4).asLazy();
    }

    @Override
    public RichIterable> keyValuesView()
    {
        return Lists.immutable.with(
                Tuples.pair(this.key1, this.value1),
                Tuples.pair(this.key2, this.value2),
                Tuples.pair(this.key3, this.value3),
                Tuples.pair(this.key4, this.value4)).asLazy();
    }

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

    @Override
    public boolean containsKey(Object key)
    {
        return Comparators.nullSafeEquals(this.key4, key)
                || Comparators.nullSafeEquals(this.key3, key)
                || Comparators.nullSafeEquals(this.key2, key)
                || Comparators.nullSafeEquals(this.key1, key);
    }

    @Override
    public boolean containsValue(Object value)
    {
        return Comparators.nullSafeEquals(this.value4, value)
                || Comparators.nullSafeEquals(this.value3, value)
                || Comparators.nullSafeEquals(this.value2, value)
                || Comparators.nullSafeEquals(this.value1, value);
    }

    @Override
    public V get(Object key)
    {
        if (Comparators.nullSafeEquals(this.key4, key))
        {
            return this.value4;
        }
        if (Comparators.nullSafeEquals(this.key3, key))
        {
            return this.value3;
        }
        if (Comparators.nullSafeEquals(this.key2, key))
        {
            return this.value2;
        }
        if (Comparators.nullSafeEquals(this.key1, key))
        {
            return this.value1;
        }
        return null;
    }

    @Override
    public Set keySet()
    {
        return Sets.immutable.with(this.key1, this.key2, this.key3, this.key4).castToSet();
    }

    @Override
    public Collection values()
    {
        return Lists.immutable.with(this.value1, this.value2, this.value3, this.value4).castToList();
    }

    @Override
    public int hashCode()
    {
        return this.keyAndValueHashCode(this.key1, this.value1)
                + this.keyAndValueHashCode(this.key2, this.value2)
                + this.keyAndValueHashCode(this.key3, this.value3)
                + this.keyAndValueHashCode(this.key4, this.value4);
    }

    @Override
    public boolean equals(Object other)
    {
        if (!(other instanceof Map))
        {
            return false;
        }
        Map that = (Map) other;
        return that.size() == this.size()
                && this.keyAndValueEquals(this.key1, this.value1, that)
                && this.keyAndValueEquals(this.key2, this.value2, that)
                && this.keyAndValueEquals(this.key3, this.value3, that)
                && this.keyAndValueEquals(this.key4, this.value4, that);
    }

    @Override
    public String toString()
    {
        return "{"
                + this.key1 + '=' + this.value1 + ", "
                + this.key2 + '=' + this.value2 + ", "
                + this.key3 + '=' + this.value3 + ", "
                + this.key4 + '=' + this.value4 + '}';
    }

    @Override
    public ImmutableMap flipUniqueValues()
    {
        return Maps.immutable.with(this.value1, this.key1, this.value2, this.key2, this.value3, this.key3, this.value4, this.key4);
    }

    @Override
    public void forEachKeyValue(Procedure2 procedure)
    {
        procedure.value(this.key1, this.value1);
        procedure.value(this.key2, this.value2);
        procedure.value(this.key3, this.value3);
        procedure.value(this.key4, this.value4);
    }

    @Override
    public void forEachKey(Procedure procedure)
    {
        procedure.value(this.key1);
        procedure.value(this.key2);
        procedure.value(this.key3);
        procedure.value(this.key4);
    }

    @Override
    public void forEachValue(Procedure procedure)
    {
        procedure.value(this.value1);
        procedure.value(this.value2);
        procedure.value(this.value3);
        procedure.value(this.value4);
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        objectIntProcedure.value(this.value1, 0);
        objectIntProcedure.value(this.value2, 1);
        objectIntProcedure.value(this.value3, 2);
        objectIntProcedure.value(this.value4, 3);
    }

    @Override
    public 

void forEachWith(Procedure2 procedure, P parameter) { procedure.value(this.value1, parameter); procedure.value(this.value2, parameter); procedure.value(this.value3, parameter); procedure.value(this.value4, parameter); } @Override public ImmutableMap collect(Function2> function) { Pair pair1 = function.value(this.key1, this.value1); Pair pair2 = function.value(this.key2, this.value2); Pair pair3 = function.value(this.key3, this.value3); Pair pair4 = function.value(this.key4, this.value4); return Maps.immutable.with(pair1.getOne(), pair1.getTwo(), pair2.getOne(), pair2.getTwo(), pair3.getOne(), pair3.getTwo(), pair4.getOne(), pair4.getTwo()); } @Override public ImmutableMap collectValues(Function2 function) { return Maps.immutable.with( this.key1, function.value(this.key1, this.value1), this.key2, function.value(this.key2, this.value2), this.key3, function.value(this.key3, this.value3), this.key4, function.value(this.key4, this.value4)); } @Override public Pair detect(Predicate2 predicate) { if (predicate.accept(this.key1, this.value1)) { return Tuples.pair(this.key1, this.value1); } if (predicate.accept(this.key2, this.value2)) { return Tuples.pair(this.key2, this.value2); } if (predicate.accept(this.key3, this.value3)) { return Tuples.pair(this.key3, this.value3); } if (predicate.accept(this.key4, this.value4)) { return Tuples.pair(this.key4, this.value4); } return null; } @Override public ImmutableMap select(Predicate2 predicate) { return this.filter(predicate); } @Override public ImmutableMap reject(Predicate2 predicate) { return this.filter(Predicates2.not(predicate)); } @Override public V getOnly() { throw new IllegalStateException("Size must be 1 but was " + this.size()); } private ImmutableMap filter(Predicate2 predicate) { int result = 0; if (predicate.accept(this.key1, this.value1)) { result |= 1; } if (predicate.accept(this.key2, this.value2)) { result |= 2; } if (predicate.accept(this.key3, this.value3)) { result |= 4; } if (predicate.accept(this.key4, this.value4)) { result |= 8; } switch (result) { case 1: return Maps.immutable.with(this.key1, this.value1); case 2: return Maps.immutable.with(this.key2, this.value2); case 3: return Maps.immutable.with(this.key1, this.value1, this.key2, this.value2); case 4: return Maps.immutable.with(this.key3, this.value3); case 5: return Maps.immutable.with(this.key1, this.value1, this.key3, this.value3); case 6: return Maps.immutable.with(this.key2, this.value2, this.key3, this.value3); case 7: return Maps.immutable.with(this.key1, this.value1, this.key2, this.value2, this.key3, this.value3); case 8: return Maps.immutable.with(this.key4, this.value4); case 9: return Maps.immutable.with(this.key1, this.value1, this.key4, this.value4); case 10: return Maps.immutable.with(this.key2, this.value2, this.key4, this.value4); case 11: return Maps.immutable.with(this.key1, this.value1, this.key2, this.value2, this.key4, this.value4); case 12: return Maps.immutable.with(this.key3, this.value3, this.key4, this.value4); case 13: return Maps.immutable.with(this.key1, this.value1, this.key3, this.value3, this.key4, this.value4); case 14: return Maps.immutable.with(this.key2, this.value2, this.key3, this.value3, this.key4, this.value4); case 15: return Maps.immutable.with(this.key1, this.value1, this.key2, this.value2, this.key3, this.value3, this.key4, this.value4); default: return Maps.immutable.empty(); } } private Object writeReplace() { return new ImmutableMapSerializationProxy<>(this); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy