package org.eclipse.collections.impl.bag.immutable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.eclipse.collections.api.bag.ImmutableBag;
import org.eclipse.collections.api.bag.ImmutableBagIterable;
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.procedure.Procedure2;
import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.ImmutableObjectLongMap;
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.impl.bag.AbstractBag;
import org.eclipse.collections.impl.block.factory.PrimitiveFunctions;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import org.eclipse.collections.impl.factory.Bags;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
public abstract class AbstractImmutableBagIterable<T>
extends AbstractBag<T>
implements ImmutableBagIterable<T>
{
@Override
public <K, V> ImmutableMap<K, V> aggregateInPlaceBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Procedure2<? super V, ? super T> mutatingAggregator)
{
MutableMap<K, V> map = UnifiedMap.newMap();
this.forEach(new MutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, mutatingAggregator));
return map.toImmutable();
}
@Override
public <K, V> ImmutableMap<K, V> aggregateBy(
Function<? super T, ? extends K> groupBy,
Function0<? extends V> zeroValueFactory,
Function2<? super V, ? super T, ? extends V> nonMutatingAggregator)
{
MutableMap<K, V> map = UnifiedMap.newMap();
this.forEach(new NonMutatingAggregationProcedure<>(map, groupBy, zeroValueFactory, nonMutatingAggregator));
return map.toImmutable();
}
@Override
public <V> ImmutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function)).toImmutable();
}
@Override
public <V> ImmutableObjectDoubleMap<V> sumByFloat(Function<? super T, ? extends V> groupBy, FloatFunction<? super T> function)
{
MutableObjectDoubleMap<V> result = ObjectDoubleHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByFloatFunction(groupBy, function)).toImmutable();
}
@Override
public <V> ImmutableObjectLongMap<V> sumByLong(Function<? super T, ? extends V> groupBy, LongFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function)).toImmutable();
}
@Override
public <V> ImmutableObjectDoubleMap<V> sumByDouble(Function<? super T, ? extends V> groupBy, DoubleFunction<? super T> function)
{
MutableObjectDoubleMap<V> result = ObjectDoubleHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByDoubleFunction(groupBy, function)).toImmutable();
}
protected void removeAllFrom(Iterable<? extends T> elements, MutableCollection<T> result)
{
if (elements instanceof Set)
{
result.removeAll((Set<?>) elements);
}
else if (elements instanceof List)
{
List<T> toBeRemoved = (List<T>) elements;
if (this.size() * toBeRemoved.size() > 10000)
{
result.removeAll(UnifiedSet.newSet(elements));
}
else
{
result.removeAll(toBeRemoved);
}
}
else
{
result.removeAll(UnifiedSet.newSet(elements));
}
}
@Override
public boolean add(T t)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAll(Collection<? extends T> collection)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot call removeAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot call retainAll() on " + this.getClass().getSimpleName());
}
@Override
public void clear()
{
throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName());
}
@Override
public <V> ImmutableBag<V> countBy(Function<? super T, ? extends V> function)
{
return this.countBy(function, Bags.mutable.<V>empty()).toImmutable();
}
@Override
public <V, P> ImmutableBag<V> countByWith(Function2<? super T, ? super P, ? extends V> function, P parameter)
{
return this.countByWith(function, parameter, Bags.mutable.<V>empty()).toImmutable();
}
@Override
public <V> ImmutableBag<V> countByEach(Function<? super T, ? extends Iterable<V>> function)
{
return this.countByEach(function, Bags.mutable.empty()).toImmutable();
}
@Override
public Stream<T> stream()
{
return StreamSupport.stream(this.spliterator(), false);
}
@Override
public Stream<T> parallelStream()
{
return StreamSupport.stream(this.spliterator(), true);
}
@Override
public Spliterator<T> spliterator()
{
return Spliterators.spliterator(this, 0);
}
@Override
public Collection<T> castToCollection()
{
return this;
}
}