/*
* Copyright (c) 2021 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.api;
import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.IntSummaryStatistics;
import java.util.Comparator;
import org.eclipse.collections.api.block.comparator.primitive.ShortComparator;
import org.eclipse.collections.api.bag.primitive.MutableShortBag;
import org.eclipse.collections.api.block.function.primitive.LongShortToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToBooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToByteFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToShortFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToCharFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToDoubleFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToFloatFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToIntFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToLongFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectShortToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ShortToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.ShortPredicate;
import org.eclipse.collections.api.block.procedure.primitive.ShortProcedure;
import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection;
import org.eclipse.collections.api.collection.primitive.MutableByteCollection;
import org.eclipse.collections.api.collection.primitive.MutableCharCollection;
import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection;
import org.eclipse.collections.api.collection.primitive.MutableFloatCollection;
import org.eclipse.collections.api.collection.primitive.MutableIntCollection;
import org.eclipse.collections.api.collection.primitive.MutableLongCollection;
import org.eclipse.collections.api.collection.primitive.MutableShortCollection;
import org.eclipse.collections.api.iterator.ShortIterator;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.set.primitive.ShortSet;
import org.eclipse.collections.api.set.primitive.MutableShortSet;
ShortIterable is an interface which is memory-optimized for short primitives.
It is inspired by the interface RichIterable, and contains a subset of the internal iterator methods on RichIterable like collect, sum, etc.
The API also includes an external iterator method, which returns an ShortIterator. ShortIterator helps iterate over the ShortIterable without boxing the primitives.
This file was automatically generated from template file primitiveIterable.stg.
/**
* ShortIterable is an interface which is memory-optimized for short primitives.
* It is inspired by the interface RichIterable, and contains a subset of the internal iterator methods on RichIterable like collect, sum, etc.
* The API also includes an external iterator method, which returns an ShortIterator. ShortIterator helps iterate over the ShortIterable without boxing the primitives.
* This file was automatically generated from template file primitiveIterable.stg.
*/
public interface ShortIterable extends PrimitiveIterable
{
Returns a primitive iterator that can be used to iterate over the ShortIterable in an
imperative style.
/**
* Returns a primitive iterator that can be used to iterate over the ShortIterable in an
* imperative style.
*/
ShortIterator shortIterator();
Converts the ShortIterable to a primitive short array.
/**
* Converts the ShortIterable to a primitive short array.
*/
short[] toArray();
Converts the ShortIterable to a primitive short array. If the collection fits into the provided array it is used
to store its elements and is returned from the method, otherwise a new array of the appropriate size is allocated
and returned. If the iterable is empty, the target array is returned unchanged.
/**
* Converts the ShortIterable to a primitive short array. If the collection fits into the provided array it is used
* to store its elements and is returned from the method, otherwise a new array of the appropriate size is allocated
* and returned. If the iterable is empty, the target array is returned unchanged.
*/
default short[] toArray(short[] target)
{
return this.toList().toArray(target);
}
Returns true if the value is contained in the ShortIterable, and false if it is not.
/**
* Returns true if the value is contained in the ShortIterable, and false if it is not.
*/
boolean contains(short value);
Returns true if all of the values specified in the source array are contained
in the ShortIterable, and false if they are not.
/**
* Returns true if all of the values specified in the source array are contained
* in the ShortIterable, and false if they are not.
*/
default boolean containsAll(short... source)
{
if (this.size() <= 32 || source.length < 4)
{
for (short item : source)
{
if (!this.contains(item))
{
return false;
}
}
return true;
}
else
{
ShortSet set = this instanceof ShortSet ? (ShortSet) this : this.toSet();
for (short item : source)
{
if (!set.contains(item))
{
return false;
}
}
return true;
}
}
Returns true if all of the values specified in the source ShortIterable are contained
in the ShortIterable, and false if they are not.
/**
* Returns true if all of the values specified in the source ShortIterable are contained
* in the ShortIterable, and false if they are not.
*/
default boolean containsAll(ShortIterable source)
{
if (this.size() <= 32 || source.size() < 4)
{
return source.allSatisfy(this::contains);
}
else
{
ShortSet set = this instanceof ShortSet ? (ShortSet) this : this.toSet();
return source.allSatisfy(set::contains);
}
}
Returns true if any of the values specified in the source array are contained
in the ShortIterable, and false if they are not.
Since: 11.0
/**
* Returns true if any of the values specified in the source array are contained
* in the ShortIterable, and false if they are not.
*
* @since 11.0
*/
default boolean containsAny(short... source)
{
ShortIterable inside = this;
if (this.size() > 32 && source.length > 32 && !(this instanceof ShortSet))
{
inside = this.toSet();
}
for (int i = 0; i < source.length; i++)
{
if (inside.contains(source[i]))
{
return true;
}
}
return false;
}
Returns true if any of the values specified in the source ShortIterable are contained
in the ShortIterable, and false if they are not.
Since: 11.0
/**
* Returns true if any of the values specified in the source ShortIterable are contained
* in the ShortIterable, and false if they are not.
*
* @since 11.0
*/
default boolean containsAny(ShortIterable source)
{
ShortIterable outside = this;
ShortIterable inside = source;
if (this.size() < source.size())
{
outside = source;
inside = this;
}
if (outside instanceof ShortSet)
{
ShortIterable temp = outside;
outside = inside;
inside = temp;
}
else if (inside.size() > 32 && !(inside instanceof ShortSet))
{
inside = inside.toSet();
}
return outside.anySatisfy(inside::contains);
}
Returns true if none of the values specified in the source array are contained
in the ShortIterable, and false if they are.
Since: 11.0
/**
* Returns true if none of the values specified in the source array are contained
* in the ShortIterable, and false if they are.
*
* @since 11.0
*/
default boolean containsNone(short... source)
{
ShortIterable inside = this;
if (this.size() > 32 && source.length > 32 && !(this instanceof ShortSet))
{
inside = this.toSet();
}
for (int i = 0; i < source.length; i++)
{
if (inside.contains(source[i]))
{
return false;
}
}
return true;
}
Returns true if none of the values specified in the source ShortIterable are contained
in the ShortIterable, and false if they are.
Since: 11.0
/**
* Returns true if none of the values specified in the source ShortIterable are contained
* in the ShortIterable, and false if they are.
*
* @since 11.0
*/
default boolean containsNone(ShortIterable source)
{
ShortIterable outside = this;
ShortIterable inside = source;
if (this.size() < source.size())
{
outside = source;
inside = this;
}
if (outside instanceof ShortSet)
{
ShortIterable temp = outside;
outside = inside;
inside = temp;
}
else if (inside.size() > 32 && !(inside instanceof ShortSet))
{
inside = inside.toSet();
}
return outside.noneSatisfy(inside::contains);
}
Applies the ShortProcedure to each element in the ShortIterable.
/**
* Applies the ShortProcedure to each element in the ShortIterable.
*/
void forEach(ShortProcedure procedure);
A synonym for forEach.
Since: 7.0.
/**
* A synonym for forEach.
*
* @since 7.0.
*/
void each(ShortProcedure procedure);
Since: 9.0.
/**
* @since 9.0.
*/
default ShortIterable tap(ShortProcedure procedure)
{
this.forEach(procedure);
return this;
}
Returns a new ShortIterable with all of the elements in the ShortIterable that
return true for the specified predicate.
/**
* Returns a new ShortIterable with all of the elements in the ShortIterable that
* return true for the specified predicate.
*/
ShortIterable select(ShortPredicate predicate);
Returns a new ShortIterable with all of the elements in the ShortIterable that
return false for the specified predicate.
/**
* Returns a new ShortIterable with all of the elements in the ShortIterable that
* return false for the specified predicate.
*/
ShortIterable reject(ShortPredicate predicate);
Same as select(ShortPredicate)
, only the results are added to the target MutableShortCollection. Since: 8.1.
/**
* Same as {@link #select(ShortPredicate)} , only the results are added to the target MutableShortCollection.
*
* @since 8.1.
*/
default <R extends MutableShortCollection> R select(ShortPredicate predicate, R target)
{
this.each(each ->
{
if (predicate.accept(each))
{
target.add(each);
}
});
return target;
}
Same as reject(ShortPredicate)
, only the results are added to the target MutableShortCollection. Since: 8.1.
/**
* Same as {@link #reject(ShortPredicate)} , only the results are added to the target MutableShortCollection.
*
* @since 8.1.
*/
default <R extends MutableShortCollection> R reject(ShortPredicate predicate, R target)
{
this.each(each ->
{
if (!predicate.accept(each))
{
target.add(each);
}
});
return target;
}
Returns a new collection with the results of applying the specified function on each element of the source
collection. This method is also commonly called transform or map.
/**
* Returns a new collection with the results of applying the specified function on each element of the source
* collection. This method is also commonly called transform or map.
*/
<V> RichIterable<V> collect(ShortToObjectFunction<? extends V> function);
Same as collect(ShortToObjectFunction<? extends Object>)
, only the results are added to the target Collection. Since: 8.1.
/**
* Same as {@link #collect(ShortToObjectFunction)} , only the results are added to the target Collection.
*
* @since 8.1.
*/
default <V, R extends Collection<V>> R collect(ShortToObjectFunction<? extends V> function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
flatCollect
is a special case of collect(ShortToObjectFunction<? extends Object>)
. With collect
, when the ShortToObjectFunction
returns a collection, the result is a collection of collections. flatCollect
outputs a single "flattened" collection instead. This method is commonly called flatMap. Since: 8.1.
/**
* {@code flatCollect} is a special case of {@link #collect(ShortToObjectFunction)}. With {@code collect}, when the {@link ShortToObjectFunction} returns
* a collection, the result is a collection of collections. {@code flatCollect} outputs a single "flattened" collection
* instead. This method is commonly called flatMap.
*
* @since 8.1.
*/
default <V, R extends Collection<V>> R flatCollect(ShortToObjectFunction<? extends Iterable<V>> function, R target)
{
this.each(each ->
{
Iterable<V> iterable = function.valueOf(each);
if (iterable instanceof Collection)
{
target.addAll((Collection<V>) iterable);
}
else
{
iterable.forEach(target::add);
}
});
return target;
}
Returns the target MutableBooleanCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableBooleanCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableBooleanCollection> R collectBoolean(ShortToBooleanFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableByteCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableByteCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableByteCollection> R collectByte(ShortToByteFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableCharCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableCharCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableCharCollection> R collectChar(ShortToCharFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableShortCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableShortCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableShortCollection> R collectShort(ShortToShortFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableIntCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableIntCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableIntCollection> R collectInt(ShortToIntFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableFloatCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableFloatCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableFloatCollection> R collectFloat(ShortToFloatFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableLongCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableLongCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableLongCollection> R collectLong(ShortToLongFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
Returns the target MutableDoubleCollection
with the results of applying the specified function on each element of the source collection. Since: 8.1.
/**
* Returns the target {@code MutableDoubleCollection} with the results of applying the specified function on each element
* of the source collection.
*
* @since 8.1.
*/
default <R extends MutableDoubleCollection> R collectDouble(ShortToDoubleFunction function, R target)
{
this.each(each -> target.add(function.valueOf(each)));
return target;
}
short detectIfNone(ShortPredicate predicate, short ifNone);
Returns a count of the number of elements in the ShortIterable that return true for the
specified predicate.
/**
* Returns a count of the number of elements in the ShortIterable that return true for the
* specified predicate.
*/
int count(ShortPredicate predicate);
Returns true if any of the elements in the ShortIterable return true for the
specified predicate, otherwise returns false.
/**
* Returns true if any of the elements in the ShortIterable return true for the
* specified predicate, otherwise returns false.
*/
boolean anySatisfy(ShortPredicate predicate);
Returns true if all of the elements in the ShortIterable return true for the
specified predicate, otherwise returns false.
/**
* Returns true if all of the elements in the ShortIterable return true for the
* specified predicate, otherwise returns false.
*/
boolean allSatisfy(ShortPredicate predicate);
Returns true if none of the elements in the ShortIterable return true for the
specified predicate, otherwise returns false.
/**
* Returns true if none of the elements in the ShortIterable return true for the
* specified predicate, otherwise returns false.
*/
boolean noneSatisfy(ShortPredicate predicate);
Converts the ShortIterable to a new MutableShortList.
/**
* Converts the ShortIterable to a new MutableShortList.
*/
MutableShortList toList();
Converts the ShortIterable to a new MutableShortSet.
/**
* Converts the ShortIterable to a new MutableShortSet.
*/
MutableShortSet toSet();
Converts the ShortIterable to a new MutableShortBag.
/**
* Converts the ShortIterable to a new MutableShortBag.
*/
MutableShortBag toBag();
Returns a LazyShortIterable adapter wrapping the source ShortIterable.
/**
* Returns a LazyShortIterable adapter wrapping the source ShortIterable.
*/
LazyShortIterable asLazy();
<T> T injectInto(T injectedValue, ObjectShortToObjectFunction<? super T, ? extends T> function);
See Also: - reduce(LongShortToLongFunction)
Since: 10.0
/**
* @see #reduce(LongShortToLongFunction)
*
* @since 10.0
*/
default long reduceIfEmpty(LongShortToLongFunction accumulator, long defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
else
{
return this.reduce(accumulator);
}
}
See Also: - reduce.reduce(BinaryOperator)
Since: 10.0
/**
* @see RichIterable#reduce(BinaryOperator)
*
* @since 10.0
*/
default long reduce(LongShortToLongFunction accumulator)
{
boolean[] seenOne = new boolean[1];
long[] result = new long[1];
this.each(each ->
{
if (seenOne[0])
{
result[0] = accumulator.valueOf(result[0], each);
}
else
{
seenOne[0] = true;
result[0] = (long) each;
}
});
if (!seenOne[0])
{
throw new NoSuchElementException();
}
else
{
return result[0];
}
}
Partitions elements in fixed size chunks.
Params: - size – the number of elements per chunk
Returns: A RichIterable
containing ShortIterable
s of size size
, except the last will be truncated if the elements don't divide evenly. Since: 9.2
/**
* Partitions elements in fixed size chunks.
*
* @param size the number of elements per chunk
*
* @return A {@code RichIterable} containing {@code ShortIterable}s of size {@code size}, except the last will be
* truncated if the elements don't divide evenly.
*
* @since 9.2
*/
default RichIterable<ShortIterable> chunk(int size)
{
throw new UnsupportedOperationException("Default method to prevent breaking backwards compatibility");
}
long sum();
Since: 8.0
/**
* @since 8.0
*/
default IntSummaryStatistics summaryStatistics()
{
IntSummaryStatistics stats = new IntSummaryStatistics();
this.forEach(stats::accept);
return stats;
}
short max();
short maxIfEmpty(short defaultValue);
short min();
short minIfEmpty(short defaultValue);
double average();
Since: 9.0
/**
* @since 9.0
*/
default double averageIfEmpty(double defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.average();
}
double median();
Since: 9.0
/**
* @since 9.0
*/
default double medianIfEmpty(double defaultValue)
{
if (this.isEmpty())
{
return defaultValue;
}
return this.median();
}
short[] toSortedArray();
MutableShortList toSortedList();
Converts the collection to a MutableShortList implementation sorted using the provided comparator.
/**
* Converts the collection to a MutableShortList implementation sorted using the provided comparator.
*/
default MutableShortList toSortedList(ShortComparator comparator)
{
return this.toList().sortThis(comparator);
}
Converts the collection to a MutableShortListImplementation sorted based on the natural order of the key returned by function
. /**
* Converts the collection to a MutableShortListImplementation sorted based on the natural order of the key returned
* by {@code function}.
*/
default <T> MutableShortList toSortedListBy(ShortToObjectFunction<T> function)
{
return this.toList().sortThisBy(function);
}
Converts the collection to a MutableShortList implementation, which is sorted based on the key returned by function
using the provided comparator
. /**
* Converts the collection to a MutableShortList implementation, which is sorted based on the key returned by
* {@code function} using the provided {@code comparator}.
*/
default <T> MutableShortList toSortedListBy(ShortToObjectFunction<T> function, Comparator<? super T> comparator)
{
return this.toList().sortThisBy(function, comparator);
}
}