java.util.function.IntFunction Java Examples

The following examples show how to use java.util.function.IntFunction. 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: TestOzoneFSInputStream.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Test
public void readToByteBuffer() throws IOException {
  for (IntFunction<ByteBuffer> constructor : BUFFER_CONSTRUCTORS) {
    for (int streamLength = 1; streamLength <= 10; streamLength++) {
      for (int bufferCapacity = 0; bufferCapacity <= 10; bufferCapacity++) {
        testReadToByteBuffer(constructor, streamLength, bufferCapacity, 0);
        if (bufferCapacity > 1) {
          testReadToByteBuffer(constructor, streamLength, bufferCapacity, 1);
          if (bufferCapacity > 2) {
            testReadToByteBuffer(constructor, streamLength, bufferCapacity,
                bufferCapacity - 1);
          }
        }
        testReadToByteBuffer(constructor, streamLength, bufferCapacity,
            bufferCapacity);
      }
    }
  }
}
 
Example #2
Source File: SortedOps.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@Override
public <P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper,
                                         Spliterator<P_IN> spliterator,
                                         IntFunction<T[]> generator) {
    // If the input is already naturally sorted and this operation
    // naturally sorts then collect the output
    if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags()) && isNaturalSort) {
        return helper.evaluate(spliterator, false, generator);
    }
    else {
        // @@@ Weak two-pass parallel implementation; parallel collect, parallel sort
        T[] flattenedData = helper.evaluate(spliterator, true, generator).asArray(generator);
        Arrays.parallelSort(flattenedData, comparator);
        return Nodes.node(flattenedData);
    }
}
 
Example #3
Source File: IntPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@Override
public final <U> Stream<U> mapToObj(IntFunction<? extends U> mapper) {
    Objects.requireNonNull(mapper);
    return new ReferencePipeline.StatelessOp<Integer, U>(this, StreamShape.INT_VALUE,
                                                         StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
        @Override
        Sink<Integer> opWrapSink(int flags, Sink<U> sink) {
            return new Sink.ChainedInt<U>(sink) {
                @Override
                public void accept(int t) {
                    downstream.accept(mapper.apply(t));
                }
            };
        }
    };
}
 
Example #4
Source File: AbstractPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Collect the elements output from the pipeline stage.
 *
 * @param generator the array generator to be used to create array instances
 * @return a flat array-backed Node that holds the collected output elements
 */
@SuppressWarnings("unchecked")
final Node<E_OUT> evaluateToArrayNode(IntFunction<E_OUT[]> generator) {
    if (linkedOrConsumed)
        throw new IllegalStateException(MSG_STREAM_LINKED);
    linkedOrConsumed = true;

    // If the last intermediate operation is stateful then
    // evaluate directly to avoid an extra collection step
    if (isParallel() && previousStage != null && opIsStateful()) {
        // Set the depth of this, last, pipeline stage to zero to slice the
        // pipeline such that this operation will not be included in the
        // upstream slice and upstream operations will not be included
        // in this slice
        depth = 0;
        return opEvaluateParallel(previousStage, previousStage.sourceSpliterator(0), generator);
    }
    else {
        return evaluate(sourceSpliterator(0), true, generator);
    }
}
 
Example #5
Source File: SortedOps.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <P_IN> Node<Long> opEvaluateParallel(PipelineHelper<Long> helper,
                                            Spliterator<P_IN> spliterator,
                                            IntFunction<Long[]> generator) {
    if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
        return helper.evaluate(spliterator, false, generator);
    }
    else {
        Node.OfLong n = (Node.OfLong) helper.evaluate(spliterator, true, generator);

        long[] content = n.asPrimitiveArray();
        Arrays.parallelSort(content);

        return Nodes.node(content);
    }
}
 
Example #6
Source File: SortedOps.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
@Override
public <P_IN> Node<Integer> opEvaluateParallel(PipelineHelper<Integer> helper,
                                               Spliterator<P_IN> spliterator,
                                               IntFunction<Integer[]> generator) {
    if (StreamOpFlag.SORTED.isKnown(helper.getStreamAndOpFlags())) {
        return helper.evaluate(spliterator, false, generator);
    }
    else {
        Node.OfInt n = (Node.OfInt) helper.evaluate(spliterator, true, generator);

        int[] content = n.asPrimitiveArray();
        Arrays.parallelSort(content);

        return Nodes.node(content);
    }
}
 
Example #7
Source File: Mappings.java    From Quicksql with MIT License 6 votes vote down vote up
/**
 * Returns a mapping that shifts a given mapping's source by a given
 * offset.
 *
 * <p>For example, given {@code mapping} with sourceCount=2, targetCount=8,
 * and (source, target) entries {[0: 5], [1: 7]}, offsetSource(mapping, 3)
 * returns a mapping with sourceCount=5, targetCount=8,
 * and (source, target) entries {[3: 5], [4: 7]}.
 *
 * @param mapping     Input mapping
 * @param offset      Offset to be applied to each source
 * @param sourceCount New source count; must be at least {@code mapping}'s
 *                    source count plus {@code offset}
 * @return Shifted mapping
 */
public static TargetMapping offsetSource(
    final TargetMapping mapping, final int offset, final int sourceCount) {
  if (sourceCount < mapping.getSourceCount() + offset) {
    throw new IllegalArgumentException("new source count too low");
  }
  return target(
      (IntFunction<Integer>) source -> {
        int source2 = source - offset;
        return source2 < 0 || source2 >= mapping.getSourceCount()
            ? null
            : mapping.getTargetOpt(source2);
      },
      sourceCount,
      mapping.getTargetCount());
}
 
Example #8
Source File: DoublePipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
final <P_IN> Node<Double> evaluateToNode(PipelineHelper<Double> helper,
                                         Spliterator<P_IN> spliterator,
                                         boolean flattenTree,
                                         IntFunction<Double[]> generator) {
    return Nodes.collectDouble(helper, spliterator, flattenTree);
}
 
Example #9
Source File: LongPipeline.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
final <P_IN> Node<Long> evaluateToNode(PipelineHelper<Long> helper,
                                       Spliterator<P_IN> spliterator,
                                       boolean flattenTree,
                                       IntFunction<Long[]> generator) {
    return Nodes.collectLong(helper, spliterator, flattenTree);
}
 
Example #10
Source File: IntPipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
final <P_IN> Node<Integer> evaluateToNode(PipelineHelper<Integer> helper,
                                          Spliterator<P_IN> spliterator,
                                          boolean flattenTree,
                                          IntFunction<Integer[]> generator) {
    return Nodes.collectInt(helper, spliterator, flattenTree);
}
 
Example #11
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 #12
Source File: SetAllTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "string")
public void testSetAllString(String name, int size, IntFunction<String> generator, String[] expected) {
    String[] result = new String[size];
    Arrays.setAll(result, generator);
    assertEquals(result, expected, "setAll(String[], IntFunction<String>) case " + name + " failed.");

    // ensure fresh array
    result = new String[size];
    Arrays.parallelSetAll(result, generator);
    assertEquals(result, expected, "parallelSetAll(String[], IntFunction<String>) case " + name + " failed.");
}
 
Example #13
Source File: SetAllTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "string")
public void testSetAllString(String name, int size, IntFunction<String> generator, String[] expected) {
    String[] result = new String[size];
    Arrays.setAll(result, generator);
    assertEquals(result, expected, "setAll(String[], IntFunction<String>) case " + name + " failed.");

    // ensure fresh array
    result = new String[size];
    Arrays.parallelSetAll(result, generator);
    assertEquals(result, expected, "parallelSetAll(String[], IntFunction<String>) case " + name + " failed.");
}
 
Example #14
Source File: PartitionedConsumption.java    From presto with Apache License 2.0 5 votes vote down vote up
private PartitionedConsumption(
        int consumersCount,
        ListenableFuture<?> activator,
        Iterable<Integer> partitionNumbers,
        IntFunction<ListenableFuture<T>> loader,
        IntConsumer disposer)
{
    checkArgument(consumersCount > 0, "consumersCount must be positive");
    this.consumersCount = consumersCount;
    this.partitions = createPartitions(activator, partitionNumbers, loader, disposer);
}
 
Example #15
Source File: SliceOps.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
SliceTask(AbstractPipeline<P_OUT, P_OUT, ?> op,
          PipelineHelper<P_OUT> helper,
          Spliterator<P_IN> spliterator,
          IntFunction<P_OUT[]> generator,
          long offset, long size) {
    super(helper, spliterator);
    this.op = op;
    this.generator = generator;
    this.targetOffset = offset;
    this.targetSize = size;
}
 
Example #16
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 #17
Source File: Nodes.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@Override
public Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
    if (from == 0 && to == count())
        return this;
    long leftCount = left.count();
    if (from >= leftCount)
        return right.truncate(from - leftCount, to - leftCount, generator);
    else if (to <= leftCount)
        return left.truncate(from, to, generator);
    else {
        return Nodes.conc(getShape(), left.truncate(from, leftCount, generator),
                          right.truncate(0, to - leftCount, generator));
    }
}
 
Example #18
Source File: CompositeSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFieldSerializer() {
	TEST_FIELD_SERIALIZERS.forEach(t -> {
		@SuppressWarnings("unchecked")
		TypeSerializer<Object>[] fieldSerializers = new TypeSerializer[] { t.f0 };
		List<Object>[] instances = Arrays.stream(t.f1)
			.map(Arrays::asList)
			.toArray((IntFunction<List<Object>[]>) List[]::new);
		runTests(t.f0.getLength(), fieldSerializers, instances);
	});
}
 
Example #19
Source File: NodeEditionCodeArea.java    From pmd-designer with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@NonNull
public Label buildExpectedLabel(IntFunction<Val<Integer>> numViolationsPerLine, int idx) {
    Label foo = new Label();
    foo.getStyleClass().addAll("num-violations-gutter-label");
    Val<Integer> num = numViolationsPerLine.apply(idx + 1);
    foo.textProperty().bind(num.map(Object::toString));
    foo.setTooltip(new Tooltip("Number of violations expected on this line"));
    foo.visibleProperty().bind(num.map(it -> it > 0));
    return foo;
}
 
Example #20
Source File: SpinedBuffer.java    From jdk1.8-source-analysis with Apache License 2.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 #21
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 #22
Source File: Node.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@Override
default Node.OfInt truncate(long from, long to, IntFunction<Integer[]> generator) {
    if (from == 0 && to == count())
        return this;
    long size = to - from;
    Spliterator.OfInt spliterator = spliterator();
    Node.Builder.OfInt nodeBuilder = Nodes.intBuilder(size);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance((IntConsumer) e -> { }); i++) { }
    for (int i = 0; (i < size) && spliterator.tryAdvance((IntConsumer) nodeBuilder); i++) { }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
Example #23
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 #24
Source File: ReferencePipeline.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@Override
final <P_IN> Node<P_OUT> evaluateToNode(PipelineHelper<P_OUT> helper,
                                    Spliterator<P_IN> spliterator,
                                    boolean flattenTree,
                                    IntFunction<P_OUT[]> generator) {
    return Nodes.collect(helper, spliterator, flattenTree, generator);
}
 
Example #25
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) {
    if (array.length == curSize) {
        return array;
    } else {
        throw new IllegalStateException();
    }
}
 
Example #26
Source File: Nodes.java    From jdk1.8-source-analysis with Apache License 2.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 #27
Source File: ReferencePipeline.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final <A> A[] toArray(IntFunction<A[]> generator) {
    // Since A has no relation to U (not possible to declare that A is an upper bound of U)
    // there will be no static type checking.
    // Therefore use a raw type and assume A == U rather than propagating the separation of A and U
    // throughout the code-base.
    // The runtime type of U is never checked for equality with the component type of the runtime type of A[].
    // Runtime checking will be performed when an element is stored in A[], thus if A is not a
    // super type of U an ArrayStoreException will be thrown.
    @SuppressWarnings("rawtypes")
    IntFunction rawGenerator = (IntFunction) generator;
    return (A[]) Nodes.flatten(evaluateToArrayNode(rawGenerator), rawGenerator)
                          .asArray(rawGenerator);
}
 
Example #28
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 #29
Source File: Node.java    From TencentKona-8 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 #30
Source File: SliceOps.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
SliceTask(AbstractPipeline<P_OUT, P_OUT, ?> op,
          PipelineHelper<P_OUT> helper,
          Spliterator<P_IN> spliterator,
          IntFunction<P_OUT[]> generator,
          long offset, long size) {
    super(helper, spliterator);
    this.op = op;
    this.generator = generator;
    this.targetOffset = offset;
    this.targetSize = size;
}