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

org.eclipse.collections.impl.bimap.immutable.AbstractImmutableBiMap Maven / Gradle / Ivy

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2022 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.bimap.immutable;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.primitive.ImmutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.ImmutableByteBag;
import org.eclipse.collections.api.bag.primitive.ImmutableCharBag;
import org.eclipse.collections.api.bag.primitive.ImmutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.ImmutableFloatBag;
import org.eclipse.collections.api.bag.primitive.ImmutableIntBag;
import org.eclipse.collections.api.bag.primitive.ImmutableLongBag;
import org.eclipse.collections.api.bag.primitive.ImmutableShortBag;
import org.eclipse.collections.api.bimap.ImmutableBiMap;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
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.factory.BiMaps;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectLongMap;
import org.eclipse.collections.api.map.primitive.ObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ObjectLongMap;
import org.eclipse.collections.api.multimap.set.ImmutableSetMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.set.PartitionImmutableSet;
import org.eclipse.collections.api.partition.set.PartitionMutableSet;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.bimap.AbstractBiMap;
import org.eclipse.collections.impl.bimap.mutable.HashBiMap;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.PartitionProcedure;
import org.eclipse.collections.impl.block.procedure.SelectInstancesOfProcedure;
import org.eclipse.collections.impl.list.fixed.ArrayAdapter;
import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap;
import org.eclipse.collections.impl.partition.set.PartitionUnifiedSet;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.MapIterate;

public abstract class AbstractImmutableBiMap extends AbstractBiMap implements ImmutableBiMap, Map
{
    private final ImmutableMap delegate;
    private final AbstractImmutableBiMap inverse;

    private AbstractImmutableBiMap(ImmutableMap delegate, AbstractImmutableBiMap valuesToKeys)
    {
        this.delegate = delegate;
        this.inverse = valuesToKeys;
    }

    AbstractImmutableBiMap(ImmutableMap map, ImmutableMap inverse)
    {
        this.delegate = map;
        this.inverse = new Inverse<>(inverse, this);
    }

    @Override
    protected ImmutableMap getDelegate()
    {
        return this.delegate;
    }

    @Override
    protected ImmutableMap getInverse()
    {
        return this.inverse.delegate;
    }

    @Override
    public ImmutableBiMap newWithKeyValue(K key, V value)
    {
        HashBiMap map = new HashBiMap<>(this.delegate.castToMap());
        map.put(key, value);
        return map.toImmutable();
    }

    @Override
    public ImmutableBiMap newWithAllKeyValues(Iterable> keyValues)
    {
        HashBiMap map = new HashBiMap<>(this.delegate.castToMap());
        for (Pair keyValuePair : keyValues)
        {
            map.put(keyValuePair.getOne(), keyValuePair.getTwo());
        }
        return map.toImmutable();
    }

    @Override
    public ImmutableBiMap newWithMap(Map map)
    {
        HashBiMap result = new HashBiMap<>(this.delegate.castToMap());
        result.putAll(map);
        return result.toImmutable();
    }

    @Override
    public ImmutableBiMap newWithMapIterable(MapIterable mapIterable)
    {
        HashBiMap map = new HashBiMap<>(this.delegate.castToMap());
        mapIterable.forEachKeyValue(map::put);
        return map.toImmutable();
    }

    @Override
    public ImmutableBiMap newWithAllKeyValueArguments(Pair... keyValuePairs)
    {
        return this.newWithAllKeyValues(ArrayAdapter.adapt(keyValuePairs));
    }

    @Override
    public ImmutableBiMap newWithoutKey(K key)
    {
        HashBiMap map = new HashBiMap<>(this.delegate.castToMap());
        map.removeKey(key);
        return map.toImmutable();
    }

    @Override
    public ImmutableBiMap newWithoutAllKeys(Iterable keys)
    {
        HashBiMap map = new HashBiMap<>(this.delegate.castToMap());
        for (K key : keys)
        {
            map.removeKey(key);
        }
        return map.toImmutable();
    }

