Commit d5c47624 authored by Colin DAMON's avatar Colin DAMON

Merge branch '64-code-coffee-stream-code' into 'master'

Resolve "Code & coffee stream code"

Closes #64

See merge request !52
parents e268d66a ccb49a26
......@@ -22,3 +22,4 @@ include:
- local: "/trip-service-kata-typescript/.gitlab-ci.yml"
- local: "/roman-calculator/.gitlab-ci.yml"
- local: "/h2g2/.gitlab-ci.yml"
- local: "/java-streams/.gitlab-ci.yml"
......@@ -46,3 +46,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
| [Roman calculator](/roman-calculator) | Kata | Moyenne |
| [Atomic Design I and II](https://gitlab.ippon.fr/arey/pattern-library) | Front | Moyenne |
| [H2G2 (TypeScript)](/h2g2) | Kata | Moyenne |
| [Java streams](/java-streams) | Code&coffee | Moyenne |
package-java-streams:
variables:
PROJECT_FOLDER: "java-streams"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "java-streams/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>java-streams</artifactId>
<name>JavaStreams</name>
<developers>
<developer>
<email>arey@ippon.fr</email>
<name>Anthony REY</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Java streams
Code & coffee pour parler des streams en Java
- **Auteurs** : Anthony REY & Colin DAMON
- **Date** : 06/01/2021
- **Langage** : Java
- **Niveau** : Moyen
- **Replay** : TODO
package fr.ippon.streams;
public class Employee {
private final String name;
private final int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
package fr.ippon.streams;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Mall {
private final List<Shop> shops;
public Mall(Shop... shops) {
this.shops = Arrays.asList(shops);
}
public Collection<String> getShopNames() {
return shops.stream().map(Shop::getName).sorted().collect(Collectors.toList());
}
public String getEmployeesMeanAge() {
return employeesAgeStream()
.map(BigDecimal::valueOf)
.reduce(BigDecimal::add)
.map(this::toAverage)
.orElse(BigDecimal.ZERO)
.toPlainString();
}
private Stream<Integer> employeesAgeStream() {
return shops.stream().flatMap(this::toEmployeesAges);
}
private Stream<Integer> toEmployeesAges(Shop shop) {
return toEmployees(shop).map(Employee::getAge);
}
private BigDecimal toAverage(BigDecimal sum) {
return sum.divide(new BigDecimal(employeesAgeStream().count()), 2, RoundingMode.HALF_UP);
}
public Collection<String> getEmployeesOver(int age) {
return shops
.stream()
.flatMap(this::toEmployees)
.filter(employee -> overAge(age, employee))
.map(Employee::getName)
.sorted()
.collect(Collectors.toList());
}
private Stream<Employee> toEmployees(Shop shop) {
return shop.getEmployees().stream();
}
private boolean overAge(int age, Employee employee) {
return employee.getAge() > age;
}
}
package fr.ippon.streams;
import java.util.Arrays;
import java.util.Collection;
public class Shop {
private final String name;
private final Collection<Employee> employees;
public Shop(String name, Employee... employees) {
this.name = name;
this.employees = Arrays.asList(employees);
}
public String getName() {
return name;
}
public Collection<Employee> getEmployees() {
return employees;
}
}
package fr.ippon.streams;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class MallUnitTest {
private static final Mall mall = mall(shop("Nature", employee("Michel", 25)), shop("Beer", employee("Bob", 42), employee("John", 19)));
@Test
void shouldGetShopNames() {
assertThat(mall.getShopNames()).containsExactly("Beer", "Nature");
}
@Test
void shouldGetZeroAverageAgeWithoutShopInMall() {
assertThat(new Mall().getEmployeesMeanAge()).isEqualTo("0");
}
@Test
void shouldGetEmployeesAverageAge() {
assertThat(mall.getEmployeesMeanAge()).isEqualTo("28.67");
}
@Test
void shouldGetEmployeesOverAge() {
assertThat(mall.getEmployeesOver(20)).containsExactly("Bob", "Michel");
}
private static Mall mall(Shop... shops) {
return new Mall(shops);
}
private static Shop shop(String name, Employee... employees) {
return new Shop(name, employees);
}
private static Employee employee(String name, int age) {
return new Employee(name, age);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment