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

com.gs.collections.impl.map.immutable.AbstractImmutableMap Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2014 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.impl.map.immutable;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.collection.ImmutableCollection;
import com.gs.collections.api.collection.primitive.ImmutableBooleanCollection;
import com.gs.collections.api.collection.primitive.ImmutableByteCollection;
import com.gs.collections.api.collection.primitive.ImmutableCharCollection;
import com.gs.collections.api.collection.primitive.ImmutableDoubleCollection;
import com.gs.collections.api.collection.primitive.ImmutableFloatCollection;
import com.gs.collections.api.collection.primitive.ImmutableIntCollection;
import com.gs.collections.api.collection.primitive.ImmutableLongCollection;
import com.gs.collections.api.collection.primitive.ImmutableShortCollection;
import com.gs.collections.api.map.ImmutableMap;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.ImmutableMultimap;
import com.gs.collections.api.multimap.set.ImmutableSetMultimap;
import com.gs.collections.api.partition.PartitionImmutableCollection;
import com.gs.collections.api.partition.PartitionMutableCollection;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.PartitionPredicate2Procedure;
import com.gs.collections.impl.block.procedure.PartitionProcedure;
import com.gs.collections.impl.block.procedure.SelectInstancesOfProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectBooleanProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectByteProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectCharProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectDoubleProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectFloatProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectIntProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectLongProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectShortProcedure;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
import com.gs.collections.impl.map.AbstractMapIterable;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.tuple.ImmutableEntry;
import com.gs.collections.impl.utility.MapIterate;
import net.jcip.annotations.Immutable;