    @Override
    public ImmutableBiMap inverse()
    {
        return this.inverse;
    }

    @Override
    public ImmutableSetMultimap flip()
    {
        // TODO: We could optimize this since we know the values are unique
        return MapIterate.flip(this).toImmutable();
    }

    @Override
    public ImmutableBiMap flipUniqueValues()
    {
        return this.inverse();
    }

    @Override
    public V put(K key, V value)
    {
        throw new UnsupportedOperationException("Cannot call put() on " + this.getClass().getSimpleName());
    }

    @Override
    public void putAll(Map map)
    {
        throw new UnsupportedOperationException("Cannot call putAll() on " + this.getClass().getSimpleName());
    }

    @Override
    public V remove(Object key)
    {
        throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
    }

    @Override
    public void clear()
    {
        throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName());
    }

    @Override
    public Set keySet()
    {
        return this.delegate.castToMap().keySet();
    }

    @Override
    public Collection values()
    {
        return this.delegate.castToMap().values();
    }

    @Override
    public Set> entrySet()
    {
        return this.delegate.castToMap().entrySet();
    }

    @Override
    public Iterator iterator()
    {
        return this.delegate.iterator();
    }

    @Override
    public ImmutableBiMap toImmutable()
    {
        return this;
    }

    @Override
    public Map castToMap()
    {
        return this;
    }

    public MutableMap toMap()
    {
        return this.getDelegate().toMap();
    }

    @Override
    public  ImmutableBiMap collect(Function2> function)
    {
        ImmutableMap result = this.delegate.collect(function);
        return BiMaps.immutable.withAll(result);
    }

    @Override
    public  ImmutableBag collect(Function function)
    {
        return this.delegate.collect(function);
    }

    @Override
    public  ImmutableBiMap collectValues(Function2 function)
    {
        ImmutableMap result = this.delegate.collectValues(function);
        return BiMaps.immutable.withAll(result);
    }

    @Override
    public ImmutableBooleanBag collectBoolean(BooleanFunction booleanFunction)
    {
        return this.delegate.collectBoolean(booleanFunction);
    }

    @Override
    public ImmutableByteBag collectByte(ByteFunction byteFunction)
    {
        return this.delegate.collectByte(byteFunction);
    }

    @Override
    public ImmutableCharBag collectChar(CharFunction charFunction)
    {
        return this.delegate.collectChar(charFunction);
    }

    @Override
    public ImmutableDoubleBag collectDouble(DoubleFunction doubleFunction)
    {
        return this.delegate.collectDouble(doubleFunction);
    }

    @Override
    public ImmutableFloatBag collectFloat(FloatFunction floatFunction)
    {
        return this.delegate.collectFloat(floatFunction);
    }

    @Override
    public ImmutableIntBag collectInt(IntFunction intFunction)
    {
        return this.delegate.collectInt(intFunction);
    }

    @Override
    public ImmutableLongBag collectLong(LongFunction longFunction)
    {
        return this.delegate.collectLong(longFunction);
    }

    @Override
    public ImmutableShortBag collectShort(ShortFunction shortFunction)
    {
        return this.delegate.collectShort(shortFunction);
    }

    @Override
    public  ImmutableBag collectWith(Function2 function, P parameter)
    {
        return this.delegate.collectWith(function, parameter);
    }

    @Override
    public  ImmutableBag collectIf(Predicate predicate, Function function)
    {
        return this.delegate.collectIf(predicate, function);
    }

    @Override
    public  ImmutableBag flatCollect(Function> function)
    {
        return this.delegate.flatCollect(function);
    }

    @Override
    public ImmutableBiMap select(Predicate2 predicate)
    {
        return MapIterate.selectMapOnEntry(this, predicate, HashBiMap.newMap()).toImmutable();
    }

    @Override
    public ImmutableBiMap tap(Procedure procedure)
    {
        this.forEach(procedure);
        return this;
    }

    @Override
    public ImmutableSet select(Predicate predicate)
    {
        return this.delegate.select(predicate, Sets.mutable.empty()).toImmutable();
    }

    @Override
    public 

