Example usage for java.util.stream Collectors summingInt

List of usage examples for java.util.stream Collectors summingInt

Introduction

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

Prototype

public static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper) 

Source Link

Document

Returns a Collector that produces the sum of a integer-valued function applied to the input elements.

Usage

From source file:hr.diskobolos.service.impl.DashboardServiceImpl.java

private DashboardDto.GroupsOfCategorization fetchGroupsOfCategorization(
        Map<MemberRegister, Integer> categorizationTotalPointsPerMemberRegister) {

    int totalPoints = categorizationTotalPointsPerMemberRegister.entrySet().stream()
            .collect(Collectors.summingInt(c -> c.getValue()));

    Map<IDashboardDto.CategorizationOfSportsGroup, Integer> categorizationOfSportsGroups = categorizationTotalPointsPerMemberRegister
            .entrySet().stream().collect(Collectors.groupingBy(t -> {
                int value = t.getValue();
                IDashboardDto.CategorizationOfSportsGroup categorizationOfSportsGroup = null;
                if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1;
                } else if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2;
                } else if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3;
                } else if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4;
                }//from ww w .ja  v  a2  s .c  o m
                return categorizationOfSportsGroup;
            }, Collectors.summingInt(t -> t.getValue())));

    DashboardDto.GroupsOfCategorization groupsOfCategorization = new DashboardDto.GroupsOfCategorization();
    groupsOfCategorization.setNumberOfMembersFirstCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1)));
    groupsOfCategorization.setNumberOfMembersSecondCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2)));
    groupsOfCategorization.setNumberOfMembersThirdCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3)));
    groupsOfCategorization.setNumberOfMembersFourthCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4)));
    return groupsOfCategorization;
}

From source file:uk.co.jassoft.markets.api.SentimentController.java

@PreAuthorize("permitAll")
@RequestMapping(value = "company/{id}", method = RequestMethod.GET)
public @ResponseBody SentimentByDate getCurrentSentimentsByCompany(final HttpServletResponse response,
        @PathVariable String id) {

    // This could do some mongoDB magic to only select the StorySentiments in the date range
    List<StorySentiment> storySentiments = storySentimentRepository.findByCompany(id);

    List<SentimentByDate> companySentiments = storySentiments.stream().filter(isToday())
            .sorted((s1, s2) -> s1.getStoryDate().compareTo(s2.getStoryDate()))
            .map(storySentiment -> new SentimentByDate(id, storySentiment.getStoryDate(),
                    storySentiment.getEntitySentiment().stream()
                            .collect(Collectors.summingInt(value -> value.getSentiment()))))
            .collect(Collectors.groupingBy(
                    sentimentByDate -> getTrincatedDate(PeriodType.Day, sentimentByDate.getDate()),
                    Collectors.summingInt(value1 -> value1.getSentiment())))
            .entrySet().stream()//from  ww  w .ja va2s.com
            .map(dateIntegerEntry -> new SentimentByDate(id, dateIntegerEntry.getKey(),
                    dateIntegerEntry.getValue()))
            .sorted((o1, o2) -> o1.getDate().compareTo(o2.getDate())).collect(Collectors.toList());

    response.setHeader("Cache-Control", "max-age=" + CacheTimeout.FIFTEEN_MINUTES);
    return companySentiments.isEmpty() ? new SentimentByDate(id, new Date(), null) : companySentiments.get(0);
}

From source file:com.sillelien.dollar.api.types.DollarList.java

@NotNull
@Override//from  w  w  w .j av  a 2  s.  co  m
public Integer toInteger() {
    return $stream(false).collect(
            Collectors.summingInt((java.util.function.ToIntFunction<NumericAware>) NumericAware::toInteger));
}

From source file:com.qcadoo.mes.cmmsMachineParts.states.MaintenanceEventStateValidationService.java

private Map<Entity, Integer> getGroupedStaffWorkTimes(Entity event) {
    List<Entity> staffWorkTimes = event.getHasManyField(MaintenanceEventFields.STAFF_WORK_TIMES);
    Function<Entity, Entity> toWorker = entity -> entity.getBelongsToField(StaffWorkTimeFields.WORKER);
    ToIntFunction<Entity> toInt = entity -> entity.getIntegerField(StaffWorkTimeFields.LABOR_TIME);
    Map<Entity, Integer> map = staffWorkTimes.stream()
            .collect(Collectors.groupingBy(toWorker, Collectors.summingInt(toInt)));
    return map;/*from   www .  j av a2  s.  c o m*/
}

From source file:de.blizzy.rust.lootconfig.LootConfigDump.java

