Example usage for java.util List stream

List of usage examples for java.util List stream

Introduction

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

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:com.yahoo.bullet.parsing.RuleUtils.java

public static String makeFilter(String field, List<String> values, FilterType operation) {
    return "{" + "'field' : " + makeString(field) + ", " + "'operation' : "
            + makeString(getOperationFor(operation)) + ", " + "'values' : ['"
            + values.stream().reduce((a, b) -> a + "' , '" + b).orElse("") + "']" + "}";
}

From source file:com.uber.hoodie.common.model.HoodieTestUtils.java

public static void writeRecordsToLogFiles(FileSystem fs, String basePath, Schema schema,
        List<HoodieRecord> updatedRecords) {
    Map<HoodieRecordLocation, List<HoodieRecord>> groupedUpdated = updatedRecords.stream()
            .collect(Collectors.groupingBy(HoodieRecord::getCurrentLocation));

    groupedUpdated.entrySet().forEach(s -> {
        HoodieRecordLocation location = s.getKey();
        String partitionPath = s.getValue().get(0).getPartitionPath();

        Writer logWriter;/*  ww  w . jav  a  2s. c o m*/
        try {
            logWriter = HoodieLogFormat.newWriterBuilder().onParentPath(new Path(basePath, partitionPath))
                    .withFileExtension(HoodieLogFile.DELTA_EXTENSION).withFileId(location.getFileId())
                    .overBaseCommit(location.getCommitTime()).withFs(fs).build();

            Map<HoodieLogBlock.HeaderMetadataType, String> header = Maps.newHashMap();
            header.put(HoodieLogBlock.HeaderMetadataType.INSTANT_TIME, location.getCommitTime());
            header.put(HoodieLogBlock.HeaderMetadataType.SCHEMA, schema.toString());
            logWriter.appendBlock(new HoodieAvroDataBlock(s.getValue().stream().map(r -> {
                try {
                    GenericRecord val = (GenericRecord) r.getData().getInsertValue(schema).get();
                    HoodieAvroUtils.addHoodieKeyToRecord(val, r.getRecordKey(), r.getPartitionPath(), "");
                    return (IndexedRecord) val;
                } catch (IOException e) {
                    return null;
                }
            }).collect(Collectors.toList()), header));
            logWriter.close();
        } catch (Exception e) {
            fail(e.toString());
        }
    });
}

From source file:org.wallride.web.controller.admin.article.ArticleEditForm.java

public static ArticleEditForm fromDomainObject(Article article, Set<CustomField> customFields) {
    ArticleEditForm form = new ArticleEditForm();
    BeanUtils.copyProperties(article, form);

    if (article.getStatus().equals(Post.Status.DRAFT)) {
        form.setCode(article.getDraftedCode());
    }/*w  ww  .  ja  va 2  s. c  o m*/

    form.setCoverId(article.getCover() != null ? article.getCover().getId() : null);

    for (Category category : article.getCategories()) {
        form.getCategoryIds().add(category.getId());
    }

    List<String> tagNames = new ArrayList<>();
    for (Tag tag : article.getTags()) {
        tagNames.add(tag.getName());
    }
    form.setTags(StringUtils.join(tagNames, ","));

    for (Post post : article.getRelatedToPosts()) {
        form.getRelatedPostIds().add(post.getId());
    }

    if (article.getSeo() != null) {
        form.setSeoTitle(article.getSeo().getTitle());
        form.setSeoDescription(article.getSeo().getDescription());
        form.setSeoKeywords(article.getSeo().getKeywords());
    }

    List<CustomFieldValue> storedValues = new ArrayList<>(article.getCustomFieldValues());
    Map<CustomField, CustomFieldValue> storedFieldValueMap = new LinkedHashMap<>();
    storedValues.stream().forEach(value -> {
        storedFieldValueMap.put(value.getCustomField(), value);
    });
    for (CustomField orgField : customFields) {
        CustomFieldValueEditForm valueForm = new CustomFieldValueEditForm();
        valueForm.setCustomFieldId(orgField.getId());
        valueForm.setName(orgField.getName());
        valueForm.setDescription(orgField.getDescription());
        valueForm.setFieldType(orgField.getFieldType());
        valueForm.setOptions(orgField.getOptions());

        CustomFieldValue value = storedFieldValueMap.get(orgField);
        if (value != null) {
            valueForm.setId(value.getId());
            if (value.getCustomField().getFieldType().equals(CustomField.FieldType.CHECKBOX)) {
                if (value.getTextValue() != null) {
                    valueForm.setTextValues(value.getTextValue().split(","));
                }
            } else {
                valueForm.setTextValue(value.getTextValue());
            }
            valueForm.setStringValue(value.getStringValue());
            valueForm.setNumberValue(value.getNumberValue());
            valueForm.setDateValue(value.getDateValue());
            valueForm.setDatetimeValue(value.getDatetimeValue());
        }
        form.getCustomFieldValues().add(valueForm);
    }
    return form;
}

