org.eclipse.collections.impl.map.immutable.AbstractImmutableMap Maven / Gradle / Ivy
/*
* 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 extends K, ? extends V> 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 extends Pair extends K, ? extends V>> keyValues)
{
UnifiedMap map = UnifiedMap.newMap(this);
for (Pair extends K, ? extends V> keyValuePair : keyValues)
{
map.put(keyValuePair.getOne(), keyValuePair.getTwo());
}
return map.toImmutable();
}
@Override
public ImmutableMap newWithAllKeyValueArguments(Pair extends K, ? extends V>... keyValuePairs)
{
UnifiedMap map = UnifiedMap.newMap(this);
for (Pair extends K, ? extends V> 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 extends K> 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 super K, ? super V, Pair> function)
{
UnifiedMap result = MapIterate.collect(this, function, UnifiedMap.newMap());
return result.toImmutable();
}
@Override
public ImmutableMap collectValues(Function2 super K, ? super V, ? extends R> function)
{
UnifiedMap result = MapIterate.collectValues(this, function, UnifiedMap.newMap(this.size()));
return result.toImmutable();
}
@Override
public ImmutableMap select(Predicate2 super K, ? super V> predicate)
{
UnifiedMap result = MapIterate.selectMapOnEntry(this, predicate, UnifiedMap.newMap());
return result.toImmutable();
}
@Override
public ImmutableMap reject(Predicate2 super K, ? super V> predicate)
{
UnifiedMap result = MapIterate.rejectMapOnEntry(this, predicate, UnifiedMap.newMap());
return result.toImmutable();
}
@Override
public Pair detect(Predicate2 super K, ? super V> predicate)
{
return MapIterate.detect(this, predicate);
}
@Override
public Optional> detectOptional(Predicate2 super K, ? super V> predicate)
{
return MapIterate.detectOptional(this, predicate);
}
@Override
public ImmutableBag collect(Function super V, ? extends R> function)
{
return this.collect(function, new HashBag()).toImmutable();
}
@Override
public ImmutableBag collectWith(Function2 super V, ? super P, ? extends VV> function, P parameter)
{
return this.collect(Functions.bind(function, parameter));
}
@Override
public ImmutableBooleanBag collectBoolean(BooleanFunction super V> booleanFunction)
{
MutableBooleanBag result = new BooleanHashBag();
this.forEach(new CollectBooleanProcedure<>(booleanFunction, result));
return result.toImmutable();
}
@Override
public ImmutableByteBag collectByte(ByteFunction super V> byteFunction)
{
MutableByteBag result = new ByteHashBag();
this.forEach(new CollectByteProcedure<>(byteFunction, result));
return result.toImmutable();
}
@Override
public ImmutableCharBag collectChar(CharFunction super V> charFunction)
{
MutableCharBag result = new CharHashBag();
this.forEach(new CollectCharProcedure<>(charFunction, result));
return result.toImmutable();
}
@Override
public ImmutableDoubleBag collectDouble(DoubleFunction super V> doubleFunction)
{
MutableDoubleBag result = new DoubleHashBag();
this.forEach(new CollectDoubleProcedure<>(doubleFunction, result));
return result.toImmutable();
}
@Override
public ImmutableFloatBag collectFloat(FloatFunction super V> floatFunction)
{
MutableFloatBag result = new FloatHashBag();
this.forEach(new CollectFloatProcedure<>(floatFunction, result));
return result.toImmutable();
}
@Override
public ImmutableIntBag collectInt(IntFunction super V> intFunction)
{
MutableIntBag result = new IntHashBag();
this.forEach(new CollectIntProcedure<>(intFunction, result));
return result.toImmutable();
}
@Override
public ImmutableLongBag collectLong(LongFunction super V> longFunction)
{
MutableLongBag result = new LongHashBag();
this.forEach(new CollectLongProcedure<>(longFunction, result));
return result.toImmutable();
}
@Override
public ImmutableShortBag collectShort(ShortFunction super V> shortFunction)
{
MutableShortBag result = new ShortHashBag();
this.forEach(new CollectShortProcedure<>(shortFunction, result));
return result.toImmutable();
}
@Override
public ImmutableBag collectIf(Predicate super V> predicate, Function super V, ? extends R> function)
{
return this.collectIf(predicate, function, new HashBag()).toImmutable();
}
@Override
public ImmutableBag flatCollect(Function super V, ? extends Iterable> function)
{
return this.flatCollect(function, new HashBag<>()).toImmutable();
}
@Override
public ImmutableBag select(Predicate super V> predicate)
{
return this.select(predicate, new HashBag<>()).toImmutable();
}
@Override
public ImmutableBag selectWith(Predicate2 super V, ? super P> predicate, P parameter)
{
return this.select(Predicates.bind(predicate, parameter));
}
@Override
public ImmutableMap tap(Procedure super V> procedure)
{
this.forEach(procedure);
return this;
}
@Override
public ImmutableBag reject(Predicate super V> predicate)
{
return this.reject(predicate, new HashBag<>()).toImmutable();
}
@Override
public ImmutableBag rejectWith(Predicate2 super V, ? super P> predicate, P parameter)
{
return this.reject(Predicates.bind(predicate, parameter));
}
@Override
public PartitionImmutableBag partition(Predicate super V> predicate)
{
PartitionMutableBag partitionMutableBag = new PartitionHashBag<>();
this.forEach(new PartitionProcedure<>(predicate, partitionMutableBag));
return partitionMutableBag.toImmutable();
}
@Override
public PartitionImmutableBag partitionWith(Predicate2 super V, ? super P> 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 super V, ? extends VV> function)
{
return this.groupBy(function, HashBagMultimap.newMultimap()).toImmutable();
}
@Override
public ImmutableBagMultimap groupByEach(Function super V, ? extends Iterable> function)
{
return this.groupByEach(function, HashBagMultimap.newMultimap()).toImmutable();
}
@Override
public ImmutableMap groupByUniqueKey(Function super V, ? extends V1> function)
{
return this.groupByUniqueKey(function, UnifiedMap.newMap()).toImmutable();
}
@Override
public ImmutableMap aggregateInPlaceBy(
Function super V, ? extends K2> groupBy,
Function0 extends V2> zeroValueFactory,
Procedure2 super V2, ? super V> mutatingAggregator)
{
MutableMap map = UnifiedMap.newMap();
this.forEach(new MutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, mutatingAggregator));
return map.toImmutable();
}
@Override
public ImmutableMap aggregateBy(
Function super V, ? extends K2> groupBy,
Function0 extends V2> zeroValueFactory,
Function2 super V2, ? super V, ? extends V2> nonMutatingAggregator)
{
MutableMap map = UnifiedMap.newMap();
this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator));
return map.toImmutable();
}
@Override
public ImmutableObjectLongMap sumByInt(Function super V, ? extends V1> groupBy, IntFunction super V> function)
{
MutableObjectLongMap result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function)).toImmutable();
}
@Override
public ImmutableObjectDoubleMap sumByFloat(Function super V, ? extends V1> groupBy, FloatFunction super V> function)
{
MutableObjectDoubleMap result = ObjectDoubleHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function)).toImmutable();
}
@Override
public ImmutableObjectLongMap sumByLong(Function super V, ? extends V1> groupBy, LongFunction super V> function)
{
MutableObjectLongMap result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function)).toImmutable();
}
@Override
public ImmutableObjectDoubleMap sumByDouble(Function super V, ? extends V1> groupBy, DoubleFunction super V> function)
{
MutableObjectDoubleMap result = ObjectDoubleHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function)).toImmutable();
}
}