@Override public RouterFunction<ServerResponse> build() { RouterFunction<ServerResponse> result = this.routerFunctions.stream() .reduce(RouterFunction::and) .orElseThrow(IllegalStateException::new); if (this.filterFunctions.isEmpty()) { return result; } else { HandlerFilterFunction<ServerResponse, ServerResponse> filter = this.filterFunctions.stream() .reduce(HandlerFilterFunction::andThen) .orElseThrow(IllegalStateException::new); return result.filter(filter); } }
Integer sum = collection.stream().reduce((s1, s2) -> s1 + s2).orElse(0); // using stream Api Integer sumOld = 0; // using Java 7 for(Integer i: collection) { Integer max1 = collection.stream().reduce((s1, s2) -> s1 > s2 ? s1 : s2).orElse(0); // using stream Api Integer max2 = collection.stream().reduce(Integer::max).orElse(0); // using stream Api using Integer::max Integer min = collection.stream().reduce((s1, s2) -> s1 < s2 ? s1 : s2).orElse(0); // using stream Api Integer last = collection.stream().reduce((s1, s2) -> s2).orElse(0); // using stream Api Integer sumMore2 = collection.stream().filter(o -> o > 2).reduce((s1, s2) -> s1 + s2).orElse(0); // using stream Api Integer sumMore2Old = 0; // using Java 7 for(Integer i: collection) { Integer sumOdd = collection.stream().filter(o -> o % 2 != 0).reduce((s1, s2) -> s1 + s2).orElse(0); // using stream Api Integer sumOddOld = 0; // using Java 7 for(Integer i: collection) { int oldMan = peoples.stream().filter((p) -> p.getSex() == Sex.MAN).map(People::getAge).reduce((s1, s2) -> s1 > s2 ? s1 : s2).get(); System.out.println("oldMan = " + oldMan); // print 69
private TupleDomain<ColumnHandle> toTupleDomain(Map<TpchColumnHandle, Set<NullableValue>> predicate) { return TupleDomain.withColumnDomains(predicate.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> { Type type = entry.getKey().getType(); return entry.getValue().stream() .map(nullableValue -> Domain.singleValue(type, nullableValue.getValue())) .reduce((Domain::union)) .orElse(Domain.none(type)); }))); }
/** * Initialized the router functions by detecting them in the application context. */ protected void initRouterFunctions() { List<RouterFunction<?>> routerFunctions = routerFunctions(); this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null); logRouterFunctions(routerFunctions); }
private Predicate<Transaction> constructFilterPredicate(List<String> from, List<String> to, List<String> message) { List<Predicate<Transaction>> predicates = new ArrayList<>(); if (from != null) { predicates.add(transaction -> from.contains(transaction.getFrom())); } if (to != null) { predicates.add(transaction -> to.contains(transaction.getTo())); } if (message != null) { predicates.add(transaction -> message.stream().filter(o -> ((String)o).contains(transaction.getMessage())).count() > 0); } // Elegant predicates combination return predicates.stream().reduce(transaction -> true, Predicate::and); }
public static Facet of(String name) { return stream(values()) .filter(f -> f.getName().equals(name)) .reduce((a, b) -> { throw new IllegalStateException("Multiple facets with same name: " + a + ", " + b); }) .orElseThrow(() -> new IllegalArgumentException(String.format("Facet name '%s' hasn't been found", name))); } }
private Stream<Film> serveFilmsWithfunctionalFold(String rating, int page) { System.out.format("serveFilmsWithfunctionalFold(String rating=%s, int page=%d) %n", rating, page); return Stream.<UnaryOperator<Stream<Film>>>of( (s) -> rating == null ? s : s.filter(Film.RATING.equal(rating)), s -> s.sorted(Film.LENGTH), s -> s.skip(page * PAGE_SIZE), s -> s.limit(PAGE_SIZE) ).reduce( films.stream(), (s, o) -> o.apply(s), (a, b) -> a ); }
private Optional<Event> verifyWindowContiguity(List<Event> newValues, Collector<String> out) { return newValues.stream() .sorted(Comparator.comparingLong(Event::getSequenceNumber)) .reduce((event, event2) -> { if (event2.getSequenceNumber() - 1 != event.getSequenceNumber()) { out.collect("Alert: events in window out ouf order!"); } return event2; }); } }
protected AccessTokenModel entityToModel(OAuth2AccessToken token) { AccessTokenModel model = new AccessTokenModel(); model.setAccess_token(token.getAccessToken()); model.setRefresh_token(token.getRefreshToken()); model.setExpires_in(token.getExpiresIn()); if (token.getScope() != null) { model.setScope(token.getScope().stream().reduce((t1, t2) -> t1.concat(",").concat(t2)).orElse("")); } else { model.setScope("public"); } model.setToken_type("bearer"); return model; }
public String description( String text ) { String wrappedText = wrapText( text, LINE_LENGTH ); if ( namedArgs.isEmpty() ) { return wrappedText; } wrappedText = String.join( NEWLINE + NEWLINE, wrappedText, "options:" ); //noinspection OptionalGetWithoutIsPresent handled by if-statement above final int alignLength = namedArgs.values().stream() .map( a -> a.optionsListing().length() ) .reduce( 0, Integer::max ); return String.join( NEWLINE, wrappedText, namedArgs.values().stream() .map( c -> formatArgumentDescription( alignLength, c ) ) .collect( Collectors.joining( NEWLINE ) ) ); }
@Override public Health check(Set<NodeHealth> nodeHealths) { Set<NodeHealth> appNodes = nodeHealths.stream() .filter(s -> s.getDetails().getType() == NodeDetails.Type.APPLICATION) .collect(toSet()); return Arrays.stream(AppNodeClusterHealthSubChecks.values()) .map(s -> s.check(appNodes)) .reduce(Health.GREEN, HealthReducer.INSTANCE); }