@Immutable
public abstract class AbstractImmutableMap
        extends AbstractMapIterable
        implements ImmutableMap, Map
{
    /**
     * Returns a string representation of this map.  The string representation consists of a list of key-value mappings
     * in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}").
     * Adjacent mappings are separated by the characters ", " (comma and space).  Each key-value mapping is
     * rendered as the key followed by an equals sign ("=") followed by the associated value.  Keys and values
     * are converted to strings as by String.valueOf(Object).

*

* This implementation creates an empty string buffer, appends a left brace, and iterates over the map's * entrySet view, appending the string representation of each map.entry in turn. After appending * each entry except the last, the string ", " is appended. Finally a right brace is appended. A string * is obtained from the stringbuffer, and returned. * * @return a String representation of this map. */ public Map castToMap() { return this; } public MutableMap toMap() { return UnifiedMap.newMap(this); } public ImmutableMap toImmutable() { return this; } public Iterator iterator() { return this.valuesView().iterator(); } public void putAll(Map map) { throw new UnsupportedOperationException("Cannot call putAll() on " + this.getClass().getSimpleName()); } public void clear() { throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName()); } public ImmutableSetMultimap flip() { return MapIterate.flip(this).toImmutable(); } public Set> entrySet() { final MutableSet> set = UnifiedSet.newSet(this.size()); this.forEachKeyValue(new Procedure2() { public void value(K key, V value) { set.add(ImmutableEntry.of(key, value)); } }); return set.toImmutable().castToSet(); } public ImmutableMap newWithKeyValue(K key, V value) { UnifiedMap map = UnifiedMap.newMap(this); map.put(key, value); return map.toImmutable(); } public ImmutableMap newWithAllKeyValues(Iterable> keyValues) { UnifiedMap map = UnifiedMap.newMap(this); for (Pair keyValuePair : keyValues) { map.put(keyValuePair.getOne(), keyValuePair.getTwo()); } return map.toImmutable(); } public ImmutableMap newWithAllKeyValueArguments(Pair... keyValuePairs) { UnifiedMap map = UnifiedMap.newMap(this); for (Pair keyValuePair : keyValuePairs) { map.put(keyValuePair.getOne(), keyValuePair.getTwo()); } return map.toImmutable(); } public ImmutableMap newWithoutKey(K key) { UnifiedMap map = UnifiedMap.newMap(this); map.removeKey(key); return map.toImmutable(); } public ImmutableMap newWithoutAllKeys(Iterable keys) { UnifiedMap map = UnifiedMap.newMap(this); for (K key : keys) { map.removeKey(key); } return map.toImmutable(); } public V put(K key, V value) { throw new UnsupportedOperationException("Cannot call put() on " + this.getClass().getSimpleName()); } public V remove(Object key) { throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName()); } public ImmutableMap flipUniqueValues() { return MapIterate.flipUniqueValues(this).toImmutable(); } public ImmutableMap collect(Function2> function) { UnifiedMap result = MapIterate.collect(this, function, UnifiedMap.newMap()); return result.toImmutable(); } public ImmutableMap collectValues(Function2 function) { UnifiedMap result = MapIterate.collectValues(this, function, UnifiedMap.newMap(this.size())); return result.toImmutable(); } public ImmutableMap select(Predicate2 predicate) { UnifiedMap result = MapIterate.selectMapOnEntry(this, predicate, UnifiedMap.newMap()); return result.toImmutable(); } public ImmutableMap reject(Predicate2 predicate) { UnifiedMap result = MapIterate.rejectMapOnEntry(this, predicate, UnifiedMap.newMap()); return result.toImmutable(); } public Pair detect(Predicate2 predicate) { return MapIterate.detect(this, predicate); } @Override public ImmutableCollection collect(Function function) { return this.collect(function, FastList.newList(this.size())).toImmutable(); } @Override public ImmutableBooleanCollection collectBoolean(BooleanFunction booleanFunction) { BooleanArrayList result = new BooleanArrayList(this.size()); this.forEach(new CollectBooleanProcedure(booleanFunction, result)); return result.toImmutable(); } public ImmutableByteCollection collectByte(ByteFunction byteFunction) { ByteArrayList result = new ByteArrayList(this.size()); this.forEach(new CollectByteProcedure(byteFunction, result)); return result.toImmutable(); } public ImmutableCharCollection collectChar(CharFunction charFunction) { CharArrayList result = new CharArrayList(this.size()); this.forEach(new CollectCharProcedure(charFunction, result)); return result.toImmutable(); } public ImmutableDoubleCollection collectDouble(DoubleFunction doubleFunction) { DoubleArrayList result = new DoubleArrayList(this.size()); this.forEach(new CollectDoubleProcedure(doubleFunction, result)); return result.toImmutable(); } public ImmutableFloatCollection collectFloat(FloatFunction floatFunction) { FloatArrayList result = new FloatArrayList(this.size()); this.forEach(new CollectFloatProcedure(floatFunction, result)); return result.toImmutable(); } public ImmutableIntCollection collectInt(IntFunction intFunction) { IntArrayList result = new IntArrayList(this.size()); this.forEach(new CollectIntProcedure(intFunction, result)); return result.toImmutable(); } public ImmutableLongCollection collectLong(LongFunction longFunction) { LongArrayList result = new LongArrayList(this.size()); this.forEach(new CollectLongProcedure(longFunction, result)); return result.toImmutable(); } public ImmutableShortCollection collectShort(ShortFunction shortFunction) { ShortArrayList result = new ShortArrayList(this.size()); this.forEach(new CollectShortProcedure(shortFunction, result)); return result.toImmutable(); } public ImmutableCollection collectIf(Predicate predicate, Function function) { return this.collectIf(predicate, function, FastList.newList(this.size())).toImmutable(); } public ImmutableCollection flatCollect(Function> function) { return this.flatCollect(function, FastList.newList(this.size())).toImmutable(); } public ImmutableCollection reject(Predicate predicate) { return this.reject(predicate, FastList.newList(this.size())).toImmutable(); } public ImmutableCollection select(Predicate predicate) { return this.select(predicate, FastList.newList(this.size())).toImmutable(); } public PartitionImmutableCollection partition(Predicate predicate) { PartitionMutableCollection partitionMutableCollection = new PartitionFastList(); this.forEach(new PartitionProcedure(predicate, partitionMutableCollection)); return partitionMutableCollection.toImmutable(); } public

PartitionImmutableCollection partitionWith(Predicate2 predicate, P parameter) { PartitionMutableCollection partitionMutableCollection = new PartitionFastList(); this.forEach(new PartitionPredicate2Procedure(predicate, parameter, partitionMutableCollection)); return partitionMutableCollection.toImmutable(); } public ImmutableCollection selectInstancesOf(Class clazz) { FastList result = FastList.newList(this.size()); this.forEach(new SelectInstancesOfProcedure(clazz, result)); return result.toImmutable(); } public ImmutableCollection> zip(Iterable that) { return this.zip(that, FastList.>newList(this.size())).toImmutable(); } public ImmutableCollection> zipWithIndex() { return this.zipWithIndex(FastList.>newList(this.size())).toImmutable(); } public ImmutableMultimap groupBy(Function function) { return this.groupBy(function, FastListMultimap.newMultimap()).toImmutable(); } public ImmutableMultimap groupByEach(Function> function) { return this.groupByEach(function, FastListMultimap.newMultimap()).toImmutable(); } public ImmutableMap groupByUniqueKey(Function function) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet"); } public ImmutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map.toImmutable(); } public ImmutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map.toImmutable(); } }