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

org.eclipse.collections.impl.bag.mutable.AbstractMutableBag Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2018 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.bag.mutable;

import java.util.Collection;
import java.util.concurrent.ExecutorService;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.annotation.Beta;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.ParallelUnsortedBag;
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.ObjectIntToObjectFunction;
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.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.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.lazy.parallel.bag.NonParallelUnsortedBag;
import org.eclipse.collections.impl.partition.bag.PartitionHashBag;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.utility.Iterate;

public abstract class AbstractMutableBag
        extends AbstractMutableBagIterable
        implements MutableBag
{
    @Override
    public ImmutableBag toImmutable()
    {
        return Bags.immutable.withAll(this);
    }

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

    @Override
    public SynchronizedBag asSynchronized()
    {
        return new SynchronizedBag<>(this);
    }

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

    @Override
    public  MutableBag selectInstancesOf(Class clazz)
    {
        MutableBag result = HashBag.newBag();
        this.forEachWithOccurrences((each, occurrences) -> {
            if (clazz.isInstance(each))
            {
                result.addOccurrences((S) each, occurrences);
            }
        });
        return result;
    }

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

    @Override
    public 

MutableBag selectWith(Predicate2 predicate, P parameter) { return this.selectWith(predicate, parameter, this.newEmpty()); } @Override public MutableBag reject(Predicate predicate) { return this.reject(predicate, this.newEmpty()); } @Override public

MutableBag rejectWith(Predicate2 predicate, P parameter) { return this.rejectWith(predicate, parameter, this.newEmpty()); } @Override public PartitionMutableBag partition(Predicate predicate) { PartitionMutableBag result = new PartitionHashBag<>(); this.forEachWithOccurrences((each, index) -> { MutableBag bucket = predicate.accept(each) ? result.getSelected() : result.getRejected(); bucket.addOccurrences(each, index); }); return result; } @Override public

PartitionMutableBag partitionWith(Predicate2 predicate, P parameter) { PartitionMutableBag result = new PartitionHashBag<>(); this.forEachWithOccurrences((each, index) -> { MutableBag bucket = predicate.accept(each, parameter) ? result.getSelected() : result.getRejected(); bucket.addOccurrences(each, index); }); return result; } /** * @since 9.0 */ @Override public MutableBag countBy(Function function) { return this.collect(function); } /** * @since 9.0 */ @Override public MutableBag countByWith(Function2 function, P parameter) { return this.collectWith(function, parameter); } /** * @since 10.0.0 */ @Override public MutableBag countByEach(Function> function) { return this.flatCollect(function); } @Override public MutableBag collectWithOccurrences(ObjectIntToObjectFunction function) { return this.collectWithOccurrences(function, Bags.mutable.empty()); } @Override public MutableBag collect(Function function) { return this.collect(function, HashBag.newBag()); } @Override public MutableBag collectWith(Function2 function, P parameter) { return this.collectWith(function, parameter, HashBag.newBag()); } @Override public MutableBag collectIf(Predicate predicate, Function function) { return this.collectIf(predicate, function, HashBag.newBag()); } @Override public MutableBag flatCollect(Function> function) { return this.flatCollect(function, HashBag.newBag()); } @Override public MutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return this.collectBoolean(booleanFunction, new BooleanHashBag()); } @Override public MutableByteBag collectByte(ByteFunction byteFunction) { return this.collectByte(byteFunction, new ByteHashBag()); } @Override public MutableCharBag collectChar(CharFunction charFunction) { return this.collectChar(charFunction, new CharHashBag()); } @Override public MutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return this.collectDouble(doubleFunction, new DoubleHashBag()); } @Override public MutableFloatBag collectFloat(FloatFunction floatFunction) { return this.collectFloat(floatFunction, new FloatHashBag()); } @Override public MutableIntBag collectInt(IntFunction intFunction) { return this.collectInt(intFunction, new IntHashBag()); } @Override public MutableLongBag collectLong(LongFunction longFunction) { return this.collectLong(longFunction, new LongHashBag()); } @Override public MutableShortBag collectShort(ShortFunction shortFunction) { return this.collectShort(shortFunction, new ShortHashBag()); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Override @Deprecated public MutableBag> zip(Iterable that) { if (that instanceof Collection || that instanceof RichIterable) { int thatSize = Iterate.sizeOf(that); HashBag> target = HashBag.newBag(Math.min(this.size(), thatSize)); return this.zip(that, target); } return this.zip(that, HashBag.newBag()); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Override @Deprecated public MutableSet> zipWithIndex() { return this.zipWithIndex(UnifiedSet.newSet()); } @Beta public ParallelUnsortedBag asParallel(ExecutorService executorService, int batchSize) { if (executorService == null) { throw new NullPointerException(); } if (batchSize < 1) { throw new IllegalArgumentException(); } return new NonParallelUnsortedBag<>(this); } }