private void fillItemCategoryDropChances(Category itemCategory, Set<SubSpawn> subSpawns, float parentChance,
        Multiset<Float> itemCategoryDropChances) {
    int totalWeight = subSpawns.stream().collect(Collectors.summingInt(subSpawn -> subSpawn.Weight));

    for (SubSpawn subSpawn : subSpawns) {
        float subSpawnChance = parentChance * subSpawn.Weight / totalWeight;
        if (subSpawn.Category == itemCategory) {
            itemCategoryDropChances.add(subSpawnChance);
        } else if (subSpawn.Category.hasSubSpawns()) {
            fillItemCategoryDropChances(itemCategory, subSpawn.Category.SubSpawn, subSpawnChance,
                    itemCategoryDropChances);
        }//from  w w w  .  j ava  2  s .co m
    }
}

From source file:com.github.sevntu.checkstyle.ordering.MethodOrder.java

public int getTotalSumOfMethodDistances() {
    return currentOrdering.stream().collect(Collectors.summingInt(caller -> {
        return getMethodDependenciesInAppearanceOrder(caller).stream().collect(Collectors.summingInt(callee -> {
            return Math.abs(getMethodsIndexDifference(caller, callee));
        }));/* w  w  w.  j av a 2s  . c  o  m*/
    }));
}

From source file:uk.co.jassoft.markets.api.SentimentController.java

@PreAuthorize("permitAll")
@RequestMapping(value = "{direction}/period/{period}/limit/{limit}", method = RequestMethod.GET)
public @ResponseBody List<CompanySentiment> getChartToday(final HttpServletResponse response,
        @PathVariable String direction, @PathVariable PeriodType period, @PathVariable int limit)
        throws UnknownHostException {

    List<StorySentiment> storySentiments = storySentimentRepository
            .findByStoryDateGreaterThan(DateUtils.truncate(new Date(), Calendar.DATE));

    List<CompanySentiment> todayCompanySentiments = storySentiments.stream()
            .map(storySentiment -> new ImmutablePair<>(storySentiment.getCompany(),
                    storySentiment.getEntitySentiment().stream()
                            .collect(Collectors.summingInt(value -> value.getSentiment()))))
            .collect(Collectors.groupingBy(pair -> pair.getKey(),
                    Collectors.summingInt(value -> value.getValue())))
            .entrySet().stream().map(stringIntegerEntry -> {
                Company company = companyRepository.findOne(stringIntegerEntry.getKey());
                return new CompanySentiment(stringIntegerEntry.getKey(), company.getName(),
                        stringIntegerEntry.getValue());
            }).sorted((o1, o2) -> {//ww  w . j a v  a  2 s.  c  o  m
                switch (direction) {
                case "highest":
                    return Integer.compare(o2.getSentiment(), o1.getSentiment());

                case "lowest":
                default:
                    return Integer.compare(o1.getSentiment(), o2.getSentiment());
                }
            }).limit(limit).collect(Collectors.toList());

    response.setHeader("Cache-Control", "max-age=" + CacheTimeout.FIFTEEN_MINUTES);
    return todayCompanySentiments;
}

From source file:com.github.sevntu.checkstyle.ordering.MethodOrder.java

public int getOverloadGroupsSplitCases() {
    return currentOrdering.stream().filter(method -> !method.isCtor())
            .collect(Collectors.groupingBy(Method::getName)).values().stream()
            .collect(Collectors.summingInt(this::getMethodGroupSplitCount));
}

From source file:com.github.sevntu.checkstyle.ordering.MethodOrder.java

public int getDependenciesBetweenDistantMethodsCases(int screenLinesCount) {
    return invocations.stream().collect(Collectors.groupingBy(MethodInvocation::getCaller)).values().stream()
            .collect(Collectors
                    .summingInt(callerInvocations -> (int) callerInvocations.stream().filter(invocation -> {
                        final int invocationLineNo = translateInitialLineNo(invocation.getInitialLineNo());
                        final int calleeLineNo = translateInitialLineNo(
                                invocation.getCallee().getInitialLineNo());
                        return Math.abs(calleeLineNo - invocationLineNo) > screenLinesCount;
                    }).filter(new UniqueCallerCalleeMethodInvocationFilter()).count()));
}

From source file:com.github.sevntu.checkstyle.ordering.MethodOrder.java

public int getAccessorsSplitCases() {
    return currentOrdering.stream().filter(method -> method.isGetter() || method.isSetter())
            .collect(Collectors.groupingBy(Method::getAccessiblePropertyName)).values().stream()
            .collect(Collectors.summingInt(this::getMethodGroupSplitCount));
}