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

org.eclipse.collections.impl.map.mutable.AbstractMutableMap Maven / Gradle / Ivy

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2022 Goldman Sachs and others.
 * 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.mutable;

import java.util.Collection;

import org.eclipse.collections.api.bag.MutableBag;
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.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.factory.Bags;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.factory.primitive.BooleanBags;
import org.eclipse.collections.api.factory.primitive.ByteBags;
import org.eclipse.collections.api.factory.primitive.CharBags;
import org.eclipse.collections.api.factory.primitive.DoubleBags;
import org.eclipse.collections.api.factory.primitive.FloatBags;
import org.eclipse.collections.api.factory.primitive.IntBags;
import org.eclipse.collections.api.factory.primitive.LongBags;
import org.eclipse.collections.api.factory.primitive.ShortBags;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.multimap.set.MutableSetMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
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.list.fixed.ArrayAdapter;
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.utility.MapIterate;

public abstract class AbstractMutableMap extends AbstractMutableMapIterable
        implements MutableMap
{
    @Override
    @SuppressWarnings("AbstractMethodOverridesAbstractMethod")
    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);

    @Override
    public MutableMap asUnmodifiable()
    {
        return UnmodifiableMutableMap.of(this);
    }

    @Override
    public ImmutableMap toImmutable()
    {
        return Maps.immutable.withAll(this);
    }

    @Override
    public MutableMap asSynchronized()
    {
        return SynchronizedMutableMap.of(this);
    }

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

    @Override
    public  MutableMap collectValues(Function2 function)
    {
        return MapIterate.collectValues(this, function, this.newEmpty(this.size()));
    }

    @Override
    public MutableMap select(Predicate2 predicate)
    {
        return MapIterate.selectMapOnEntry(this, predicate, this.newEmpty());
    }

    @Override
    public MutableMap reject(Predicate2 predicate)
    {
        return MapIterate.rejectMapOnEntry(this, predicate, this.newEmpty());
    }

    @Override
    public  MutableBag collect(Function function)
    {
        return this.collect(function, Bags.mutable.empty());
    }

    @Override
    public MutableBooleanBag collectBoolean(BooleanFunction booleanFunction)
    {
        return this.collectBoolean(booleanFunction, BooleanBags.mutable.empty());
    }

    @Override
    public MutableByteBag collectByte(ByteFunction byteFunction)
    {
        return this.collectByte(byteFunction, ByteBags.mutable.empty());
    }

    @Override
    public MutableCharBag collectChar(CharFunction charFunction)
    {
        return this.collectChar(charFunction, CharBags.mutable.empty());
    }

    @Override
    public MutableDoubleBag collectDouble(DoubleFunction doubleFunction)
    {
        return this.collectDouble(doubleFunction, DoubleBags.mutable.empty());
    }

    @Override
    public MutableFloatBag collectFloat(FloatFunction floatFunction)
    {
        return this.collectFloat(floatFunction, FloatBags.mutable.empty());
    }

    @Override
    public MutableIntBag collectInt(IntFunction intFunction)
    {
        return this.collectInt(intFunction, IntBags.mutable.empty());
    }

    @Override
    public MutableLongBag collectLong(LongFunction longFunction)
    {
        return this.collectLong(longFunction, LongBags.mutable.empty());
    }

    @Override
    public MutableShortBag collectShort(ShortFunction shortFunction)
    {
        return this.collectShort(shortFunction, ShortBags.mutable.empty());
    }

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

    @Override
    public  MutableBag collectIf(Predicate predicate, Function function)
    {
        return this.collectIf(predicate, function, Bags.mutable.empty());
    }

    @Override
    public  MutableBag flatCollect(Function> function)
    {
        return this.flatCollect(function, Bags.mutable.empty());
    }

    @Override
    public MutableBag select(Predicate predicate)
    {
        return this.select(predicate, Bags.mutable.empty());
    }

    @Override
    public MutableMap tap(Procedure procedure)
    {
        this.forEach(procedure);
        return this;
    }

    @Override
    public 

MutableBag selectWith(Predicate2 predicate, P parameter) { return this.select(Predicates.bind(predicate, parameter)); } @Override public MutableBag reject(Predicate predicate) { return this.reject(predicate, Bags.mutable.empty()); } @Override public

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

PartitionMutableBag partitionWith(Predicate2 predicate, P parameter) { PartitionMutableBag partitionMutableBag = new PartitionHashBag<>(); this.forEach(new PartitionPredicate2Procedure<>(predicate, parameter, partitionMutableBag)); return partitionMutableBag; } @Override public MutableBag selectInstancesOf(Class clazz) { MutableBag result = Bags.mutable.empty(); this.forEach(new SelectInstancesOfProcedure<>(clazz, result)); return result; } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public MutableBag> zip(Iterable that) { return this.zip(that, Bags.mutable.empty()); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public MutableSet> zipWithIndex() { return this.zipWithIndex(new UnifiedSet<>(this.size())); } @Override public MutableMap withKeyValue(K key, V value) { this.put(key, value); return this; } @Override public MutableMap withAllKeyValues(Iterable> keyValues) { for (Pair keyVal : keyValues) { this.put(keyVal.getOne(), keyVal.getTwo()); } return this; } @Override public MutableMap withAllKeyValueArguments(Pair... keyValues) { return this.withAllKeyValues(ArrayAdapter.adapt(keyValues)); } @Override public MutableMap withoutKey(K key) { this.removeKey(key); return this; } @Override 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 { @Override public boolean add(V v) { throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName()); } @Override public boolean addAll(Collection collection) { throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName()); } } @Override public MutableBagMultimap groupBy(Function function) { return this.groupBy(function, HashBagMultimap.newMultimap()); } @Override public MutableBagMultimap groupByEach(Function> function) { return this.groupByEach(function, HashBagMultimap.newMultimap()); } @Override public MutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap(this.size())); } }