本文整理汇总了Java中java.util.function.IntFunction.apply方法的典型用法代码示例。如果您正苦于以下问题:Java IntFunction.apply方法的具体用法?Java IntFunction.apply怎么用?Java IntFunction.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.function.IntFunction
的用法示例。
在下文中一共展示了IntFunction.apply方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: flatMap
import java.util.function.IntFunction; //导入方法依赖的package包/类
@Override
public final IntStream flatMap(IntFunction<? extends IntStream> mapper) {
Objects.requireNonNull(mapper);
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
return new Sink.ChainedInt<Integer>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(int t) {
try (IntStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
示例2: flatMap
import java.util.function.IntFunction; //导入方法依赖的package包/类
@Override
public final IntStream flatMap(IntFunction<? extends IntStream> mapper) {
return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
return new Sink.ChainedInt<Integer>(sink) {
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(int t) {
try (IntStream result = mapper.apply(t)) {
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it
if (result != null)
result.sequential().forEach(i -> downstream.accept(i));
}
}
};
}
};
}
示例3: asArray
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* Create a new array using the specified array factory, and copy the
* elements into it.
*/
public E[] asArray(IntFunction<E[]> arrayFactory) {
long size = count();
if (size >= Nodes.MAX_ARRAY_SIZE)
throw new IllegalArgumentException(Nodes.BAD_SIZE);
E[] result = arrayFactory.apply((int) size);
copyInto(result, 0);
return result;
}
示例4: readArray
import java.util.function.IntFunction; //导入方法依赖的package包/类
public <T> T[] readArray(Writeable.Reader<T> reader, IntFunction<T[]> arraySupplier) throws IOException {
int length = readArraySize();
T[] values = arraySupplier.apply(length);
for (int i = 0; i < length; i++) {
values[i] = reader.read(this);
}
return values;
}
示例5: asArray
import java.util.function.IntFunction; //导入方法依赖的package包/类
@Override
public T[] asArray(IntFunction<T[]> generator) {
long size = count();
if (size >= MAX_ARRAY_SIZE)
throw new IllegalArgumentException(BAD_SIZE);
T[] array = generator.apply((int) size);
copyInto(array, 0);
return array;
}
示例6: asArray
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*
* @implSpec the default implementation invokes the generator to create
* an instance of a boxed primitive array with a length of
* {@link #count()} and then invokes {@link #copyInto(T[], int)} with
* that array at an offset of 0.
*/
@Override
default T[] asArray(IntFunction<T[]> generator) {
if (java.util.stream.Tripwire.ENABLED)
java.util.stream.Tripwire.trip(getClass(), "{0} calling Node.OfPrimitive.asArray");
long size = count();
if (size >= Nodes.MAX_ARRAY_SIZE)
throw new IllegalArgumentException(Nodes.BAD_SIZE);
T[] boxed = generator.apply((int) count());
copyInto(boxed, 0);
return boxed;
}
示例7: readSliceFrom
import java.util.function.IntFunction; //导入方法依赖的package包/类
public static Bytes readSliceFrom(InputStream streamToDrain, int offset, int len, IntFunction<Bytes.BuilderStream> builderFactory) throws IOException {
// the implementation is based on commons-io IOUtils.copyLarge
if (len == 0) {
return Bytes.empty();
}
if (offset > 0) {
final long skipped = streamToDrain.skip(offset);
if (skipped < offset) return Bytes.empty();
}
byte[] readBuffer = new byte[len > 4096 ? 4096 : len];
final int bufferLength = readBuffer.length;
int bytesToRead = bufferLength;
if (len > 0 && len < bufferLength) {
bytesToRead = len;
}
final Bytes.BuilderStream builder = builderFactory.apply(len);
int read;
long totalRead = 0;
while (bytesToRead > 0 && -1 != (read = streamToDrain.read(readBuffer, 0, bytesToRead))) {
builder.write(readBuffer, 0, read);
totalRead += read;
if (len > 0) { // only adjust length if not reading to the end
// Note the cast must work because buffer.length is an integer
bytesToRead = (int) Math.min(len - totalRead, bufferLength);
}
}
return builder.toBytes();
}
示例8: fromInteger
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* Creates an INTEGER function that wraps to function provided
* @param function the function to wrap
* @param <O> the output type
* @return the newly created function wrapper
*/
public static <O> Function2<Integer,O> fromInteger(IntFunction<O> function) {
return new Function2<Integer,O>(FunctionStyle.INTEGER) {
@Override
public final O apply(int input) {
return function.apply(input);
}
};
}
示例9: forInt
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* Creates an INTEGER Printer that wraps the function provided
* @param function the function to wrap
* @return the newly created function Printer
*/
public static Printer<Integer> forInt(IntFunction<String> function) {
return new Printer<Integer>(FunctionStyle.INTEGER, DEFAULT_NULL) {
@Override
public final String apply(int input) {
return function.apply(input);
}
};
}
示例10: create
import java.util.function.IntFunction; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private static <T> T create(Map<Class<?>, IntFunction<?>> map, Class<T> clazz, int size)
{
IntFunction<?> intFunction = map.get(clazz);
if (intFunction != null)
{
return (T) intFunction.apply(size);
}
if ((! Modifier.isAbstract(clazz.getModifiers())) && (Map.class.isAssignableFrom(clazz) || Collection.class.isAssignableFrom(clazz)))
{
ConstructorInvoker<T> constructor = DioriteReflectionUtils.getConstructor(clazz, false);
if (constructor != null)
{
constructor.ensureAccessible();
IntFunction<T> creator = constructor::invokeWith;
map.put(clazz, creator);
return creator.apply(size);
}
}
for (Entry<Class<?>, IntFunction<?>> entry : map.entrySet())
{
if (clazz.isAssignableFrom(entry.getKey()))
{
IntFunction<?> function = entry.getValue();
map.put(clazz, function);
return (T) function.apply(size);
}
}
throw new YAMLException("Can't create collection: " + clazz);
}
示例11: flatten
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* Flatten, in parallel, a {@link Node}. A flattened node is one that has
* no children. If the node is already flat, it is simply returned.
*
* @implSpec
* If a new node is to be created, the generator is used to create an array
* whose length is {@link Node#count()}. Then the node tree is traversed
* and leaf node elements are placed in the array concurrently by leaf tasks
* at the correct offsets.
*
* @param <T> type of elements contained by the node
* @param node the node to flatten
* @param generator the array factory used to create array instances
* @return a flat {@code Node}
*/
public static <T> Node<T> flatten(Node<T> node, IntFunction<T[]> generator) {
if (node.getChildCount() > 0) {
long size = node.count();
if (size >= MAX_ARRAY_SIZE)
throw new IllegalArgumentException(BAD_SIZE);
T[] array = generator.apply((int) size);
new ToArrayTask.OfRef<>(node, array, 0).invoke();
return node(array);
} else {
return node;
}
}
示例12: setAll
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* Set all elements of the specified array, using the provided
* generator function to compute each element.
*
* <p>If the generator function throws an exception, it is relayed to
* the caller and the array is left in an indeterminate state.
*
* @apiNote
* Setting a subrange of an array, using a generator function to compute
* each element, can be written as follows:
* <pre>{@code
* IntStream.range(startInclusive, endExclusive)
* .forEach(i -> array[i] = generator.apply(i));
* }</pre>
*
* @param <T> type of elements of the array
* @param array array to be initialized
* @param generator a function accepting an index and producing the desired
* value for that position
* @throws NullPointerException if the generator is null
* @since 1.8
*/
public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
Objects.requireNonNull(generator);
for (int i = 0; i < array.length; i++)
array[i] = generator.apply(i);
}
示例13: setAll
import java.util.function.IntFunction; //导入方法依赖的package包/类
/**
* Set all elements of the specified array, using the provided
* generator function to compute each element.
*
* <p>If the generator function throws an exception, it is relayed to
* the caller and the array is left in an indeterminate state.
*
* @param <T> type of elements of the array
* @param array array to be initialized
* @param generator a function accepting an index and producing the desired
* value for that position
* @throws NullPointerException if the generator is null
* @since 1.8
*/
public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
Objects.requireNonNull(generator);
for (int i = 0; i < array.length; i++)
array[i] = generator.apply(i);
}