ImmutableSet selectWith(Predicate2 predicate, P parameter) { return this.delegate.selectWith(predicate, parameter, Sets.mutable.empty()).toImmutable(); } @Override public ImmutableBiMap reject(Predicate2 predicate) { return MapIterate.rejectMapOnEntry(this, predicate, HashBiMap.newMap()).toImmutable(); } @Override public ImmutableSet reject(Predicate predicate) { return this.delegate.reject(predicate, Sets.mutable.empty()).toImmutable(); } @Override public

ImmutableSet rejectWith(Predicate2 predicate, P parameter) { return this.delegate.rejectWith(predicate, parameter, Sets.mutable.empty()).toImmutable(); } @Override public PartitionImmutableSet partition(Predicate predicate) { PartitionMutableSet result = new PartitionUnifiedSet<>(); this.inverse.forEachKey(new PartitionProcedure<>(predicate, result)); return result.toImmutable(); } @Override public

PartitionImmutableSet partitionWith(Predicate2 predicate, P parameter) { return this.partition(Predicates.bind(predicate, parameter)); } /** * @deprecated in 8.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public ImmutableSet> zip(Iterable that) { if (that instanceof Collection || that instanceof RichIterable) { int thatSize = Iterate.sizeOf(that); UnifiedSet> target = UnifiedSet.newSet(Math.min(this.size(), thatSize)); return this.delegate.zip(that, target).toImmutable(); } return this.delegate.zip(that, Sets.mutable.empty()).toImmutable(); } /** * @deprecated in 8.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public ImmutableSet> zipWithIndex() { return this.delegate.zipWithIndex(Sets.mutable.withInitialCapacity(this.size())).toImmutable(); } @Override public ImmutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { ObjectLongMap map = this.delegate.sumByInt(groupBy, function); return map.toImmutable(); } @Override public ImmutableObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { ObjectDoubleMap map = this.delegate.sumByFloat(groupBy, function); return map.toImmutable(); } @Override public ImmutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { ObjectLongMap map = this.delegate.sumByLong(groupBy, function); return map.toImmutable(); } @Override public ImmutableObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { ObjectDoubleMap map = this.delegate.sumByDouble(groupBy, function); return map.toImmutable(); } @Override public ImmutableSetMultimap groupBy(Function function) { return this.delegate.groupBy(function, new UnifiedSetMultimap()).toImmutable(); } @Override public ImmutableSetMultimap groupByEach(Function> function) { return this.delegate.groupByEach(function, new UnifiedSetMultimap<>()).toImmutable(); } @Override public ImmutableBiMap groupByUniqueKey(Function function) { return BiMaps.immutable.withAll(this.delegate.groupByUniqueKey(function)); } @Override public ImmutableMap aggregateBy(Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { return this.delegate.aggregateBy(groupBy, zeroValueFactory, nonMutatingAggregator); } @Override public ImmutableMap aggregateBy( Function keyFunction, Function valueFunction, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { return this.delegate.aggregateBy(keyFunction, valueFunction, zeroValueFactory, nonMutatingAggregator); } @Override public ImmutableMap aggregateInPlaceBy(Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { return this.delegate.aggregateInPlaceBy(groupBy, zeroValueFactory, mutatingAggregator); } @Override public ImmutableSet selectInstancesOf(Class clazz) { MutableSet result = new UnifiedSet<>(); this.inverse.forEachKey(new SelectInstancesOfProcedure<>(clazz, result)); return result.toImmutable(); } private static class Inverse extends AbstractImmutableBiMap implements Serializable { Inverse(ImmutableMap delegate, AbstractImmutableBiMap inverse) { super(delegate, inverse); } protected Object writeReplace() { return new ImmutableBiMapSerializationProxy<>(this); } } }