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

com.gs.collections.api.map.MutableMap 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 2012 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.api.map;

import java.util.Collection;
import java.util.Map;

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.MutableCollection;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableByteCollection;
import com.gs.collections.api.collection.primitive.MutableCharCollection;
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.collection.primitive.MutableLongCollection;
import com.gs.collections.api.collection.primitive.MutableShortCollection;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.partition.PartitionMutableCollection;
import com.gs.collections.api.tuple.Pair;

/**
 * A MutableMap is similar to a JCF Map but adds additional useful internal iterator methods. The MutableMap interface
 * additionally implements some of the methods in the Smalltalk Dictionary protocol.
 */
public interface MutableMap
        extends UnsortedMapIterable, Map, Cloneable
{
    /**
     * Creates a new instance of the same type, using the default capacity and growth parameters.
     */
    MutableMap newEmpty();

    /**
     * Adds all the entries derived from {@code collection} to {@code this}. The key and value for each entry
     * is determined by applying the {@code keyFunction} and {@code valueFunction} to each item in
     * {@code collection}. Any entry in {@code map} that has the same key as an entry in {@code this}
     * will have it's value replaced by that in {@code map}.
     */
     MutableMap collectKeysAndValues(
            Collection collection,
            Function keyFunction,
            Function valueFunction);

    /**
     * Remove an entry from the map at the specified {@code key}.
     *
     * @return The value removed from entry at key, or null if not found.
     * @see #remove(Object)
     */
    V removeKey(K key);

    /**
     * Get and return the value in the Map at the specified key.  Alternatively, if there is no value in the map at the key,
     * return the result of evaluating the specified Function0, and put that value in the map at the specified key.
     */
    V getIfAbsentPut(K key, Function0 function);

    /**
     * Get and return the value in the Map at the specified key.  Alternatively, if there is no value in the map for that key
     * return the result of evaluating the specified Function using the specified key, and put that value in the
     * map at the specified key.
     */
    V getIfAbsentPutWithKey(K key, Function function);

    /**
     * Get and return the value in the Map at the specified key.  Alternatively, if there is no value in the map for that key
     * return the result of evaluating the specified Function using the specified parameter, and put that value in the
     * map at the specified key.
     */
    

V getIfAbsentPutWith(K key, Function function, P parameter); MutableMap clone(); /** * Returns an unmodifiable view of this map. This method allows modules to provide users with "read-only" access to * internal maps. Any query operations on the returned map that "read through" to this map and attempt to modify the * returned map, whether direct or via its iterator, result in an {@link UnsupportedOperationException}. * The returned map will be Serializable if this map is Serializable. * * @return an unmodifiable view of this map. */ MutableMap asUnmodifiable(); /** * Returns an immutable copy of this map. If the map is immutable, it returns itself. *

* The returned map will be Serializable if this map is Serializable. */ ImmutableMap toImmutable(); /** * Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is * critical that all access to the backing map is accomplished through the returned map.

*

* It is imperative that the user manually synchronize on the returned map when iterating over any of its collection * views: *

     *  MutableMap map = myMutableMap.asSynchronized();
     *      ...
     *  Set set = map.keySet();  // Needn't be in synchronized block
     *      ...
     *  synchronized(map)
     *  {  // Synchronizing on map, not set!
     *      Iterator i = s.iterator(); // Must be in synchronized block
     *      while (i.hasNext())
     *          foo(i.next());
     *  }
     * 
* Failure to follow this advice may result in non-deterministic behavior. *

* The preferred way of iterating over a synchronized collection is to use the collection.forEach() method which is * properly synchronized internally. *

     *  MutableMap map = myMutableMap.asSynchronized();
     *      ...
     *  Set set = map.keySet();  // Needn't be in synchronized block
     *     ...
     *  Iterate.forEach(set, new Procedure()
     *  {
     *      public void value(Object each)
     *      {
     *          ...
     *      }
     *  });
     * 
*

*

The returned map will be serializable if the specified map is serializable. */ MutableMap asSynchronized(); MutableMap select(Predicate2 predicate); MutableMap collectValues(Function2 function); MutableMap collect(Function2> function); MutableMap reject(Predicate2 predicate); MutableCollection collect(Function function); MutableBooleanCollection collectBoolean(BooleanFunction booleanFunction); MutableByteCollection collectByte(ByteFunction byteFunction); MutableCharCollection collectChar(CharFunction charFunction); MutableDoubleCollection collectDouble(DoubleFunction doubleFunction); MutableFloatCollection collectFloat(FloatFunction floatFunction); MutableIntCollection collectInt(IntFunction intFunction); MutableLongCollection collectLong(LongFunction longFunction); MutableShortCollection collectShort(ShortFunction shortFunction); MutableCollection collectIf(Predicate predicate, Function function); MutableCollection flatCollect(Function> function); MutableCollection reject(Predicate predicate); MutableCollection select(Predicate predicate); PartitionMutableCollection partition(Predicate predicate); MutableCollection selectInstancesOf(Class clazz); MutableCollection> zip(Iterable that); MutableCollection> zipWithIndex(); /** * This method allows mutable, fixed size, and immutable maps the ability to add elements to their existing * elements. In order to support fixed size maps, a new instance of a map would have to be returned including the * keys and values of the original plus the additional key and value. In the case of mutable maps, the original map * is modified and then returned. In order to use this method properly with mutable and fixed size maps the * following approach must be taken: *

*

     * map = map.withKeyValue("new key", "new value");
     * 
* In the case of FixedSizeMap, a new instance will be returned by withKeyValue, and any variables that * previously referenced the original map will need to be redirected to reference the new instance. In the case * of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap * will both return "this" after calling put on themselves. * * @see #put(Object, Object) */ MutableMap withKeyValue(K key, V value); /** * This method allows mutable, fixed size, and immutable maps the ability to add elements to their existing * elements. In order to support fixed size maps, a new instance of a map would have to be returned including the * keys and values of the original plus all of the additional keys and values. In the case of mutable maps, the * original map is modified and then returned. In order to use this method properly with mutable and fixed size * maps the following approach must be taken: *

*

     * map = map.withAllKeyValues(FastList.newListWith(PairImpl.of("new key", "new value")));
     * 
* In the case of FixedSizeMap, a new instance will be returned by withAllKeyValues, and any variables that * previously referenced the original map will need to be redirected to reference the new instance. In the case * of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap * will both return "this" after calling put on themselves. * * @see #put(Object, Object) */ MutableMap withAllKeyValues(Iterable> keyValues); /** * Convenience var-args version of withAllKeyValues * * @see #withAllKeyValues(Iterable) */ MutableMap withAllKeyValueArguments(Pair... keyValuePairs); /** * This method allows mutable, fixed size, and immutable maps the ability to remove elements from their existing * elements. In order to support fixed size maps, a new instance of a map would have to be returned including the * keys and values of the original minus the key and value to be removed. In the case of mutable maps, the original * map is modified and then returned. In order to use this method properly with mutable and fixed size maps the * following approach must be taken: *

*

     * map = map.withoutKey("key");
     * 
* In the case of FixedSizeMap, a new instance will be returned by withoutKey, and any variables that previously * referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap * or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return * "this" after calling remove on themselves. * * @see #remove(Object) */ MutableMap withoutKey(K key); /** * This method allows mutable, fixed size, and immutable maps the ability to remove elements from their existing * elements. In order to support fixed size maps, a new instance of a map would have to be returned including the * keys and values of the original minus all of the keys and values to be removed. In the case of mutable maps, the * original map is modified and then returned. In order to use this method properly with mutable and fixed size * maps the following approach must be taken: *

*

     * map = map.withoutAllKeys(FastList.newListWith("key1", "key2"));
     * 
* In the case of FixedSizeMap, a new instance will be returned by withoutAllKeys, and any variables that previously * referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap * or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return * "this" after calling remove on themselves. * * @see #remove(Object) */ MutableMap withoutAllKeys(Iterable keys); MutableMultimap groupBy(Function function); MutableMultimap groupByEach(Function> function); MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator); MutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator); /** * Looks up the value associated with {@code key}, applies the {@code function} to it, and replaces the value. If there * is no value associated with {@code key}, starts it off with a value supplied by {@code factory}. */ V updateValue(K key, Function0 factory, Function function); /** * Same as {@link #updateValue(Object, Function0, Function)} with a Function2 and specified parameter which is * passed to the function. */

V updateValueWith(K key, Function0 factory, Function2 function, P parameter); }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy