Example usage for java.util.stream StreamSupport stream

List of usage examples for java.util.stream StreamSupport stream

Introduction

In this page you can find the example usage for java.util.stream StreamSupport stream.

Prototype

public static <T> Stream<T> stream(Spliterator<T> spliterator, boolean parallel) 

Source Link

Document

Creates a new sequential or parallel Stream from a Spliterator .

Usage

From source file:com.hotelbeds.distribution.hotel_api_sdk.HotelApiClient.java

private <T> Stream<T> getStreamOf(final String language, final boolean useSecondaryLanguage, ContentType type)
        throws HotelApiSDKException {
    try {/*from   ww w . j a  va  2  s.  co m*/
        return StreamSupport.stream(new ContentElementSpliterator<T>(this, type,
                generateDefaultFullRequest(language, useSecondaryLanguage, type)), false);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new HotelApiSDKException(
                new HotelbedsError("SDK Configuration error", e.getCause().getMessage()));
    }
}

From source file:org.hawkular.inventory.impl.tinkerpop.test.BasicTest.java

@Test
public void testResources() throws Exception {
    TetraFunction<String, String, String, String, Void> test = (tenantId, environmentId, resourceTypeId,
            id) -> {/*from   w  w  w .  ja  v a 2  s . c o m*/
        GremlinPipeline<Graph, Vertex> q = new GremlinPipeline<Graph, Vertex>(graph).V().has("__type", "tenant")
                .has("__eid", tenantId).out("contains").has("__type", "environment").has("__eid", environmentId)
                .out("contains").has("__type", "resource").has("__eid", id).as("resource").in("defines")
                .has("__type", "resourceType").has("__eid", resourceTypeId).back("resource").cast(Vertex.class);

        assert q.hasNext();

        Resource r = inventory.tenants().get(tenantId).environments().get(environmentId).feedlessResources()
                .getAll(Defined.by(new ResourceType(tenantId, resourceTypeId, "1.0")), With.id(id)).entities()
                .iterator().next();
        assert r.getId().equals(id);

        return null;
    };

    test.apply("com.acme.tenant", "production", "URL", "host1");
    test.apply("com.example.tenant", "test", "Playroom", "playroom1");
    test.apply("com.example.tenant", "test", "Playroom", "playroom2");

    GraphQuery query = graph.query().has("__type", "resource");
    Assert.assertEquals(6, StreamSupport.stream(query.vertices().spliterator(), false).count());
}

From source file:org.eclipse.hawkbit.repository.jpa.JpaRolloutManagement.java

private void deleteScheduledActions(final JpaRollout rollout, final Slice<JpaAction> scheduledActions) {
    final boolean hasScheduledActions = scheduledActions.getNumberOfElements() > 0;

    if (hasScheduledActions) {
        try {/*  w  w w  .j  a  v a2 s  .co m*/
            final Iterable<JpaAction> iterable = scheduledActions::iterator;
            final List<Long> actionIds = StreamSupport.stream(iterable.spliterator(), false).map(Action::getId)
                    .collect(Collectors.toList());
            actionRepository.deleteByIdIn(actionIds);
            afterCommit.afterCommit(() -> eventPublisher.publishEvent(
                    new RolloutUpdatedEvent(rollout, EventPublisherHolder.getInstance().getApplicationId())));
        } catch (final RuntimeException e) {
            LOGGER.error("Exception during deletion of actions of rollout {}", rollout, e);
        }
    }
}

From source file:com.joyent.manta.client.MantaClient.java

/**
 * Return a stream of the contents of a directory in Manta.
 *
 * @param path The fully qualified path of the directory.
 * @return A {@link Stream} of {@link MantaObjectResponse} listing the contents of the directory.
 * @throws IOException thrown when there is a problem getting the listing over the network
 *//*from   www  .j  a va 2  s .  co  m*/
