
org.eclipse.collections.impl.bag.immutable.AbstractImmutableBag Maven / Gradle / Ivy
/*
* 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 extends T> elements)
{
return this.reject(Predicates.in(elements));
}
@Override
public ImmutableBag toImmutable()
{
return this;
}
@Override
public ImmutableBag tap(Procedure super T> procedure)
{
this.forEach(procedure);
return this;
}
@Override
public ImmutableBag selectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.select(Predicates.bind(predicate, parameter));
}
@Override
public ImmutableBag rejectWith(Predicate2 super T, ? super P> predicate, P parameter)
{
return this.reject(Predicates.bind(predicate, parameter));
}
@Override
public PartitionImmutableBag partition(Predicate super T> 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 super T, ? super P> 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 super T, ? extends V> function)
{
return this.collect(function);
}
/**
* @since 9.0
*/
@Override
public ImmutableBag countByWith(Function2 super T, ? super P, ? extends V> function, P parameter)
{
return this.collectWith(function, parameter);
}
/**
* @since 10.0.0
*/
@Override
public ImmutableBag countByEach(Function super T, ? extends Iterable> function)
{
return this.flatCollect(function);
}
@Override
public ImmutableBag collectWithOccurrences(ObjectIntToObjectFunction super T, ? extends V> function)
{
return this.collectWithOccurrences(function, Bags.mutable.empty()).toImmutable();
}
@Override
public ImmutableBag collectWith(Function2 super T, ? super P, ? extends V> function, P parameter)
{
return this.collect(Functions.bind(function, parameter));
}
@Override
public ImmutableBooleanBag collectBoolean(BooleanFunction super T> booleanFunction)
{
return this.collectBoolean(booleanFunction, BooleanBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableByteBag collectByte(ByteFunction super T> byteFunction)
{
return this.collectByte(byteFunction, ByteBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableCharBag collectChar(CharFunction super T> charFunction)
{
return this.collectChar(charFunction, CharBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableDoubleBag collectDouble(DoubleFunction super T> doubleFunction)
{
return this.collectDouble(doubleFunction, DoubleBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableFloatBag collectFloat(FloatFunction super T> floatFunction)
{
return this.collectFloat(floatFunction, FloatBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableIntBag collectInt(IntFunction super T> intFunction)
{
return this.collectInt(intFunction, IntBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableLongBag collectLong(LongFunction super T> longFunction)
{
return this.collectLong(longFunction, LongBags.mutable.empty()).toImmutable();
}
@Override
public ImmutableShortBag collectShort(ShortFunction super T> 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 super T, ? extends V> 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();
}
}