From source file:com.streamsets.pipeline.lib.el.StringEL.java

@ElFunction(prefix = "list", name = "joinSkipNulls", description = "Returns each element of a LIST field joined on the specified character sequence skipping null "
        + "values.")
public static String joinListSkipNulls(@ElParam("list") List<Field> list,
        @ElParam("separator") String separator) {
    if (list == null) {
        return "";
    }// w  w  w. j ava2s .  c om

    List<String> listOfStrings = list.stream()
            .map(field -> field.getValue() == null ? null : field.getValueAsString())
            .collect(Collectors.toList());
    return Joiner.on(separator).skipNulls().join(listOfStrings);
}

From source file:wiki.doc.DocResource.java

public static List<DocId> getAllLinkedDocs(List<DocId> docs, DbConnector dbc) {
    if (docs.size() == 0) {
        return new ArrayList<>(0);
    } else if (docs.size() <= 30000) {
        String inList = getParamList(docs.size());
        String sql = "SELECT pages.id FROM pages " + "INNER JOIN links ON links.toPage = pages.id "
                + "WHERE links.fromPage IN " + inList;
        Object[] ids = docs.stream().map((doc) -> doc.id).toArray();

        return dbc.jdbcTemplate.query(sql, ids, docIdMapper);
    } else {/*from   w w  w .ja  v  a 2s  .  c o m*/
        List<DocId> first = getAllLinkedDocs(docs.subList(0, 30000), dbc);
        first.addAll(getAllLinkedDocs(docs.subList(30000, docs.size()), dbc));
        return first;
    }
}

From source file:wiki.doc.DocResource.java

public static List<DocId> getAllLinking(List<DocId> docs, DbConnector dbc) {
    if (docs.size() == 0) {
        return new ArrayList<>(0);
    } else if (docs.size() <= 30000) {
        String inList = getParamList(docs.size());
        String sql = "SELECT pages.id FROM pages " + "INNER JOIN links ON links.fromPage = pages.id "
                + "WHERE links.toPage IN " + inList;
        Object[] ids = docs.stream().map((doc) -> doc.id).toArray();

        return dbc.jdbcTemplate.query(sql, ids, docIdMapper);
    } else {// w  w w .ja  v a 2s . c  om
        List<DocId> first = getAllLinking(docs.subList(0, 30000), dbc);
        first.addAll(getAllLinking(docs.subList(30000, docs.size()), dbc));
        return first;
    }
}

From source file:info.rmarcus.birkhoffvonneumann.MatrixUtils.java

public static int[] randomPermutationSparse(Random r, int n) {
    List<Integer> s = IntStream.range(0, n).mapToObj(i -> i)
            .collect(Collectors.toCollection(() -> new ArrayList<Integer>(n)));
    Collections.shuffle(s, r);/*  w w w.  j a  v  a 2s.  c  o  m*/
    return NullUtils.orThrow(s.stream().mapToInt(i -> i).toArray(),
            () -> new BVNRuntimeException("Could not convert ArrayList to array!"));
}

From source file:org.apache.gobblin.utils.HttpUtils.java

/**
 * Update {@link StatusType} of a {@link ResponseStatus} based on statusCode and error code white list
 *
 * @param status a status report after handling the a response
 * @param statusCode a status code in http domain
 * @param errorCodeWhitelist a whitelist specifies what http error codes are tolerable
 */// w w  w  .  j  a  va 2s.c o m
public static void updateStatusType(ResponseStatus status, int statusCode, Set<String> errorCodeWhitelist) {
    if (statusCode >= 300 & statusCode < 500) {
        List<String> whitelist = new ArrayList<>();
        whitelist.add(Integer.toString(statusCode));
        if (statusCode > 400) {
            whitelist.add(HttpConstants.CODE_4XX);
        } else {
            whitelist.add(HttpConstants.CODE_3XX);
        }
        if (whitelist.stream().anyMatch(errorCodeWhitelist::contains)) {
            status.setType(StatusType.CONTINUE);
        } else {
            status.setType(StatusType.CLIENT_ERROR);
        }
    } else if (statusCode >= 500) {
        List<String> whitelist = Arrays.asList(Integer.toString(statusCode), HttpConstants.CODE_5XX);
        if (whitelist.stream().anyMatch(errorCodeWhitelist::contains)) {
            status.setType(StatusType.CONTINUE);
        } else {
            status.setType(StatusType.SERVER_ERROR);
        }
    }
}

