Java Code Examples for java.util.function.IntFunction#apply()

The following examples show how to use java.util.function.IntFunction#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeEditionCodeArea.java    From pmd-designer with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
    };
}
 
Example 2
Source File: IntPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@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));
                    }
                }
            };
        }
    };
}
 
Example 3
Source File: TestOzoneFSInputStream.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@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());
  }
}
 
Example 4
Source File: SpinedBuffer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: EnumHacks.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
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;
}
 
Example 6
Source File: Nodes.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: Nodes.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: Nodes.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 9
Source File: SpinedBuffer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: Node.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@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;
}
 
Example 11
Source File: Nodes.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 12
Source File: IntPipeline.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@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();
                }
            };
        }
    };
}
 
Example 13
Source File: Nodes.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public T[] asArray(IntFunction<T[]> generator) {
    return generator.apply(0);
}
 
Example 14
Source File: IntPipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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();
                }
            };
        }
    };
}
 
Example 15
Source File: ArrayCtorRefTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void testIntCtorRef() {
    IntFunction<int[]> factory = int[]::new;
    int[] arr = factory.apply(6);
    assertTrue(arr.length == 6);
}
 
Example 16
Source File: Nodes.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
    }
}
 
Example 17
Source File: NodeEditionCodeArea.java    From pmd-designer with BSD 2-Clause "Simplified" License 3 votes vote down vote up
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;
    };
}
 
Example 18
Source File: Nodes.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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;
    }
}
 
Example 19
Source File: CollectionUtils.java    From beihu-boot with Apache License 2.0 2 votes vote down vote up
/**
 * 对数组进行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);
}
 
Example 20
Source File: CollectionExtendUtil.java    From beihu-boot with Apache License 2.0 2 votes vote down vote up
/**
 * 对数组进行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);
}