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

org.eclipse.collections.impl.map.immutable.AbstractImmutableMap 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.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import net.jcip.annotations.Immutable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
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.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.bag.primitive.MutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.MutableFloatBag;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.bag.primitive.MutableLongBag;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
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.map.ImmutableMap;
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.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.multimap.bag.ImmutableBagMultimap;
import org.eclipse.collections.api.multimap.set.ImmutableSetMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionImmutableBag;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
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.bag.mutable.HashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.BooleanHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.ByteHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.DoubleHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.FloatHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.LongHashBag;
import org.eclipse.collections.impl.bag.mutable.primitive.ShortHashBag;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.PartitionPredicate2Procedure;
import org.eclipse.collections.impl.block.procedure.PartitionProcedure;
import org.eclipse.collections.impl.block.procedure.SelectInstancesOfProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectBooleanProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectByteProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectCharProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectDoubleProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectFloatProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectIntProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectLongProcedure;
import org.eclipse.collections.impl.block.procedure.primitive.CollectShortProcedure;
import org.eclipse.collections.impl.map.AbstractMapIterable;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
import org.eclipse.collections.impl.partition.bag.PartitionHashBag;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.impl.utility.MapIterate;

@Immutable
public abstract class AbstractImmutableMap
        extends AbstractMapIterable
        implements ImmutableMap, Map
{
    @Override
    public Map castToMap()
    {
        return this;
    }

    @Override
    public MutableMap toMap()
    {
        return UnifiedMap.newMap(this);
    }

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

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

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

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

    @Override
    public ImmutableSetMultimap flip()
    {
        return MapIterate.flip(this).toImmutable();
    }

    @Override
    public Set> entrySet()
    {
        MutableSet> set = UnifiedSet.newSet(this.size());
        this.forEachKeyValue((key, value) -> set.add(ImmutableEntry.of(key, value)));
        return set.toImmutable().castToSet();
    }

    @Override
    public ImmutableMap newWithKeyValue(K key, V value)
    {
        UnifiedMap map = UnifiedMap.newMap(this);
        map.put(key, value);
        return map.toImmutable();
    }

    @Override
    public ImmutableMap newWithAllKeyValues(Iterable> keyValues)
    {
        UnifiedMap map = UnifiedMap.newMap(this);
        for (Pair keyValuePair : keyValues)
        {
            map.put(keyValuePair.getOne(), keyValuePair.getTwo());
        }
        return map.toImmutable();
    }

    @Override
    public ImmutableMap newWithAllKeyValueArguments(Pair... keyValuePairs)
    {
        UnifiedMap map = UnifiedMap.newMap(this);
        for (Pair keyValuePair : keyValuePairs)
        {
            map.put(keyValuePair.getOne(), keyValuePair.getTwo());
        }
        return map.toImmutable();
    }

    @Override
    public ImmutableMap newWithoutKey(K key)
    {
        UnifiedMap map = UnifiedMap.newMap(this);
        map.removeKey(key);
        return map.toImmutable();
    }

    @Override
    public ImmutableMap newWithoutAllKeys(Iterable keys)
    {
        UnifiedMap map = UnifiedMap.newMap(this);
        for (K key : keys)
        {
            map.removeKey(key);
        }
        return map.toImmutable();
    }

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

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

    @Override
    public ImmutableMap flipUniqueValues()
    {
        return MapIterate.flipUniqueValues(this).toImmutable();
    }

    @Override
    public  ImmutableMap collect(Function2> function)
    {
        UnifiedMap result = MapIterate.collect(this, function, UnifiedMap.newMap());
        return result.toImmutable();
    }

    @Override
    public  ImmutableMap collectValues(Function2 function)
    {
        UnifiedMap result = MapIterate.collectValues(this, function, UnifiedMap.newMap(this.size()));
        return result.toImmutable();
    }

    @Override
    public ImmutableMap select(Predicate2 predicate)
    {
        UnifiedMap result = MapIterate.selectMapOnEntry(this, predicate, UnifiedMap.newMap());
        return result.toImmutable();
    }

    @Override
    public ImmutableMap reject(Predicate2 predicate)
    {
        UnifiedMap result = MapIterate.rejectMapOnEntry(this, predicate, UnifiedMap.newMap());
        return result.toImmutable();
    }

    @Override
    public Pair detect(Predicate2 predicate)
    {
        return MapIterate.detect(this, predicate);
    }

    @Override
    public Optional> detectOptional(Predicate2 predicate)
    {
        return MapIterate.detectOptional(this, predicate);
    }

    @Override
    public  ImmutableBag collect(Function function)
    {
        return this.collect(function, new HashBag()).toImmutable();
    }

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

    @Override
    public ImmutableBooleanBag collectBoolean(BooleanFunction booleanFunction)
    {
        MutableBooleanBag result = new BooleanHashBag();
        this.forEach(new CollectBooleanProcedure<>(booleanFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableByteBag collectByte(ByteFunction byteFunction)
    {
        MutableByteBag result = new ByteHashBag();
        this.forEach(new CollectByteProcedure<>(byteFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableCharBag collectChar(CharFunction charFunction)
    {
        MutableCharBag result = new CharHashBag();
        this.forEach(new CollectCharProcedure<>(charFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableDoubleBag collectDouble(DoubleFunction doubleFunction)
    {
        MutableDoubleBag result = new DoubleHashBag();
        this.forEach(new CollectDoubleProcedure<>(doubleFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableFloatBag collectFloat(FloatFunction floatFunction)
    {
        MutableFloatBag result = new FloatHashBag();
        this.forEach(new CollectFloatProcedure<>(floatFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableIntBag collectInt(IntFunction intFunction)
    {
        MutableIntBag result = new IntHashBag();
        this.forEach(new CollectIntProcedure<>(intFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableLongBag collectLong(LongFunction longFunction)
    {
        MutableLongBag result = new LongHashBag();
        this.forEach(new CollectLongProcedure<>(longFunction, result));
        return result.toImmutable();
    }

    @Override
    public ImmutableShortBag collectShort(ShortFunction shortFunction)
    {
        MutableShortBag result = new ShortHashBag();
        this.forEach(new CollectShortProcedure<>(shortFunction, result));
        return result.toImmutable();
    }

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

    @Override
    public  ImmutableBag flatCollect(Function> function)
    {
        return this.flatCollect(function, new HashBag<>()).toImmutable();
    }

    @Override
    public ImmutableBag select(Predicate predicate)
    {
        return this.select(predicate, new HashBag<>()).toImmutable();
    }

    @Override
    public 

ImmutableBag selectWith(Predicate2 predicate, P parameter) { return this.select(Predicates.bind(predicate, parameter)); } @Override public ImmutableMap tap(Procedure procedure) { this.forEach(procedure); return this; } @Override public ImmutableBag reject(Predicate predicate) { return this.reject(predicate, new HashBag<>()).toImmutable(); } @Override public

ImmutableBag rejectWith(Predicate2 predicate, P parameter) { return this.reject(Predicates.bind(predicate, parameter)); } @Override public PartitionImmutableBag partition(Predicate predicate) { PartitionMutableBag partitionMutableBag = new PartitionHashBag<>(); this.forEach(new PartitionProcedure<>(predicate, partitionMutableBag)); return partitionMutableBag.toImmutable(); } @Override public

PartitionImmutableBag partitionWith(Predicate2 predicate, P parameter) { PartitionMutableBag partitionMutableBag = new PartitionHashBag<>(); this.forEach(new PartitionPredicate2Procedure<>(predicate, parameter, partitionMutableBag)); return partitionMutableBag.toImmutable(); } @Override public ImmutableBag selectInstancesOf(Class clazz) { MutableBag result = HashBag.newBag(); this.forEach(new SelectInstancesOfProcedure<>(clazz, result)); return result.toImmutable(); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public ImmutableBag> zip(Iterable that) { return this.zip(that, HashBag.newBag(this.size())).toImmutable(); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public ImmutableSet> zipWithIndex() { return this.zipWithIndex(UnifiedSet.newSet(this.size())).toImmutable(); } @Override public ImmutableBagMultimap groupBy(Function function) { return this.groupBy(function, HashBagMultimap.newMultimap()).toImmutable(); } @Override public ImmutableBagMultimap groupByEach(Function> function) { return this.groupByEach(function, HashBagMultimap.newMultimap()).toImmutable(); } @Override public ImmutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap()).toImmutable(); } @Override public ImmutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, mutatingAggregator)); return map.toImmutable(); } @Override public ImmutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map.toImmutable(); } @Override public ImmutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { MutableObjectLongMap result = ObjectLongHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function)).toImmutable(); } @Override public ImmutableObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { MutableObjectDoubleMap result = ObjectDoubleHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function)).toImmutable(); } @Override public ImmutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { MutableObjectLongMap result = ObjectLongHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function)).toImmutable(); } @Override public ImmutableObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { MutableObjectDoubleMap result = ObjectDoubleHashMap.newMap(); return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function)).toImmutable(); } }