Search in sources :

Example 96 with Spliterator

use of java.util.Spliterator in project j2cl by google.

the class StreamImpl method flatMapToInt.

@Override
public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) {
    throwIfTerminated();
    final Spliterator<? extends IntStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
    AbstractIntSpliterator flatMapSpliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, 0) {

        IntStream nextStream;

        Spliterator.OfInt next;

        @Override
        public boolean tryAdvance(IntConsumer action) {
            // look for a new spliterator
            while (advanceToNextSpliterator()) {
                // if we have one, try to read and use it
                if (next.tryAdvance(action)) {
                    return true;
                } else {
                    nextStream.close();
                    nextStream = null;
                    // failed, null it out so we can find another
                    next = null;
                }
            }
            return false;
        }

        private boolean advanceToNextSpliterator() {
            while (next == null) {
                if (!spliteratorOfStreams.tryAdvance(n -> {
                    if (n != null) {
                        nextStream = n;
                        next = n.spliterator();
                    }
                })) {
                    return false;
                }
            }
            return true;
        }
    };
    return new IntStreamImpl(this, flatMapSpliterator);
}
Also used : AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) AbstractSpliterator(java.util.Spliterators.AbstractSpliterator) Spliterators(java.util.Spliterators) BiFunction(java.util.function.BiFunction) IntConsumer(java.util.function.IntConsumer) DoubleConsumer(java.util.function.DoubleConsumer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator) InternalPreconditions.checkNotNull(javaemul.internal.InternalPreconditions.checkNotNull) BiConsumer(java.util.function.BiConsumer) ToLongFunction(java.util.function.ToLongFunction) IntFunction(java.util.function.IntFunction) AbstractDoubleSpliterator(java.util.Spliterators.AbstractDoubleSpliterator) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) ToIntFunction(java.util.function.ToIntFunction) BinaryOperator(java.util.function.BinaryOperator) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) List(java.util.List) InternalPreconditions.checkState(javaemul.internal.InternalPreconditions.checkState) ToDoubleFunction(java.util.function.ToDoubleFunction) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) Spliterator(java.util.Spliterator) AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) IntConsumer(java.util.function.IntConsumer)

Example 97 with Spliterator

use of java.util.Spliterator in project cx-flow by checkmarx-ltd.

the class AzureDevopsClient method getProjectIssueIds.

private List<String> getProjectIssueIds() throws IOException {
    log.info("Getting project issue IDs.");
    ObjectNode requestBody = objectMapper.createObjectNode();
    // WIQL language is read-only, so potential parameter injection shouldn't do any harm.
    String wiqlQuery = String.format("Select System.Id From WorkItems Where System.TeamProject = '%s'", projectName);
    requestBody.put("query", wiqlQuery);
    HttpEntity<ObjectNode> request = getRequestEntity(requestBody);
    String url = getResourceUrl("wit/wiql", null);
    ResponseEntity<ObjectNode> response = restClient.exchange(url, HttpMethod.POST, request, ObjectNode.class);
    ObjectNode responseBody = extractBody(response);
    List<String> result = StreamSupport.stream(responseBody.get("workItems").spliterator(), false).map(issue -> issue.get(ID_KEY).asText()).collect(Collectors.toList());
    log.info("Issues found: {}", result.size());
    return result;
}
Also used : ADOProperties(com.checkmarx.flow.config.ADOProperties) Issue(com.checkmarx.flow.dto.Issue) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) org.springframework.http(org.springframework.http) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Duration(java.time.Duration) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) RestTemplate(org.springframework.web.client.RestTemplate) Nullable(javax.annotation.Nullable) Predicate(java.util.function.Predicate) TestComponent(org.springframework.boot.test.context.TestComponent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CreateWorkItemAttr(com.checkmarx.flow.dto.azure.CreateWorkItemAttr) Awaitility(org.awaitility.Awaitility) Spliterator(java.util.Spliterator) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode)

Example 98 with Spliterator

use of java.util.Spliterator in project baremaps by baremaps.

the class PartitionedSpliteratorTest method trySplitWithNonEquivalentElementInStreamTest.

