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

com.gs.collections.impl.bag.mutable.AbstractMutableBagIterable 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 2015 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.bag.mutable;

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

import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.MutableBagIterable;
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.IntFunction;
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.list.MutableList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.api.tuple.primitive.ObjectIntPair;
import com.gs.collections.impl.bag.AbstractBag;
import com.gs.collections.impl.block.factory.Predicates2;
import com.gs.collections.impl.block.factory.Procedures2;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.internal.IterableIterate;

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

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

    public boolean addAllIterable(Iterable iterable)
    {
        int oldSize = this.size();
        Iterate.forEachWith(iterable, Procedures2.addToCollection(), this);
        return oldSize != this.size();
    }

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

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

    public boolean retainAllIterable(Iterable iterable)
    {
        int oldSize = this.size();
        this.removeIfWith(Predicates2.notIn(), UnifiedSet.newSet(iterable));
        return this.size() != oldSize;
    }

    public 

Twin> selectAndRejectWith(Predicate2 predicate, P parameter) { return IterableIterate.selectAndRejectWith(this, predicate, parameter); } public T getFirst() { return this.getKeysView().getFirst(); } public T getLast() { return this.getKeysView().getLast(); } public MutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap()); } 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 T detectIfNone(Predicate predicate, Function0 function) { return this.getKeysView().detectIfNone(predicate, function); } @Override public

T detectWithIfNone( final Predicate2 predicate, final P parameter, Function0 function) { return this.getKeysView().detectIfNone(new Predicate() { public boolean accept(T each) { return 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); } @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); } 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 MutableList> topOccurrences(int n) { return this.occurrencesSortingBy(n, new IntFunction>() { public int intValueOf(ObjectIntPair item) { return -item.getTwo(); } }, Lists.mutable.>empty()); } public MutableList> bottomOccurrences(int n) { return this.occurrencesSortingBy(n, new IntFunction>() { public int intValueOf(ObjectIntPair item) { return item.getTwo(); } }, Lists.mutable.>empty()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy