Example usage for java.util Collection stream

List of usage examples for java.util Collection stream

Introduction

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

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:com.ikanow.aleph2.graph.titan.utils.TitanGraphBuildingUtils.java

/** Creates a JSON object out of the designated vertex properties
 * @param el/* ww w.  j  av  a2 s .  c  o m*/
 * @param fields
 * @return
 */
protected static JsonNode getElementProperties(final Element el, final Collection<String> fields) {
    return fields.stream().map(f -> Tuples._2T(f, el.property(f)))
            .filter(t2 -> (null != t2._2()) && t2._2().isPresent()).reduce(_mapper.createObjectNode(),
                    (o, t2) -> insertIntoObjectNode(t2._1(), t2._2(), o), (o1, o2) -> o2);
}

From source file:com.ikanow.aleph2.management_db.mongodb.services.IkanowV1SyncService_LibraryJars.java

/** Takes a collection of results from the management side-channel, and uses it to update a harvest node
 * @param key - source key / bucket id// w w  w .  j  a  va2s.co  m
 * @param status_messages
 * @param source_db
 * @return true - if share updated with errors, false otherwise
 */
protected static CompletableFuture<Boolean> updateV1ShareErrorStatus(final Date main_date, final String id,
        final Collection<BasicMessageBean> status_messages,
        final IManagementCrudService<SharedLibraryBean> library_mgmt, final ICrudService<JsonNode> share_db,
        final boolean create_not_update) {
    final String message_block = status_messages.stream().map(msg -> {
        return "[" + msg.date() + "] " + msg.source() + " (" + msg.command() + "): "
                + (msg.success() ? "INFO" : "ERROR") + ": " + msg.message();
    }).collect(Collectors.joining("\n"));

    final boolean any_errors = status_messages.stream().anyMatch(msg -> !msg.success());

    // Only going to do something if we have errors:

    if (any_errors) {
        _logger.warn(ErrorUtils.get("Error creating/updating shared library bean: {0} error= {1}", id,
                message_block.replace("\n", "; ")));
        return share_db.getObjectById(new ObjectId(id), Arrays.asList("title", "description"), true)
                .thenCompose(jsonopt -> {
                    if (jsonopt.isPresent()) { // (else share has vanished, nothing to do)

                        final CommonUpdateComponent<JsonNode> v1_update = Optional
                                .of(CrudUtils.update().set("description",
                                        safeJsonGet("description", jsonopt.get()).asText() + "\n\n"
                                                + message_block))
                                // If shared lib already exists then can't update the title (or the existing lib bean will get deleted)
                                .map(c -> create_not_update
                                        ? c.set("title",
                                                "ERROR:" + safeJsonGet("title", jsonopt.get()).asText())
                                        : c)
                                .get();

                        @SuppressWarnings("unchecked")
                        final CompletableFuture<Boolean> v2_res = Lambdas.get(() -> {
                            if (!create_not_update) { // also make a token effort to update the timestamp on the shared lib bean, so the same error doesn't keep getting repeated
                                final CommonUpdateComponent<SharedLibraryBean> v2_update = CrudUtils
                                        .update(SharedLibraryBean.class)
                                        .set(SharedLibraryBean::modified, new Date());

                                //(need to do this because as of Aug 2015, the updateObjectById isn't plumbed in)
                                final ICrudService<SharedLibraryBean> library_service = (ICrudService<SharedLibraryBean>) (ICrudService<?>) library_mgmt
                                        .getUnderlyingPlatformDriver(ICrudService.class, Optional.empty())
                                        .get();

                                return library_service.updateObjectById("v1_" + id, v2_update); // (just fire this off and forget about it)
                            } else
                                return CompletableFuture.completedFuture(true);
                        });
                        final CompletableFuture<Boolean> update_res = v2_res.thenCompose(b -> {
                            if (b) {
                                return share_db.updateObjectById(new ObjectId(id), v1_update);
                            } else {
                                _logger.warn(ErrorUtils
                                        .get("Error creating/updating v2 library bean: {0} unknown error", id));
                                return CompletableFuture.completedFuture(false);
                            }
                        }).exceptionally(t -> {
                            _logger.warn(ErrorUtils.getLongForm(
                                    "Error creating/updating shared library bean: {1} error= {0}", t, id));
                            return false;
                        });
                        return update_res;
                    } else {
                        return CompletableFuture.completedFuture(false);
                    }
                });
    } else {
        return CompletableFuture.completedFuture(false);
    }
}

From source file:com.aiblockchain.api.StringUtils.java

/**
 * Returns a string having each item, of the given collection, represented as a string on a separate line.
 *
 * @param items the given collection// ww w.  ja  v  a 2 s . c  om
 * @return the string representation of the given items, one per line
 */
public static String toOneItemStringPerLine(final Collection<?> items) {
    //Preconditions
    assert items != null : "items must not be null";

    final StringBuilder stringBuilder = new StringBuilder();
    items.stream().forEach(item -> {
        stringBuilder.append(item);
        stringBuilder.append('\n');
    });
    return stringBuilder.toString();
}

From source file:com.github.rutledgepaulv.testsupport.CriteriaSerializer.java

private List<?> applyList(Collection<?> items) {
    return items.stream().map(item -> {
        if (item instanceof Enum<?>) {
            return Objects.toString(item);
        } else {/*from   w  w  w.  j  a va  2  s.c o m*/
            return item;
        }
    }).collect(Collectors.toList());
}

From source file:cc.kave.commons.pointsto.evaluation.cv.AbstractCVFoldBuilder.java

protected int calcAvgFoldSize(Collection<List<Usage>> usageLists) {
    return (int) (usageLists.stream().mapToLong(usages -> usages.size()).sum() / numFolds);
}

From source file:io.github.jeddict.orm.generator.util.ImportSet.java

@Override
public boolean addAll(Collection<? extends String> fqns) {
    return super.addAll(fqns.stream().filter(this::valid).collect(toSet()));
}

From source file:fi.hsl.parkandride.front.ReportController.java

@Inject
public ReportController(Collection<ReportService> reportServices) {
    this.reporters = reportServices.stream().collect(toMap(rs -> rs.reportName(), rs -> rs));
}

From source file:org.travis4j.rest.Request.java

private <T> List<String> toStringList(Collection<T> values) {
    return values.stream().map(Objects::toString).collect(Collectors.toList());
}

From source file:ru.anr.base.BaseParent.java

/**
 * Filters the given collection according to the specified predicate which
 * can be a lambda expression.// w  w  w.  ja  va2  s . c  o m
 * 
 * @param coll
 *            An original collection
 * @param predicate
 *            A predicate (can be a lambda expression)
 * @return The filtered collection
 * 
 * @param <S>
 *            The type of collection's items
 */
public static <S> List<S> filter(Collection<S> coll, Predicate<S> predicate) {

    return coll.stream().filter(predicate).collect(Collectors.toCollection(ArrayList::new));
}

From source file:nu.yona.server.test.util.BaseSpringIntegrationTest.java

private Set<CrudRepository<?, ?>> filterForCrudRepositories(Collection<Repository<?, ?>> values) {
    return values.stream().filter(r -> r instanceof CrudRepository).map(r -> (CrudRepository<?, ?>) r)
            .collect(Collectors.toSet());
}