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

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

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

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.bag.mutable;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Optional;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.MutableBagIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
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.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.factory.primitive.ObjectDoubleMaps;
import org.eclipse.collections.api.factory.primitive.ObjectLongMaps;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.bag.AbstractBag;
import org.eclipse.collections.impl.block.factory.Predicates2;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.block.factory.Procedures2;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.internal.IterableIterate;

public abstract class AbstractMutableBagIterable
        extends AbstractBag
        implements MutableBagIterable
{
    protected abstract RichIterable getKeysView();

    @Override
    public boolean addAll(Collection source)
    {
        return this.addAllIterable(source);
    }

    @Override
    public boolean addAllIterable(Iterable iterable)
    {
        if (iterable instanceof Bag)
        {
            return this.addAllBag((Bag) iterable);
        }
        int oldSize = this.size();
        Iterate.forEachWith(iterable, Procedures2.addToCollection(), this);
        return oldSize != this.size();
    }

    protected boolean addAllBag(Bag source)
    {
        source.forEachWithOccurrences(this::addOccurrences);
        return source.notEmpty();
    }

    @Override
    public boolean removeAll(Collection collection)
    {
        return this.removeAllIterable(collection);
    }

    @Override
    public boolean retainAll(Collection collection)
    {
        return this.retainAllIterable(collection);
    }

    @Override
    public boolean retainAllIterable(Iterable iterable)
    {
        int oldSize = this.size();
        this.removeIfWith(Predicates2.notIn(), Sets.mutable.withAll(iterable));
        return this.size() != oldSize;
    }

    @Override
    public 

Twin> selectAndRejectWith(Predicate2 predicate, P parameter) { return IterableIterate.selectAndRejectWith(this, predicate, parameter); } @Override public T getFirst() { return this.getKeysView().getFirst(); } @Override public T getLast() { return this.getKeysView().getLast(); } @Override public MutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap(this.size())); } @Override public RichIterable> chunk(int size) { if (size <= 0) { throw new IllegalArgumentException("Size for groups must be positive but was: " + size); } Iterator iterator = this.iterator(); MutableList> result = Lists.mutable.empty(); while (iterator.hasNext()) { MutableCollection batch = this.newEmpty(); for (int i = 0; i < size && iterator.hasNext(); i++) { batch.add(iterator.next()); } result.add(batch); } return result; } @Override public T detect(Predicate predicate) { return this.getKeysView().detect(predicate); } @Override public

T detectWith(Predicate2 predicate, P parameter) { return this.getKeysView().detectWith(predicate, parameter); } @Override public Optional detectOptional(Predicate predicate) { return this.getKeysView().detectOptional(predicate); } @Override public

Optional detectWithOptional(Predicate2 predicate, P parameter) { return this.getKeysView().detectWithOptional(predicate, parameter); } @Override public T detectIfNone(Predicate predicate, Function0 function) { return this.getKeysView().detectIfNone(predicate, function); } @Override public

T detectWithIfNone( Predicate2 predicate, P parameter, Function0 function) { return this.getKeysView().detectIfNone(each -> predicate.accept(each, parameter), function); } @Override public boolean anySatisfy(Predicate predicate) { return this.getKeysView().anySatisfy(predicate); } @Override public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return this.getKeysView().anySatisfyWith(predicate, parameter); } @Override public boolean allSatisfy(Predicate predicate) { return this.getKeysView().allSatisfy(predicate); } @Override public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { return this.getKeysView().allSatisfyWith(predicate, parameter); } @Override public boolean noneSatisfy(Predicate predicate) { return this.getKeysView().noneSatisfy(predicate); } @Override public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return this.getKeysView().noneSatisfyWith(predicate, parameter); } /** * @since 9.0 */ @Override public MutableBag countBy(Function function) { return this.countBy(function, Bags.mutable.empty()); } /** * @since 9.0 */ @Override public MutableBag countByWith(Function2 function, P parameter) { return this.countByWith(function, parameter, Bags.mutable.empty()); } /** * @since 10.0.0 */ @Override public MutableBag countByEach(Function> function) { return this.countByEach(function, Bags.mutable.empty()); } @Override public T min() { return this.getKeysView().min(); } @Override public T min(Comparator comparator) { return this.getKeysView().min(comparator); } @Override public > T minBy(Function function) { return this.getKeysView().minBy(function); } @Override public T max() { return this.getKeysView().max(); } @Override public T max(Comparator comparator) { return this.getKeysView().max(comparator); } @Override public > T maxBy(Function function) { return this.getKeysView().maxBy(function); } @Override public MutableObjectLongMap sumByInt(Function groupBy, IntFunction function) { MutableObjectLongMap result = ObjectLongMaps.mutable.empty(); this.forEachWithOccurrences((each, occurrences) -> result.addToValue( groupBy.valueOf(each), function.intValueOf(each) * (long) occurrences)); return result; } @Override public MutableObjectDoubleMap sumByFloat(Function groupBy, FloatFunction function) { MutableObjectDoubleMap result = ObjectDoubleMaps.mutable.empty(); return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function)); } @Override public MutableObjectLongMap sumByLong(Function groupBy, LongFunction function) { MutableObjectLongMap result = ObjectLongMaps.mutable.empty(); this.forEachWithOccurrences((each, occurrences) -> result.addToValue( groupBy.valueOf(each), function.longValueOf(each) * (long) occurrences)); return result; } @Override public MutableObjectDoubleMap sumByDouble(Function groupBy, DoubleFunction function) { MutableObjectDoubleMap result = ObjectDoubleMaps.mutable.empty(); return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function)); } @Override public MutableList> topOccurrences(int n) { return this.occurrencesSortingBy(n, item -> -item.getTwo(), Lists.mutable.empty()); } @Override public MutableList> bottomOccurrences(int n) { return this.occurrencesSortingBy(n, ObjectIntPair::getTwo, Lists.mutable.empty()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy