use of java.util.stream.IntStream in project TeeTime by teetime-framework.
the class ThreeStagesGlobalTaskPoolIT method shouldExecutePipelineCorrectlyManyElements.
private void shouldExecutePipelineCorrectlyManyElements(final int numElements, final int numThreads, final int numExecutions) {
List<Integer> processedElements = new ArrayList<>();
IntStream inputElements = IntStream.iterate(0, i -> i + 1).limit(numElements);
Configuration config = new Configuration().from(new StreamProducer<>(inputElements)).to(new Counter<>()).end(new CollectorSink<>(processedElements));
TeeTimeScheduler scheduling = new GlobalTaskPoolScheduling(numThreads, config, numExecutions);
Execution<Configuration> execution = new Execution<>(config, true, scheduling);
execution.executeBlocking();
for (int i = 0; i < numElements; i++) {
Integer actualElement = processedElements.get(i);
assertThat(actualElement, is(i));
}
assertThat(processedElements, hasSize(numElements));
}
use of java.util.stream.IntStream in project chuidiang-ejemplos by chuidiang.
the class Java8RandomExample method main.
public static void main(String[] args) {
Random random = new Random();
// IntStream for values between 1 and 6 both included
final IntStream ints = random.ints(1, 7);
// Iterator to get integers
final PrimitiveIterator.OfInt iterator = ints.iterator();
// Loop to print 10 integers.
int counter = 0;
while (iterator.hasNext() && counter < 10) {
System.out.println(iterator.next());
counter++;
}
// Close de stream
ints.close();
}
use of java.util.stream.IntStream in project ddf by codice.
the class IngestCommand method processBatch.
private boolean processBatch(CatalogFacade catalog, ArrayList<Metacard> metacards) throws SourceUnavailableException {
CreateResponse createResponse = null;
try {
createResponse = createMetacards(catalog, metacards);
} catch (IngestException e) {
printErrorMessage("Error executing command: " + e.getMessage());
if (INGEST_LOGGER.isWarnEnabled()) {
INGEST_LOGGER.warn("Error ingesting metacard batch {}", buildIngestLog(metacards), e);
}
} catch (SourceUnavailableException e) {
if (INGEST_LOGGER.isWarnEnabled()) {
INGEST_LOGGER.warn("Error on process batch, local Provider not available. {}" + " metacards failed to ingest. {}", metacards.size(), buildIngestLog(metacards), e);
}
} finally {
IntStream range = IntStream.range(0, metacards.size());
range.forEach(i -> phaser.arriveAndDeregister());
range.close();
}
if (createResponse != null) {
ingestCount.getAndAdd(metacards.size());
}
return createResponse != null;
}
use of java.util.stream.IntStream in project jdk8u_jdk by JetBrains.
the class StreamSpliteratorTest method testIntSplitting.
//
public void testIntSplitting() {
List<Consumer<IntStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
}), s -> s.reduce(Integer::sum));
List<UnaryOperator<IntStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
s -> s.map(i -> i).parallel());
for (int i = 0; i < terminalOps.size(); i++) {
setContext("termOpIndex", i);
Consumer<IntStream> terminalOp = terminalOps.get(i);
for (int j = 0; j < intermediateOps.size(); j++) {
setContext("intOpIndex", j);
UnaryOperator<IntStream> intermediateOp = intermediateOps.get(j);
for (boolean proxyEstimateSize : new boolean[] { false, true }) {
setContext("proxyEstimateSize", proxyEstimateSize);
// Size is assumed to be larger than the target size for no splitting
// @@@ Need way to obtain the target size
Spliterator.OfInt sp = intermediateOp.apply(IntStream.range(0, 1000)).spliterator();
ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
IntStream s = StreamSupport.intStream(psp, true);
terminalOp.accept(s);
Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
}
}
}
}
use of java.util.stream.IntStream in project jdk8u_jdk by JetBrains.
the class SplittableRandomTest method intsDataProvider.
@DataProvider(name = "ints")
public static Object[][] intsDataProvider() {
List<Object[]> data = new ArrayList<>();
// Function to create a stream using a RandomBoxedSpliterator
Function<Function<SplittableRandom, Integer>, IntStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToInt(i -> i);
// Unbounded
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints().limit(%d)", SIZE), () -> new SplittableRandom().ints().limit(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d)", SIZE), () -> new SplittableRandom().ints(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt())", SIZE), () -> rbsf.apply(sr -> sr.nextInt())), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
for (int b : BOUNDS) {
for (int o : ORIGINS) {
final int origin = o;
final int bound = b;
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().ints(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().ints(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
if (origin == 0) {
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextInt(bound))), randomAsserter(SIZE, origin, bound) });
}
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextInt(origin, bound))), randomAsserter(SIZE, origin, bound) });
}
}
return data.toArray(new Object[0][]);
}
Aggregations