@Test
void trySplitWithNonEquivalentElementInStreamTest() {
    PartitionedSpliterator<Integer> stream = new PartitionedSpliterator<>(IntStream.range(0, 600).spliterator(), 250);
    Spliterator<Stream<Integer>> a = stream.trySplit();
    Spliterator<Stream<Integer>> b = stream.trySplit();
    Spliterator<Stream<Integer>> c = stream.trySplit();
    Spliterator<Stream<Integer>> d = stream.trySplit();
    AtomicInteger i = new AtomicInteger();
    a.forEachRemaining(s -> s.forEach(item -> {
        // cast necessary otherwise call is ambiguous
        assertEquals(i.get(), (long) item);
        i.getAndIncrement();
    }));
    b.forEachRemaining(s -> s.forEach(item -> {
        assertEquals(i.get(), (long) item);
        i.getAndIncrement();
    }));
    c.forEachRemaining(s -> s.forEach(item -> {
        assertEquals(i.get(), (long) item);
        i.getAndIncrement();
    }));
    d.forEachRemaining(s -> s.forEach(item -> fail()));
    stream.forEachRemaining(item -> fail());
    assertEquals(600, i.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertTrue(org.junit.Assert.assertTrue) Assert.fail(org.junit.Assert.fail) Spliterator(java.util.Spliterator) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 99 with Spliterator

use of java.util.Spliterator in project geotoolkit by Geomatys.

the class DistanceSpliteratorTest method testSegments.

private void testSegments(final CoordinateReferenceSystem crs, final boolean isOrthodromic) throws IOException, FactoryException {
    final DistanceSpliterator.Builder builder = DistanceSpliterator.builder().setCrs(crs);
    final UnaryOperator<Coordinate> pointTransformer = getTransformer(crs);
    final List<Segment> segments = Segment.loadTestData();
    for (Segment s : segments) {
        builder.setPolyline(new CoordinateArraySequence(new Coordinate[] { pointTransformer.apply(s.start), pointTransformer.apply(s.end) }, 2));
        final Spliterator.OfDouble ds = isOrthodromic ? builder.buildOrthodromic() : builder.buildLoxodromic();
        Assert.assertEquals("Returned spliterator is not of expected type", DistanceSpliterator.class, ds.getClass());
        boolean segmentComputed = ds.tryAdvance((double val) -> EngineTest.assertDistance(s, isOrthodromic, val));
        Assert.assertTrue("Segment length has not been computed", segmentComputed);
        ds.tryAdvance((double val) -> org.junit.Assert.fail("A second computing has been done, despite the fact that we've got only one segment."));
    }
}
Also used : Coordinate(org.locationtech.jts.geom.Coordinate) CoordinateArraySequence(org.locationtech.jts.geom.impl.CoordinateArraySequence) Spliterator(java.util.Spliterator)

Example 100 with Spliterator

use of java.util.Spliterator in project structure-project by wudskq.

the class IntPipeline method forEachWithCancel.

@Override
final void forEachWithCancel(Spliterator<Integer> spliterator, Sink<Integer> sink) {
    Spliterator.OfInt spl = adapt(spliterator);
    IntConsumer adaptedSink = adapt(sink);
    do {
    } while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink));
}
Also used : Spliterator(java.util.Spliterator) IntConsumer(java.util.function.IntConsumer) ObjIntConsumer(java.util.function.ObjIntConsumer)

Aggregations

Spliterator (java.util.Spliterator)109 List (java.util.List)43 ArrayList (java.util.ArrayList)35 HashSet (java.util.HashSet)31 IntConsumer (java.util.function.IntConsumer)31 Set (java.util.Set)26 Function (java.util.function.Function)24 Objects (java.util.Objects)23 Spliterators (java.util.Spliterators)23 Stream (java.util.stream.Stream)22 Collectors (java.util.stream.Collectors)21 Iterator (java.util.Iterator)20 Map (java.util.Map)20 Consumer (java.util.function.Consumer)20 Comparator (java.util.Comparator)18 LongConsumer (java.util.function.LongConsumer)18 StreamSupport (java.util.stream.StreamSupport)18 Arrays (java.util.Arrays)17 Supplier (java.util.function.Supplier)17 Collections (java.util.Collections)16