com.gs.collections.impl.map.mutable.AbstractMutableMap Maven / Gradle / Ivy
/*
* 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.mutable;
import java.util.Collection;
import java.util.Iterator;
import com.gs.collections.api.RichIterable;
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.list.MutableList;
import com.gs.collections.api.list.primitive.MutableBooleanList;
import com.gs.collections.api.list.primitive.MutableByteList;
import com.gs.collections.api.list.primitive.MutableCharList;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.list.primitive.MutableLongList;
import com.gs.collections.api.list.primitive.MutableShortList;
import com.gs.collections.api.map.ImmutableMap;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.multimap.set.MutableSetMultimap;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.factory.Functions;
import com.gs.collections.impl.block.factory.Predicates;
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.factory.Maps;
import com.gs.collections.impl.list.fixed.ArrayAdapter;
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.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.tuple.AbstractImmutableEntry;
import com.gs.collections.impl.utility.LazyIterate;
import com.gs.collections.impl.utility.MapIterate;
public abstract class AbstractMutableMap extends AbstractMapIterable
implements MutableMap
{
/**
* 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.
*/
@Override
public abstract MutableMap clone();
/**
* Creates a new instance of the same type, using the given capacity and the default growth parameters.
*/
public abstract MutableMap newEmpty(int capacity);
public MutableMap asUnmodifiable()
{
return UnmodifiableMutableMap.of(this);
}
public ImmutableMap toImmutable()
{
return Maps.immutable.ofAll(this);
}
public MutableMap asSynchronized()
{
return SynchronizedMutableMap.of(this);
}
public RichIterable keysView()
{
return LazyIterate.adapt(this.keySet());
}
public RichIterable valuesView()
{
return LazyIterate.adapt(this.values());
}
public RichIterable> keyValuesView()
{
return LazyIterate.adapt(this.entrySet()).collect(AbstractImmutableEntry.getPairFunction());
}
public MutableMap flipUniqueValues()
{
return MapIterate.flipUniqueValues(this);
}
public V getIfAbsentPut(K key, Function0 function)
{
V result = this.get(key);
if (this.isAbsent(result, key))
{
result = function.value();
this.put(key, result);
}
return result;
}
public MutableSetMultimap flip()
{
return MapIterate.flip(this);
}
public V getIfAbsentPut(K key, V value)
{
V result = this.get(key);
if (this.isAbsent(result, key))
{
result = value;
this.put(key, result);
}
return result;
}
public V getIfAbsentPutWithKey(K key, Function function)
{
return this.getIfAbsentPutWith(key, function, key);
}
public V getIfAbsentPutWith(K key, Function function, P parameter)
{
V result = this.get(key);
if (this.isAbsent(result, key))
{
result = function.valueOf(parameter);
this.put(key, result);
}
return result;
}
public Iterator iterator()
{
return this.values().iterator();
}
public MutableMap collect(Function2> function)
{
return MapIterate.collect(this, function, UnifiedMap.newMap(this.size()));
}
public MutableMap collectValues(Function2 function)
{
return MapIterate.collectValues(this, function, this.newEmpty(this.size()));
}
public MutableMap select(Predicate2 predicate)
{
return MapIterate.selectMapOnEntry(this, predicate, this.newEmpty());
}
public MutableMap reject(Predicate2 predicate)
{
return MapIterate.rejectMapOnEntry(this, predicate, this.newEmpty());
}
public Pair detect(Predicate2 predicate)
{
return MapIterate.detect(this, predicate);
}
@Override
public MutableList collect(Function function)
{
return this.collect(function, FastList.newList(this.size()));
}
@Override
public MutableBooleanList collectBoolean(BooleanFunction booleanFunction)
{
BooleanArrayList result = new BooleanArrayList(this.size());
this.forEach(new CollectBooleanProcedure(booleanFunction, result));
return result;
}
public MutableByteList collectByte(ByteFunction byteFunction)
{
ByteArrayList result = new ByteArrayList(this.size());
this.forEach(new CollectByteProcedure(byteFunction, result));
return result;
}
public MutableCharList collectChar(CharFunction charFunction)
{
CharArrayList result = new CharArrayList(this.size());
this.forEach(new CollectCharProcedure(charFunction, result));
return result;
}
public MutableDoubleList collectDouble(DoubleFunction doubleFunction)
{
DoubleArrayList result = new DoubleArrayList(this.size());
this.forEach(new CollectDoubleProcedure(doubleFunction, result));
return result;
}
public MutableFloatList collectFloat(FloatFunction floatFunction)
{
FloatArrayList result = new FloatArrayList(this.size());
this.forEach(new CollectFloatProcedure(floatFunction, result));
return result;
}
public MutableIntList collectInt(IntFunction intFunction)
{
IntArrayList result = new IntArrayList(this.size());
this.forEach(new CollectIntProcedure(intFunction, result));
return result;
}
public MutableLongList collectLong(LongFunction longFunction)
{
LongArrayList result = new LongArrayList(this.size());
this.forEach(new CollectLongProcedure(longFunction, result));
return result;
}
public MutableShortList collectShort(ShortFunction shortFunction)
{
ShortArrayList result = new ShortArrayList(this.size());
this.forEach(new CollectShortProcedure(shortFunction, result));
return result;
}
@Override
public MutableList collectWith(Function2 function, P parameter)
{
return this.collect(Functions.bind(function, parameter));
}
public MutableList collectIf(Predicate predicate, Function function)
{
return this.collectIf(predicate, function, FastList.newList(this.size()));
}
public MutableList flatCollect(Function> function)
{
return this.flatCollect(function, FastList.newList(this.size()));
}
public MutableList reject(Predicate predicate)
{
return this.reject(predicate, FastList.newList(this.size()));
}
public MutableList select(Predicate predicate)
{
return this.select(predicate, FastList.newList(this.size()));
}
@Override
public RichIterable selectWith(Predicate2 predicate, P parameter)
{
return this.select(Predicates.bind(predicate, parameter));
}
public PartitionMutableList partition(Predicate predicate)
{
PartitionMutableList partitionMutableList = new PartitionFastList();
this.forEach(new PartitionProcedure(predicate, partitionMutableList));
return partitionMutableList;
}
public PartitionMutableList partitionWith(Predicate2 predicate, P parameter)
{
PartitionMutableList partitionMutableList = new PartitionFastList();
this.forEach(new PartitionPredicate2Procedure(predicate, parameter, partitionMutableList));
return partitionMutableList;
}
public MutableList selectInstancesOf(Class clazz)
{
FastList result = FastList.newList(this.size());
this.forEach(new SelectInstancesOfProcedure(clazz, result));
result.trimToSize();
return result;
}
public MutableList> zip(Iterable that)
{
return this.zip(that, FastList.>newList(this.size()));
}
public MutableList> zipWithIndex()
{
return this.zipWithIndex(FastList.>newList(this.size()));
}
public V add(Pair keyValuePair)
{
return this.put(keyValuePair.getOne(), keyValuePair.getTwo());
}
public MutableMap withKeyValue(K key, V value)
{
this.put(key, value);
return this;
}
public MutableMap withAllKeyValues(Iterable> keyValues)
{
for (Pair keyVal : keyValues)
{
this.put(keyVal.getOne(), keyVal.getTwo());
}
return this;
}
public MutableMap withAllKeyValueArguments(Pair... keyValues)
{
return this.withAllKeyValues(ArrayAdapter.adapt(keyValues));
}
public MutableMap withoutKey(K key)
{
this.removeKey(key);
return this;
}
public MutableMap withoutAllKeys(Iterable keys)
{
for (K key : keys)
{
this.removeKey(key);
}
return this;
}
/**
* Trait-style class that is used to capture commonalities between ValuesCollection class implementations in order to
* avoid code duplication.
*/
protected abstract static class ValuesCollectionCommon implements Collection
{
public boolean add(V v)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
public boolean addAll(Collection collection)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
}
public MutableMultimap groupBy(Function function)
{
return this.groupBy(function, FastListMultimap.newMultimap());
}
public MutableMultimap groupByEach(Function> function)
{
return this.groupByEach(function, FastListMultimap.newMultimap());
}
public MutableMap groupByUniqueKey(Function function)
{
throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet");
}
public MutableMap aggregateInPlaceBy(
Function groupBy,
Function0 zeroValueFactory,
Procedure2 mutatingAggregator)
{
MutableMap map = UnifiedMap.newMap();
this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator));
return map;
}
public MutableMap aggregateBy(
Function groupBy,
Function0 zeroValueFactory,
Function2 nonMutatingAggregator)
{
MutableMap map = UnifiedMap.newMap();
this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator));
return map;
}
public V updateValue(K key, Function0 factory, Function function)
{
V oldValue = this.getIfAbsent(key, factory);
V newValue = function.valueOf(oldValue);
this.put(key, newValue);
return newValue;
}
public V updateValueWith(K key, Function0 factory, Function2 function, P parameter)
{
V oldValue = this.getIfAbsent(key, factory);
V newValue = function.value(oldValue, parameter);
this.put(key, newValue);
return newValue;
}
}