public Stream<MantaObject> listObjects(final String path) throws IOException {
    final MantaDirectoryListingIterator itr = streamingIterator(path);

    /* We preemptively check the iterator for a next value because that will
     * trigger an error if the path doesn't exist or is otherwise inaccessible.
     * This error typically takes the form of an UncheckedIOException, so we
     * unwind that exception if the cause is a MantaClientHttpResponseException
     * and rethrow another MantaClientHttpResponseException, so that the
     * stacktrace will point to this running method.
     */
    try {
        if (!itr.hasNext()) {
            itr.close();
            return Stream.empty();
        }
    } catch (UncheckedIOException e) {
        if (e.getCause() instanceof MantaClientHttpResponseException) {
            throw e.getCause();
        } else {
            throw e;
        }
    }

    final int additionalCharacteristics = Spliterator.CONCURRENT | Spliterator.ORDERED | Spliterator.NONNULL
            | Spliterator.DISTINCT;

    Stream<Map<String, Object>> backingStream = StreamSupport
            .stream(Spliterators.spliteratorUnknownSize(itr, additionalCharacteristics), false);

    Stream<MantaObject> stream = backingStream.map(MantaObjectConversionFunction.INSTANCE).onClose(itr::close);

    danglingStreams.add(stream);

    return stream;
}

From source file:com.ikanow.aleph2.shared.crud.mongodb.services.TestMongoDbCrudService.java

public void testJsonRepositoryCalls_common(final ICrudService<JsonNode> service,
        MongoDbCrudService<JsonNode, String> original) throws InterruptedException, ExecutionException {

    // Single object get

    final Future<Optional<JsonNode>> obj1 = service.getObjectById("id1");

    assertEquals("{ \"_id\" : \"id1\" , \"test_string\" : \"test_string1\" , \"test_long\" : 1}",
            original.convertToBson(obj1.get().get()).toString());

    // Multi object get

    final QueryComponent<JsonNode> query_2 = CrudUtils.allOf().rangeAbove("_id", "id4", false)
            .withPresent("test_long").orderBy(Tuples._2T("test_long", 1)).limit(4);

    try (Cursor<JsonNode> cursor = service.getObjectsBySpec(query_2, Arrays.asList("test_string"), false)
            .get()) {//  w ww . j  a  v a2 s . co  m

        assertEquals(6, cursor.count()); // (count ignores limit)

        final List<JsonNode> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(4, objs.size());

        final DBObject first_obj = original.convertToBson(objs.get(0));

        assertEquals("{ \"_id\" : \"id4\" , \"test_long\" : 4}", first_obj.toString());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }

    // Delete

    assertEquals(10L, (long) service.countObjects().get());

    final QueryComponent<JsonNode> query_5b = CrudUtils.allOf().rangeAbove("_id", "id4", false)
            .withPresent("test_long").orderBy(Tuples._2T("test_long", 1)).limit(4);

    assertEquals(4L, (long) service.deleteObjectsBySpec(query_5b).get());

    assertEquals(6L, (long) original._state.coll.count());

    //TODO: also need to do an update and a findAndModify
}

From source file:com.ikanow.aleph2.management_db.services.DataBucketCrudService.java

/** A utility routine to check whether the bucket is allowed to be in this location
 * @param bucket - the bucket to validate
 * @return a future containing validation errors based on the path
 *///from  ww w . j a v  a 2  s .  c o m
private CompletableFuture<Collection<BasicMessageBean>> validateOtherBucketsInPathChain(
        final DataBucketBean bucket) {
    final MethodNamingHelper<DataBucketBean> helper = BeanTemplateUtils.from(DataBucketBean.class);
    final String bucket_full_name = normalizeBucketPath(bucket.full_name(), true);

    return this._underlying_data_bucket_db.get().getObjectsBySpec(getPathQuery(bucket_full_name),
            Arrays.asList(helper.field(DataBucketBean::full_name)), true).thenApply(cursor -> {
                return StreamSupport.stream(cursor.spliterator(), false).filter(b -> (null == b.full_name()))
                        .<BasicMessageBean>map(b -> {
                            final String norm_name = normalizeBucketPath(b.full_name(), true);
                            if (norm_name.startsWith(bucket_full_name)) {
                                //TODO (ALEPH-19) call out other function, create BasicMessageBean error if not
                                return null;
                            } else if (norm_name.startsWith(bucket_full_name)) {
                                //TODO (ALEPH-19) call out other function, create BasicMessageBean error if not
                                return null;
                            } else { // we're good
                                return null;
                            }
                        }).filter(err -> err != null).collect(Collectors.toList());
            });
}

