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: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2015 Goldman Sachs.
 * 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.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.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.collection.MutableCollection;
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.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.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.factory.Lists;
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
{
    public ImmutableBag newWithoutAll(Iterable elements)
    {
        return this.reject(Predicates.in(elements));
    }

    public ImmutableBag toImmutable()
    {
        return this;
    }

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

    public 

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

ImmutableBag rejectWith(Predicate2 predicate, P parameter) { return this.reject(Predicates.bind(predicate, parameter)); } public PartitionImmutableBag partition(final Predicate predicate) { final PartitionMutableBag partitionMutableBag = new PartitionHashBag(); this.forEachWithOccurrences(new ObjectIntProcedure() { public void value(T each, int occurrences) { MutableBag bucket = predicate.accept(each) ? partitionMutableBag.getSelected() : partitionMutableBag.getRejected(); bucket.addOccurrences(each, occurrences); } }); return partitionMutableBag.toImmutable(); } public

PartitionImmutableBag partitionWith(final Predicate2 predicate, final P parameter) { final PartitionMutableBag partitionMutableBag = new PartitionHashBag(); this.forEachWithOccurrences(new ObjectIntProcedure() { public void value(T each, int occurrences) { MutableBag bucket = predicate.accept(each, parameter) ? partitionMutableBag.getSelected() : partitionMutableBag.getRejected(); bucket.addOccurrences(each, occurrences); } }); return partitionMutableBag.toImmutable(); } public ImmutableBag collectWith(Function2 function, P parameter) { return this.collect(Functions.bind(function, parameter)); } public ImmutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return this.collectBoolean(booleanFunction, new BooleanHashBag()).toImmutable(); } public ImmutableByteBag collectByte(ByteFunction byteFunction) { return this.collectByte(byteFunction, new ByteHashBag()).toImmutable(); } public ImmutableCharBag collectChar(CharFunction charFunction) { return this.collectChar(charFunction, new CharHashBag()).toImmutable(); } public ImmutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return this.collectDouble(doubleFunction, new DoubleHashBag()).toImmutable(); } public ImmutableFloatBag collectFloat(FloatFunction floatFunction) { return this.collectFloat(floatFunction, new FloatHashBag()).toImmutable(); } public ImmutableIntBag collectInt(IntFunction intFunction) { return this.collectInt(intFunction, new IntHashBag()).toImmutable(); } public ImmutableLongBag collectLong(LongFunction longFunction) { return this.collectLong(longFunction, new LongHashBag()).toImmutable(); } public ImmutableShortBag collectShort(ShortFunction shortFunction) { return this.collectShort(shortFunction, new ShortHashBag()).toImmutable(); } public ImmutableList> topOccurrences(int n) { return this.occurrencesSortingBy(n, new IntFunction>() { public int intValueOf(ObjectIntPair item) { return -item.getTwo(); } }, Lists.fixedSize.>empty() ).toImmutable(); } public ImmutableList> bottomOccurrences(int n) { return this.occurrencesSortingBy(n, new IntFunction>() { public int intValueOf(ObjectIntPair item) { return item.getTwo(); } }, Lists.fixedSize.>empty() ).toImmutable(); } public ImmutableMap groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap()).toImmutable(); } 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