├── .gitignore
├── .settings
├── org.eclipse.m2e.core.prefs
├── org.eclipse.jdt.apt.core.prefs
└── org.eclipse.jdt.core.prefs
├── .project
├── src
└── main
│ └── java
│ └── victor
│ └── clean
│ └── lambdas
│ ├── D__Passing_a_Block.java
│ ├── A__We_Love_Names.java
│ ├── C__Optional_Slays_NPE.java
│ ├── E__TypeSpecific_Functionality.java
│ ├── D2__Loan_Pattern.java
│ └── B__Stream_Wrecks.java
├── LICENSE
├── .classpath
├── pom.xml
└── .factorypath
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.apt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.apt.aptEnabled=true
3 | org.eclipse.jdt.apt.genSrcDir=target\\generated-sources\\annotations
4 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3 | org.eclipse.jdt.core.compiler.compliance=1.8
4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5 | org.eclipse.jdt.core.compiler.processAnnotations=enabled
6 | org.eclipse.jdt.core.compiler.source=1.8
7 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | clean-code-java8-devoxxfr
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/main/java/victor/clean/lambdas/D__Passing_a_Block.java:
--------------------------------------------------------------------------------
1 | package victor.clean.lambdas;
2 |
3 | import java.util.Random;
4 | import java.util.function.Consumer;
5 |
6 | import lombok.Data;
7 |
8 |
9 | // VVVVVVVVV ==== supporting (dummy) code ==== VVVVVVVVV
10 | class EmailContext implements AutoCloseable {
11 | public boolean send(Email email) {
12 | boolean r = new Random().nextBoolean();
13 | System.out.println("Sending "+(r?"OK":"KO")+" email " + email.getSubject());
14 | return r;
15 | }
16 |
17 | public void close() {
18 | }
19 | }
20 |
21 | @Data
22 | class Email {
23 | private String sender;
24 | private String subject;
25 | private String body;
26 | private String replyTo;
27 | private String to;
28 | }
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Victor Rentea
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/main/java/victor/clean/lambdas/A__We_Love_Names.java:
--------------------------------------------------------------------------------
1 | package victor.clean.lambdas;
2 |
3 | import static java.util.stream.Collectors.toList;
4 |
5 | import java.time.LocalDate;
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | import lombok.Data;
10 |
11 | // get the list of users to UI
12 |
13 | class UserFacade {
14 |
15 | private UserRepo userRepo;
16 | private UserMapper mapper;
17 |
18 | public List getAllUsers() {
19 | List users = userRepo.findAll();
20 | return users.stream().map(mapper::toDto).collect(toList());
21 | }
22 |
23 | }
24 |
25 | class UserMapper {
26 | // Inject stufff
27 | public UserDto toDto(User user) {
28 | UserDto dto = new UserDto();
29 | dto.setUsername(user.getUsername());
30 | dto.setFullName(user.getFirstName() + " " + user.getLastName().toUpperCase());
31 | dto.setActive(user.getDeactivationDate() == null);
32 | return dto;
33 | }
34 |
35 | }
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | // VVVVVVVVV ==== supporting (dummy) code ==== VVVVVVVVV
44 | interface UserRepo {
45 | List findAll();
46 | }
47 |
48 | @Data
49 | class User {
50 | private String firstName;
51 | private String lastName;
52 | private String username;
53 | private LocalDate deactivationDate;
54 | }
55 |
56 | @Data
57 | class UserDto {
58 | // public UserDto(User user) {
59 | // this.setUsername(user.getUsername());
60 | // this.setFullName(user.getFirstName() + " " + user.getLastName().toUpperCase());
61 | // this.setActive(user.getDeactivationDate() == null);
62 | // }
63 | private String fullName;
64 | private String username;
65 | private boolean active;
66 | }
67 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | victor.training
5 | clean-code-java8-devoxxfr
6 | 1.0
7 |
8 |
9 |
10 |
11 | org.apache.maven.plugins
12 | maven-compiler-plugin
13 | 2.3.2
14 |
15 | 1.8
16 | 1.8
17 |
18 |
19 |
20 |
21 |
22 |
23 | org.mockito
24 | mockito-all
25 | 2.0.2-beta
26 |
27 |
28 | org.jooq
29 | jool
30 | 0.9.12
31 |
32 |
33 | org.projectlombok
34 | lombok
35 | 1.16.16
36 |
37 |
38 | org.springframework.data
39 | spring-data-jpa
40 | 1.11.9.RELEASE
41 |
42 |
43 | junit
44 | junit
45 | 4.12
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/src/main/java/victor/clean/lambdas/C__Optional_Slays_NPE.java:
--------------------------------------------------------------------------------
1 | package victor.clean.lambdas;
2 |
3 | import static java.util.Optional.empty;
4 | import static java.util.Optional.of;
5 | import static java.util.Optional.ofNullable;
6 |
7 | import java.util.Optional;
8 |
9 | import lombok.Data;
10 |
11 | // Sir Charles Antony Richard: "I call it my billion-dollar mistake.
12 | // It was the invention of the null reference in 1965..."
13 |
14 | // Get a discount line to print in UI
15 |
16 | class DiscountService {
17 | public String getDiscountLine(Customer customer) {
18 | return customer.getMemberCard()
19 | .flatMap(card -> getApplicableDiscountPercentage(card))
20 | .map(d -> "Discount: " + d)
21 | .orElse("");
22 | }
23 |
24 | private Optional