From source file:org.xwiki.repository.internal.RepositoryManager.java

private boolean isGivenVersionOneOfExtensionVersions(ExtensionRepository repository, String extensionId,
        String extensionVersion) throws ResolveException {
    IterableResult<Version> versions = repository.resolveVersions(extensionId, 0, -1);
    return StreamSupport.stream(versions.spliterator(), false)
            .anyMatch(version -> version.getValue().equals(extensionVersion));
}

From source file:com.ikanow.aleph2.shared.crud.elasticsearch.services.TestElasticsearchCrudService.java

@Test
public void multiObjectRetrieve() throws InterruptedException, ExecutionException {

    final ElasticsearchCrudService<TestBean> service = getTestService("multiObjectRetrieve", TestBean.class);

    final List<TestBean> l = IntStream.rangeClosed(0, 9).boxed()
            .map(i -> BeanTemplateUtils.build(TestBean.class).with("_id", "id" + i)
                    .with("test_string", "test_string" + i).with("test_long", (Long) (long) i).done().get())
            .collect(Collectors.toList());

    service.storeObjects(l).get();/*from  ww w.  ja v  a 2  s .  c o  m*/

    assertEquals(10, service.countObjects().get().intValue());

    service.optimizeQuery(Arrays.asList("test_string")).get(); // (The get() waits for completion)

    // 1) Simple retrieve, no fields specified - sort

    final QueryComponent<TestBean> query = CrudUtils.allOf(TestBean.class)
            .rangeAbove("test_string", "test_string4", true).withPresent("test_long")
            .orderBy(Tuples._2T("test_string", -1));

    try (Cursor<TestBean> cursor = service.getObjectsBySpec(query).get()) {

        assertEquals(5, cursor.count());

        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(5, objs.size());

        final JsonNode first_obj = BeanTemplateUtils.toJson(objs.get(0));

        assertEquals("{\"_id\":\"id9\",\"test_string\":\"test_string9\",\"test_long\":9}",
                first_obj.toString());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }

    // 2) Simple retrieve, field specified (exclusive) - sort and limit

    final QueryComponent<TestBean> query_2 = CrudUtils.allOf(TestBean.class)
            .rangeAbove("test_string", "test_string4", false).withPresent("test_long")
            .orderBy(Tuples._2T("test_long", 1)).limit(4);

    try (Cursor<TestBean> cursor = service.getObjectsBySpec(query_2, Arrays.asList("test_string"), false)
            .get()) {

        assertEquals(6, cursor.count()); // (count ignores limit)

        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(4, objs.size());

        final JsonNode first_obj = BeanTemplateUtils.toJson(objs.get(0));

        assertEquals("{\"_id\":\"id4\",\"test_long\":4}", first_obj.toString());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }

    // 3) Simple retrieve, no docs returned

    final QueryComponent<TestBean> query_3 = CrudUtils.allOf(TestBean.class)
            .rangeAbove("test_string", "test_string9", true).withPresent("test_long").limit(4);

    try (Cursor<TestBean> cursor = service.getObjectsBySpec(query_3, Arrays.asList("test_string"), false)
            .get()) {
        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(0, objs.size());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }

    // 4) Test on an index that doens't exists, check the same

    final ElasticsearchCrudService<TestBean> service2 = getTestService("multiObjectRetrieve_missing",
            TestBean.class, false, Optional.empty());

    final QueryComponent<TestBean> query_4 = CrudUtils.allOf(TestBean.class)
            .rangeAbove("test_string", "test_string9", true).withPresent("test_long").limit(4);

    try (Cursor<TestBean> cursor = service2.getObjectsBySpec(query_4, Arrays.asList("test_string"), false)
            .get()) {
        final List<TestBean> objs = StreamSupport.stream(Optionals.ofNullable(cursor).spliterator(), false)
                .collect(Collectors.toList());

        assertEquals(0, objs.size());
    } catch (Exception e) {
        //(fail on close, normally carry on - but here error out)
        fail("getObjectsBySpec errored on close");
    }
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.services.TestElasticsearchIndexService.java

public void test_endToEnd_autoTime(boolean test_not_create_mode, Optional<String> primary_name)
        throws IOException, InterruptedException, ExecutionException {
    final Calendar time_setter = GregorianCalendar.getInstance();
    time_setter.set(2015, 1, 1, 13, 0, 0);
    final String bucket_str = Resources.toString(
            Resources.getResource(
                    "com/ikanow/aleph2/search_service/elasticsearch/services/test_end_2_end_bucket.json"),
            Charsets.UTF_8);/*from   w w w  .j a v a  2s  . c o  m*/
    final DataBucketBean bucket = BeanTemplateUtils.build(bucket_str, DataBucketBean.class)
            .with("_id", "test_end_2_end")
            .with("full_name", "/test/end-end/auto-time" + primary_name.map(s -> "/" + s).orElse(""))
            .with("modified", time_setter.getTime()).done().get();

    final String template_name = ElasticsearchIndexUtils.getBaseIndexName(bucket, primary_name);

    // Check starting from clean

    {
        try {
            _crud_factory.getClient().admin().indices().prepareDeleteTemplate(template_name).execute()
                    .actionGet();
        } catch (Exception e) {
        } // (This is fine, just means it doesn't exist)      
        try {
            _crud_factory.getClient().admin().indices().prepareDelete(template_name + "*").execute()
                    .actionGet();
        } catch (Exception e) {
        } // (This is fine, just means it doesn't exist)      

        final GetIndexTemplatesRequest gt = new GetIndexTemplatesRequest().names(template_name);
        final GetIndexTemplatesResponse gtr = _crud_factory.getClient().admin().indices().getTemplates(gt)
                .actionGet();
        assertTrue("No templates to start with", gtr.getIndexTemplates().isEmpty());
    }

    // If the primary buffer is specified then create it and switch to it
    primary_name.ifPresent(primary -> {
        _index_service.getDataService()
                .flatMap(s -> s.getWritableDataService(JsonNode.class, bucket, Optional.empty(), primary_name))
                .flatMap(IDataWriteService::getCrudService).get();

        _index_service.getDataService().get().switchCrudServiceToPrimaryBuffer(bucket, primary_name,
                Optional.empty(), Optional.empty());
    });

    // (note pass Optional.empty() in regardless of primary, since it should return the non-default primary regardless)
    final ICrudService<JsonNode> index_service_crud = _index_service.getDataService()
            .flatMap(s -> s.getWritableDataService(JsonNode.class, bucket, Optional.empty(), Optional.empty()))
            .flatMap(IDataWriteService::getCrudService).get();

    // Check template added:

    {
        final GetIndexTemplatesRequest gt2 = new GetIndexTemplatesRequest().names(template_name);
        final GetIndexTemplatesResponse gtr2 = _crud_factory.getClient().admin().indices().getTemplates(gt2)
                .actionGet();
        assertTrue(
                "Cache should contain the template: " + _index_service._bucket_template_cache.asMap().keySet(),
                _index_service._bucket_template_cache.asMap()
                        .containsKey(bucket._id() + primary_name.map(s -> ":" + s).orElse("") + ":true"));
        assertEquals(1, gtr2.getIndexTemplates().size());
    }

    // Get batch sub-service

    @SuppressWarnings("unchecked")
    final Optional<ICrudService.IBatchSubservice<JsonNode>> batch_service = index_service_crud
            .getUnderlyingPlatformDriver(ICrudService.IBatchSubservice.class, Optional.empty())
            .map(t -> (IBatchSubservice<JsonNode>) t);

    {
        assertTrue("Batch service must exist", batch_service.isPresent());
    }

    // Get information about the crud service

    final ElasticsearchContext es_context = (ElasticsearchContext) index_service_crud
            .getUnderlyingPlatformDriver(ElasticsearchContext.class, Optional.empty()).get();

    {
        assertTrue("Read write index", es_context instanceof ElasticsearchContext.ReadWriteContext);
        assertTrue("Temporal index", es_context
                .indexContext() instanceof ElasticsearchContext.IndexContext.ReadWriteIndexContext.TimedRwIndexContext);
        assertTrue("Auto type", es_context
                .typeContext() instanceof ElasticsearchContext.TypeContext.ReadWriteTypeContext.AutoRwTypeContext);

        // Check the the context contains the invalid 

        final ElasticsearchContext.TypeContext.ReadWriteTypeContext.AutoRwTypeContext context = (ElasticsearchContext.TypeContext.ReadWriteTypeContext.AutoRwTypeContext) es_context
                .typeContext();

        assertEquals(Arrays.asList("@timestamp"),
                context.fixed_type_fields().stream().collect(Collectors.toList()));
    }

    // Write some docs out

    Arrays.asList(1, 2, 3, 4, 5).stream().map(i -> {
        time_setter.set(2015, i, 1, 13, 0, 0);
        return time_setter.getTime();
    }).map(d -> (ObjectNode) _mapper.createObjectNode().put("@timestamp", d.getTime())).forEach(o -> {
        ObjectNode o1 = o.deepCopy();
        o1.set("val1", _mapper.createObjectNode().put("val2", "test"));
        ObjectNode o2 = o.deepCopy();
        o2.put("val1", "test");
        batch_service.get().storeObject(o1, false);
        batch_service.get().storeObject(o2, false);
    });

    for (int i = 0; i < 30; ++i) {
        Thread.sleep(1000L);
        if (index_service_crud.countObjects().get() >= 10) {
            System.out.println("Test end 2 end: (Got all the records)");
            break;
        }
    }
    Thread.sleep(2100L); // sleep another 2s+e for the aliases)

    // Check an alias per time slice gets created also
    Arrays.asList("_2015.02.01", "_2015.03.01", "_2015.04.01", "_2015.05.01", "_2015.06.01").stream()
            .forEach(time_suffix -> {
                final List<String> aliases = getAliasedBuffers(bucket, Optional.of(time_suffix));
                assertEquals(Arrays.asList(template_name + time_suffix), aliases);
            });

    // Check the top level alias is created
    final List<String> aliases = this.getMainAliasedBuffers(bucket);
    assertEquals(Arrays.asList("_2015.02.01", "_2015.03.01", "_2015.04.01", "_2015.05.01", "_2015.06.01")
            .stream().map(x -> template_name + x).collect(Collectors.toList()), aliases);

    final GetMappingsResponse gmr = es_context.client().admin().indices()
            .prepareGetMappings(template_name + "*").execute().actionGet();

    // Should have 5 different indexes, each with 2 types + _default_

    assertEquals(5, gmr.getMappings().keys().size());
    final Set<String> expected_keys = Arrays.asList(1, 2, 3, 4, 5).stream()
            .map(i -> template_name + "_2015.0" + (i + 1) + ".01").collect(Collectors.toSet());
    final Set<String> expected_types = Arrays.asList("_default_", "type_1", "type_2").stream()
            .collect(Collectors.toSet());

    if (test_not_create_mode)
        StreamSupport.stream(gmr.getMappings().spliterator(), false).forEach(x -> {
            assertTrue(
                    "Is one of the expected keys: " + x.key + " vs  "
                            + expected_keys.stream().collect(Collectors.joining(":")),
                    expected_keys.contains(x.key));
            //DEBUG
            //System.out.println(" ? " + x.key);
            StreamSupport.stream(x.value.spliterator(), false).forEach(Lambdas.wrap_consumer_u(y -> {
                //DEBUG
                //System.out.println("?? " + y.key + " --- " + y.value.sourceAsMap().toString());
                // Size 3: _default_, type1 and type2
                assertTrue("Is expected type: " + y.key, expected_types.contains(y.key));
            }));
            // Size 3: _default_, type_1, type_2 
            assertEquals("Should have 3 indexes: " + x.value.toString(), 3, x.value.size());
        });

    //TEST DELETION:
    if (test_not_create_mode)
        test_handleDeleteOrPurge(bucket, primary_name, true);
}