From source file:org.wallride.web.controller.admin.page.PageEditForm.java

public static PageEditForm fromDomainObject(Page page, Set<CustomField> allCustomFields) {
    PageEditForm form = new PageEditForm();
    BeanUtils.copyProperties(page, form);

    if (page.getStatus().equals(Post.Status.DRAFT)) {
        form.setCode(page.getDraftedCode());
    }/*w ww .  j a  v a  2  s .  c  om*/

    form.setCoverId(page.getCover() != null ? page.getCover().getId() : null);
    form.setParentId(page.getParent() != null ? page.getParent().getId() : null);

    for (Category category : page.getCategories()) {
        form.getCategoryIds().add(category.getId());
    }

    List<String> tagNames = new ArrayList<>();
    for (Tag tag : page.getTags()) {
        tagNames.add(tag.getName());
    }
    form.setTags(StringUtils.join(tagNames, ","));

    for (Post post : page.getRelatedToPosts()) {
        form.getRelatedPostIds().add(post.getId());
    }

    if (page.getSeo() != null) {
        form.setSeoTitle(page.getSeo().getTitle());
        form.setSeoDescription(page.getSeo().getDescription());
        form.setSeoKeywords(page.getSeo().getKeywords());
    }

    List<CustomFieldValue> storedValues = new ArrayList<>(page.getCustomFieldValues());
    Map<CustomField, CustomFieldValue> storedFieldValueMap = new LinkedHashMap<>();
    storedValues.stream().forEach(value -> {
        storedFieldValueMap.put(value.getCustomField(), value);
    });

    for (CustomField orgField : allCustomFields) {
        CustomFieldValueEditForm valueForm = new CustomFieldValueEditForm();
        valueForm.setCustomFieldId(orgField.getId());
        valueForm.setName(orgField.getName());
        valueForm.setDescription(orgField.getDescription());
        valueForm.setFieldType(orgField.getFieldType());
        valueForm.setOptions(orgField.getOptions());

        CustomFieldValue value = storedFieldValueMap.get(orgField);
        if (value != null) {
            valueForm.setId(value.getId());
            if (value.getCustomField().getFieldType().equals(CustomField.FieldType.CHECKBOX)) {
                if (value.getTextValue() != null) {
                    valueForm.setTextValues(value.getTextValue().split(","));
                }
            } else {
                valueForm.setTextValue(value.getTextValue());
            }
            valueForm.setStringValue(value.getStringValue());
            valueForm.setNumberValue(value.getNumberValue());
            valueForm.setDateValue(value.getDateValue());
            valueForm.setDatetimeValue(value.getDatetimeValue());
        }
        form.getCustomFieldValues().add(valueForm);
    }
    return form;
}

From source file:com.codelanx.codelanxlib.inventory.iinterface.InventoryPanel.java

static InventoryPanel valueOf(InventoryInterface ii, Object o) {
    Map<String, Object> map = Config.getConfigSectionValue(o);
    if (map == null || map.isEmpty()) {
        return null;
    }//from ww w  . j a  v a 2 s  .c o m
    List<Object> objs = (List<Object>) map.get("icons");
    Boolean root = Boolean.valueOf(String.valueOf(map.get("root")));
    String name = String.valueOf(map.get("name"));
    if (objs != null && root != null) {
        List<MenuIcon> icons = objs.stream().map(obj -> MenuIcon.valueOf(ii, obj)).filter(Lambdas::notNull)
                .collect(Collectors.toList());
        int rows;
        if (map.get("rows") == null) {
            rows = (icons.size() / 9) + 1;
        } else {
            rows = Integer.valueOf(String.valueOf(map.get("rows")));
        }
        InventoryPanel ip = ii.newPanel(name, rows);
        ip.icons.addAll(icons);
        ip.icons.forEach(i -> ip.locations.put(ip.index++, i));
        if (root) {
            ii.setRootPanel(ip);
        }
        return ip;
    } else {
        return null;
    }
}