Skip to content

Apply Optimize Java 8 Streams refactoring #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public Object get(String key) {

@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).filter(el -> el != null)
Optional<List<Map<String, Object>>> any = Stream.of(get(key)).parallel().filter(el -> el != null)
.map(el -> (List<Map<String, Object>>) el).findAny();
return any.isPresent() ? any.get().stream().map(constructor) : Stream.empty();
return any.isPresent() ? any.get().parallelStream().map(constructor) : Stream.empty();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public boolean tryAdvance(Consumer<? super Customer> action) {
throw new RuntimeException(e); // NOSONAR
}
}
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
}, false).parallel().onClose(() -> mutedClose(connection, statement, resultSet));
} catch (SQLException e) {
throw new CustomException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class InMemoryCustomerDao implements CustomerDao {
*/
@Override
public Stream<Customer> getAll() {
return idToCustomer.values().stream();
return idToCustomer.values().parallelStream();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void configure() throws Exception {
});

context.start();
context.getRoutes().stream().forEach(r -> LOGGER.info(r.toString()));
context.getRoutes().parallelStream().forEach(r -> LOGGER.info(r.toString()));
context.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void configure() throws Exception {
});
ProducerTemplate template = context.createProducerTemplate();
context.start();
context.getRoutes().stream().forEach(r -> LOGGER.info(r.toString()));
context.getRoutes().parallelStream().forEach(r -> LOGGER.info(r.toString()));
template.sendBody("direct:origin", "Hello from origin");
context.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion layers/src/main/java/com/iluwatar/layers/CakeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerIn
*/
public int calculateTotalCalories() {
int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;
total += cakeLayerInfos.stream().mapToInt(c -> c.calories).sum();
total += cakeLayerInfos.parallelStream().mapToInt(c -> c.calories).sum();
return total;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public static void main(String[] args) {
// find all walking creatures
LOGGER.info("Find all walking creatures");
List<Creature> walkingCreatures =
creatures.stream().filter(new MovementSelector(Movement.WALKING))
creatures.parallelStream().filter(new MovementSelector(Movement.WALKING))
.collect(Collectors.toList());
walkingCreatures.stream().forEach(c -> LOGGER.info(c.toString()));
// find all dark creatures
LOGGER.info("Find all dark creatures");
List<Creature> darkCreatures =
creatures.stream().filter(new ColorSelector(Color.DARK)).collect(Collectors.toList());
creatures.parallelStream().filter(new ColorSelector(Color.DARK)).collect(Collectors.toList());
darkCreatures.stream().forEach(c -> LOGGER.info(c.toString()));
// find all red and flying creatures
LOGGER.info("Find all red and flying creatures");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ public void testIdGeneration() throws Exception {
}

final List<Integer> ids = service.invokeAll(tasks)
.stream()
.parallelStream()
.map(TaskTest::get)
.filter(Objects::nonNull)
.collect(Collectors.toList());

service.shutdownNow();

final long uniqueIdCount = ids.stream()
final long uniqueIdCount = ids.parallelStream()
.unordered()
.distinct()
.count();

Expand Down