├── .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 getApplicableDiscountPercentage(MemberCard card) { 25 | if (card.getFidelityPoints() >= 100) { 26 | return of(5); 27 | } 28 | if (card.getFidelityPoints() >= 50) { 29 | return of(3); 30 | } 31 | return empty(); 32 | } 33 | 34 | // test: 60, 10, no MemberCard 35 | public static void main(String[] args) { 36 | DiscountService service = new DiscountService(); 37 | System.out.println(">" + service.getDiscountLine(new Customer(new MemberCard(60)))); 38 | System.out.println(">" + service.getDiscountLine(new Customer(new MemberCard(10)))); 39 | System.out.println(">" + service.getDiscountLine(new Customer())); 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | // VVVVVVVVV ==== supporting (dummy) code ==== VVVVVVVVV 50 | class Customer { 51 | private MemberCard memberCard; 52 | public Customer() { 53 | } 54 | public Customer(MemberCard profile) { 55 | this.memberCard = profile; 56 | } 57 | public Optional getMemberCard() { 58 | return ofNullable(memberCard); 59 | } 60 | } 61 | 62 | @Data 63 | class MemberCard { 64 | private final int fidelityPoints; 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/victor/clean/lambdas/E__TypeSpecific_Functionality.java: -------------------------------------------------------------------------------- 1 | package victor.clean.lambdas; 2 | 3 | import static org.mockito.Mockito.mock; 4 | import static org.mockito.Mockito.when; 5 | 6 | import java.util.function.BiFunction; 7 | 8 | import victor.clean.lambdas.Movie.Type; 9 | 10 | class Movie { 11 | enum Type { 12 | REGULAR(PriceService::computeRegularPrice), 13 | NEW_RELEASE(PriceService::computeNewReleasePrice), 14 | CHILDREN(PriceService::computeChildrenPrice); 15 | public final BiFunction priceAlgo; 16 | 17 | private Type(BiFunction priceAlgo) { 18 | this.priceAlgo = priceAlgo; 19 | } 20 | } 21 | 22 | private final Type type; 23 | 24 | public Movie(Type type) { 25 | this.type = type; 26 | } 27 | 28 | } 29 | 30 | interface FactorRepo { 31 | Double getFactor(); 32 | } 33 | 34 | class PriceService { 35 | private final FactorRepo repo; 36 | 37 | 38 | public PriceService(FactorRepo repo) { 39 | this.repo = repo; 40 | } 41 | 42 | protected Integer computeNewReleasePrice(int days) { 43 | return (int) (repo.getFactor() * days); 44 | } 45 | 46 | protected Integer computeRegularPrice(int days) { 47 | return days + 1; 48 | } 49 | 50 | protected Integer computeChildrenPrice(int days) { 51 | return 5; 52 | } 53 | 54 | public Integer computePrice(Movie.Type type, int days ) { 55 | return type.priceAlgo.apply(this, days); 56 | } 57 | 58 | } 59 | 60 | public class E__TypeSpecific_Functionality { 61 | public static void main(String[] args) { 62 | FactorRepo repo = mock(FactorRepo.class); 63 | when(repo.getFactor()).thenReturn(2d); 64 | PriceService priceService = new PriceService(repo); 65 | System.out.println(priceService.computePrice(Type.REGULAR, 2)); 66 | System.out.println(priceService.computePrice(Type.NEW_RELEASE, 2)); 67 | System.out.println(priceService.computePrice(Type.CHILDREN, 2)); 68 | System.out.println("COMMIT now!"); 69 | } 70 | } -------------------------------------------------------------------------------- /src/main/java/victor/clean/lambdas/D2__Loan_Pattern.java: -------------------------------------------------------------------------------- 1 | package victor.clean.lambdas; 2 | 3 | import java.io.File; 4 | import java.io.FileWriter; 5 | import java.io.IOException; 6 | import java.io.Writer; 7 | import java.util.function.Consumer; 8 | import java.util.stream.Stream; 9 | 10 | import org.jooq.lambda.Unchecked; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | import org.springframework.data.jpa.repository.JpaRepository; 14 | 15 | // export all orders to a file 16 | 17 | interface OrderRepo extends JpaRepository { // J'aime Spring Data! 18 | Stream findByActiveTrue(); // 1 Mln orders ;) 19 | } 20 | class FileExporter { 21 | private final static Logger log = LoggerFactory.getLogger(FileExporter.class); 22 | 23 | public File exportFile(String fileName, Consumer contentWriter) throws Exception { 24 | File file = new File("export/" + fileName); 25 | try (Writer writer = new FileWriter(file)) { 26 | contentWriter.accept(writer); 27 | return file; 28 | } catch (Exception e) { 29 | // TODO send email notification 30 | log.debug("Coucou!", e); // TERREUR ! 31 | throw e; 32 | } 33 | } 34 | 35 | public static void main(String[] args) throws Exception { 36 | FileExporter fileExporter = new FileExporter(); 37 | OrderExporterWriter orderExporterWriter = new OrderExporterWriter(); 38 | UserExporterWriter userExporterWriter = new UserExporterWriter(); 39 | 40 | fileExporter.exportFile("orders.txt", Unchecked.consumer(orderExporterWriter::writeContent)); 41 | fileExporter.exportFile("users.txt", Unchecked.consumer(userExporterWriter::writeContent)); 42 | } 43 | } 44 | class OrderExporterWriter { 45 | private OrderRepo repo; 46 | public void writeContent(Writer writer) throws IOException { 47 | writer.write("OrderID;Date\n"); 48 | try (Stream stream = repo.findByActiveTrue()) { 49 | stream 50 | .map(o -> o.getId() + ";" + o.getCreationDate()) 51 | .forEach(Unchecked.consumer(writer::write)); 52 | } 53 | } 54 | } 55 | 56 | class UserExporterWriter extends FileExporter { 57 | public void writeContent(Writer writer) throws IOException { 58 | // Neahhh! 59 | // TODO write User export contnt instead 60 | } 61 | } 62 | // CR: implement the same export for Users 63 | -------------------------------------------------------------------------------- /src/main/java/victor/clean/lambdas/B__Stream_Wrecks.java: -------------------------------------------------------------------------------- 1 | package victor.clean.lambdas; 2 | 3 | import static java.util.stream.Collectors.groupingBy; 4 | import static java.util.stream.Collectors.summingInt; 5 | import static java.util.stream.Collectors.toList; 6 | 7 | import java.time.LocalDate; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Map.Entry; 11 | import java.util.function.Predicate; 12 | import java.util.stream.Stream; 13 | 14 | import lombok.Data; 15 | 16 | // get the products frequently ordered during the past year 17 | 18 | class ProductService { 19 | private ProductRepo productRepo; 20 | 21 | public List getFrequentOrderedProducts(List orders) { 22 | List hiddenProductIds = productRepo.getHiddenProductIds(); 23 | Predicate productIsNotHidden = p-> !hiddenProductIds.contains(p.getId()); 24 | return getFrequentProductsOverTheLastYear(orders) 25 | .filter(Product::isNotDeleted) 26 | .filter(productIsNotHidden) 27 | .collect(toList()); 28 | } 29 | 30 | private Stream getFrequentProductsOverTheLastYear(List orders) { 31 | return getProductCounts(orders).entrySet().stream() 32 | .filter(e -> e.getValue() >= 10) 33 | .map(Entry::getKey); 34 | } 35 | 36 | private Map getProductCounts(List orders) { 37 | return orders.stream() 38 | .filter(this::orderWasCreatedDuringThePreviousYear) 39 | .flatMap(o -> o.getOrderLines().stream()) 40 | .collect(groupingBy(OrderLine::getProduct, summingInt(OrderLine::getItemCount))); 41 | } 42 | 43 | private boolean orderWasCreatedDuringThePreviousYear(Order o) { 44 | return o.getCreationDate().isAfter(LocalDate.now().minusYears(1)); 45 | } 46 | } 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | //VVVVVVVVV ==== supporting (dummy) code ==== VVVVVVVVV 55 | @Data 56 | class Order { 57 | private Long id; 58 | private List orderLines; 59 | private LocalDate creationDate; 60 | } 61 | 62 | @Data 63 | class OrderLine { 64 | private Product product; 65 | private int itemCount; 66 | } 67 | 68 | @Data 69 | class Product { 70 | public boolean isNotDeleted() { 71 | return !deleted; 72 | } 73 | private Long id; 74 | private boolean deleted; 75 | } 76 | 77 | interface ProductRepo { 78 | List getHiddenProductIds(); 79 | } 80 | -------------------------------------------------------------------------------- /.factorypath: -------------------------------------------------------------------------------- 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 | --------------------------------------------------------------------------------