下面列出了java.util.function.IntFunction#apply ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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));
}
}
};
}
};
}
public IntFunction<javafx.scene.Node> defaultLineNumberFactory() {
IntFunction<javafx.scene.Node> base = LineNumberFactory.get(this);
Val<Integer> activePar = Val.wrap(currentParagraphProperty());
return idx -> {
javafx.scene.Node label = base.apply(idx);
activePar.conditionOnShowing(label)
.values()
.subscribe(p -> label.pseudoClassStateChanged(PseudoClass.getPseudoClass("has-caret"), idx == p));
// adds a pseudo class if part of the focus node appears on this line
currentFocusNode.conditionOnShowing(label)
.values()
.subscribe(n -> label.pseudoClassStateChanged(PseudoClass.getPseudoClass("is-focus-node"),
n != null && idx + 1 <= n.getEndLine() && idx + 1 >= n.getBeginLine()));
return label;
};
}
@Test
public void readEmptyStreamToByteBuffer() throws IOException {
for (IntFunction<ByteBuffer> constructor : BUFFER_CONSTRUCTORS) {
final OzoneFSInputStream subject = createTestSubject(emptyStream());
final ByteBuffer buf = constructor.apply(1);
final int bytesRead = subject.read(buf);
assertEquals(-1, bytesRead);
assertEquals(0, buf.position());
}
}
@SuppressWarnings("unchecked")
ArrayNode(long size, IntFunction<T[]> generator) {
if (size >= MAX_ARRAY_SIZE)
throw new IllegalArgumentException(BAD_SIZE);
this.array = generator.apply((int) size);
this.curSize = 0;
}
/**
* {@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;
}
/**
* 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;
}
@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;
}
@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;
}
@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;
}
public static <T extends Enum<T>> T constructAndAdd(Class<T> clazz, IntFunction<? extends T> constructor) {
T[] values = clazz.getEnumConstants();
T instance = constructor.apply(values.length);
addToValues(values, instance);
clearCachedValues(clazz);
return instance;
}
/**
* 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;
}
public void testIntCtorRef() {
IntFunction<int[]> factory = int[]::new;
int[] arr = factory.apply(6);
assertTrue(arr.length == 6);
}
@Override
public T[] asArray(IntFunction<T[]> generator) {
return generator.apply(0);
}
@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) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
IntConsumer downstreamAsInt = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(int t) {
try (IntStream result = mapper.apply(t)) {
if (result != null) {
if (!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsInt);
}
else {
Spliterator.OfInt s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsInt));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}
@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) {
// true if cancellationRequested() has been called
boolean cancellationRequestedCalled;
// cache the consumer to avoid creation on every accepted element
IntConsumer downstreamAsInt = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(int t) {
try (IntStream result = mapper.apply(t)) {
if (result != null) {
if (!cancellationRequestedCalled) {
result.sequential().forEach(downstreamAsInt);
}
else {
Spliterator.OfInt s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsInt));
}
}
}
}
@Override
public boolean cancellationRequested() {
// If this method is called then an operation within the stream
// pipeline is short-circuiting (see AbstractPipeline.copyInto).
// Note that we cannot differentiate between an upstream or
// downstream operation
cancellationRequestedCalled = true;
return downstream.cancellationRequested();
}
};
}
};
}
/**
* 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;
}
}
/**
* 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;
}
}
public IntFunction<javafx.scene.Node> testCaseLineNumberFactory(LiveTestCase liveTestCase) {
IntFunction<javafx.scene.Node> base = defaultLineNumberFactory();
Val<Map<Integer, LiveList<LiveViolationRecord>>> mapVal = ReactfxUtil.groupBy(liveTestCase.getExpectedViolations(), (LiveViolationRecord v) -> v.getRange().startPos.line);
Subscription pin = mapVal.pin();
liveTestCase.addCommitHandler(t -> pin.unsubscribe());
Val<IntFunction<Val<Integer>>> map1 = mapVal.map(it -> (int j) -> Optional.ofNullable(it.get(j)).orElse(new LiveArrayList<>()).sizeProperty());
IntFunction<Val<Integer>> numViolationsPerLine = i -> map1.flatMap(it -> it.apply(i));
return idx -> {
javafx.scene.Node label = base.apply(idx);
HBox hBox = new HBox();
hBox.setSpacing(3);
Label foo = buildExpectedLabel(numViolationsPerLine, idx);
hBox.getChildren().addAll(foo, label);
return hBox;
};
}
/**
* 对数组进行map操作
*
* @param tArray -源数组
* @param mapper -map函数
* @param generator -结果构造器
* @param <T> -源类型
* @param <R> -结果类型
* @return -结果集数组
*/
public static <T, R> R[] map(T[] tArray, Function<? super T, ? extends R> mapper, IntFunction<R[]> generator) {
return tArray == null ? generator.apply(0) : Arrays.stream(tArray).map(mapper).toArray(generator);
}
/**
* 对数组进行map操作
*
* @param tArray -源数组
* @param mapper -map函数
* @param generator -结果构造器
* @param <T> -源类型
* @param <R> -结果类型
* @return -结果集数组
*/
public static <T, R> R[] map(T[] tArray, Function<? super T, ? extends R> mapper, IntFunction<R[]> generator) {
return tArray == null ? generator.apply(0) : Arrays.stream(tArray).map(mapper).toArray(generator);
}