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

org.eclipse.collections.impl.bag.immutable.AbstractImmutableBag 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.bag.immutable;

import java.util.Iterator;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.ImmutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.ImmutableByteBag;
import org.eclipse.collections.api.bag.primitive.ImmutableCharBag;
import org.eclipse.collections.api.bag.primitive.ImmutableDoubleBag;
import org.eclipse.collections.api.bag.primitive.ImmutableFloatBag;
import org.eclipse.collections.api.bag.primitive.ImmutableIntBag;
import org.eclipse.collections.api.bag.primitive.ImmutableLongBag;
import org.eclipse.collections.api.bag.primitive.ImmutableShortBag;
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.collection.MutableCollection;
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.factory.Lists;
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.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.partition.bag.PartitionImmutableBag;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.partition.bag.PartitionHashBag;

/**
 * @since 1.0
 */
public abstract class AbstractImmutableBag
        extends AbstractImmutableBagIterable
        implements ImmutableBag
{
    @Override
    public ImmutableBag newWithoutAll(Iterable elements)
    {
        return this.reject(Predicates.in(elements));
    }

    @Override
    public ImmutableBag toImmutable()
    {
        return this;
    }

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

    @Override
    public 

ImmutableBag selectWith(Predicate2 predicate, P parameter) { return this.select(Predicates.bind(predicate, parameter)); } @Override public

ImmutableBag rejectWith(Predicate2 predicate, P parameter) { return this.reject(Predicates.bind(predicate, parameter)); } @Override public PartitionImmutableBag partition(Predicate predicate) { PartitionMutableBag partitionMutableBag = new PartitionHashBag<>(); this.forEachWithOccurrences((each, occurrences) -> { MutableBag bucket = predicate.accept(each) ? partitionMutableBag.getSelected() : partitionMutableBag.getRejected(); bucket.addOccurrences(each, occurrences); }); return partitionMutableBag.toImmutable(); } @Override public

PartitionImmutableBag partitionWith(Predicate2 predicate, P parameter) { PartitionMutableBag partitionMutableBag = new PartitionHashBag<>(); this.forEachWithOccurrences((each, occurrences) -> { MutableBag bucket = predicate.accept(each, parameter) ? partitionMutableBag.getSelected() : partitionMutableBag.getRejected(); bucket.addOccurrences(each, occurrences); }); return partitionMutableBag.toImmutable(); } /** * @since 9.0 */ @Override public ImmutableBag countBy(Function function) { return this.collect(function); } /** * @since 9.0 */ @Override public ImmutableBag countByWith(Function2 function, P parameter) { return this.collectWith(function, parameter); } /** * @since 10.0.0 */ @Override public ImmutableBag countByEach(Function> function) { return this.flatCollect(function); } @Override public ImmutableBag collectWithOccurrences(ObjectIntToObjectFunction function) { return this.collectWithOccurrences(function, Bags.mutable.empty()).toImmutable(); } @Override public ImmutableBag collectWith(Function2 function, P parameter) { return this.collect(Functions.bind(function, parameter)); } @Override public ImmutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return this.collectBoolean(booleanFunction, BooleanBags.mutable.empty()).toImmutable(); } @Override public ImmutableByteBag collectByte(ByteFunction byteFunction) { return this.collectByte(byteFunction, ByteBags.mutable.empty()).toImmutable(); } @Override public ImmutableCharBag collectChar(CharFunction charFunction) { return this.collectChar(charFunction, CharBags.mutable.empty()).toImmutable(); } @Override public ImmutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return this.collectDouble(doubleFunction, DoubleBags.mutable.empty()).toImmutable(); } @Override public ImmutableFloatBag collectFloat(FloatFunction floatFunction) { return this.collectFloat(floatFunction, FloatBags.mutable.empty()).toImmutable(); } @Override public ImmutableIntBag collectInt(IntFunction intFunction) { return this.collectInt(intFunction, IntBags.mutable.empty()).toImmutable(); } @Override public ImmutableLongBag collectLong(LongFunction longFunction) { return this.collectLong(longFunction, LongBags.mutable.empty()).toImmutable(); } @Override public ImmutableShortBag collectShort(ShortFunction shortFunction) { return this.collectShort(shortFunction, ShortBags.mutable.empty()).toImmutable(); } @Override public ImmutableList> topOccurrences(int n) { MutableList> result = this.occurrencesSortingBy( n, item -> -item.getTwo(), Lists.fixedSize.empty()); return result.toImmutable(); } @Override public ImmutableList> bottomOccurrences(int n) { MutableList> result = this.occurrencesSortingBy( n, ObjectIntPair::getTwo, Lists.fixedSize.empty()); return result.toImmutable(); } @Override public ImmutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap(this.size())).toImmutable(); } @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 = Bags.mutable.empty(); for (int i = 0; i < size && iterator.hasNext(); i++) { batch.add(iterator.next()); } result.add(batch.toImmutable()); } return result.toImmutable(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy