Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
twitch
live-coding-fr
Commits
ccb49a26
Commit
ccb49a26
authored
Jan 06, 2021
by
Colin DAMON
Browse files
Java streams code & coffee code
parent
e268d66a
Changes
9
Show whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
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"
README.md
View file @
ccb49a26
...
...
@@ -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 |
java-streams/.gitlab-ci.yml
0 → 100644
View file @
ccb49a26
package-java-streams
:
variables
:
PROJECT_FOLDER
:
"
java-streams"
extends
:
.java
only
:
refs
:
-
master
-
merge_requests
changes
:
-
"
.gitlab-common-ci.yml"
-
"
java-streams/**/*"
java-streams/pom.xml
0 → 100644
View file @
ccb49a26
<?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/readme.md
0 → 100644
View file @
ccb49a26
# 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
java-streams/src/main/java/fr/ippon/streams/Employee.java
0 → 100644
View file @
ccb49a26
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
;
}
}
java-streams/src/main/java/fr/ippon/streams/Mall.java
0 → 100644
View file @
ccb49a26
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
;
}
}
java-streams/src/main/java/fr/ippon/streams/Shop.java
0 → 100644
View file @
ccb49a26
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
;
}
}
java-streams/src/test/java/fr/ippon/streams/MallUnitTest.java
0 → 100644
View file @
ccb49a26
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment