Slide 1

Slide 1 text

MAD · NOV 23-24 · 2018 Refactoring to (effective and modern) Java 11 Aurea Programando desde Madrid Katia Programando desde París MAD · NOV 23-24 · 2018

Slide 2

Slide 2 text

MAD · NOV 23-24 · 2018

Slide 3

Slide 3 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 4

Slide 4 text

MAD · NOV 23-24 · 2018

Slide 5

Slide 5 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 6

Slide 6 text

MAD · NOV 23-24 · 2018 1995

Slide 7

Slide 7 text

MAD · NOV 23-24 · 2018 1995

Slide 8

Slide 8 text

MAD · NOV 23-24 · 2018 1995

Slide 9

Slide 9 text

MAD · NOV 23-24 · 2018 1995

Slide 10

Slide 10 text

MAD · NOV 23-24 · 2018 1995

Slide 11

Slide 11 text

MAD · NOV 23-24 · 2018 1995

Slide 12

Slide 12 text

MAD · NOV 23-24 · 2018 1995

Slide 13

Slide 13 text

MAD · NOV 23-24 · 2018 1995

Slide 14

Slide 14 text

MAD · NOV 23-24 · 2018 2011 2014 2017 2018 2019 7.0 8.0 9.0 10 11 12 13 2006 6.0 Cambio soporte de Oracle

Slide 15

Slide 15 text

MAD · NOV 23-24 · 2018

Slide 16

Slide 16 text

MAD · NOV 23-24 · 2018 Para entender el cambio de licencia Trisha Gee, evangelist Jet Brains https://blog.jetbrains.com/idea/2018/09/using-java-11-in-p roduction-important-things-to-know/ Stephen Colebourne, Joda man https://blog.joda.org/2018/09/time-to-look-beyond-oracles- jdk.html?m=1 Simone Bordet, Jetty, Charla Voxxed Days Ticino 2018 https://youtu.be/cUqcSFLnk_E

Slide 17

Slide 17 text

MAD · NOV 23-24 · 2018 Migrar a java 11 Compilar … compila, pero...

Slide 18

Slide 18 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 19

Slide 19 text

MAD · NOV 23-24 · 2018 default void mySuperMethod() { // Enjoy the 1000 lines of code }

Slide 20

Slide 20 text

MAD · NOV 23-24 · 2018 default void mySuperMethod() { // Enjoy the 1000 lines of code } Java 9: Private methods!!

Slide 21

Slide 21 text

MAD · NOV 23-24 · 2018 Person Woman Man Animal Dog Cat

Slide 22

Slide 22 text

MAD · NOV 23-24 · 2018 Person Woman Man Animal Dog Cat Cat Woman

Slide 23

Slide 23 text

MAD · NOV 23-24 · 2018 Person Woman Man Animal Dog Cat Cat Woman

Slide 24

Slide 24 text

MAD · NOV 23-24 · 2018 Interfaces - default methods java 8 default V merge(K key, V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); V oldValue = get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if(newValue == null) { remove(key); } else { put(key, newValue); } return newValue; }

Slide 25

Slide 25 text

MAD · NOV 23-24 · 2018 Interfaces - default methods java 8 default V merge(K key, V value, BiFunction remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); V oldValue = get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if(newValue == null) { remove(key); } else { put(key, newValue); } return newValue; } public interface Cache extends AsyncCache, ConcurrentMap {

Slide 26

Slide 26 text

MAD · NOV 23-24 · 2018 Interfaces - default methods java 8 V merge(K key, V value, BiFunction remappingFunction) { assertKeyNotNull(key); assertValueNotNull(value); assertFunctionNotNull(remappingFunction); ReadWriteKeyCommand command = commandsFactory.buildReadWriteKeyCommand(key, new MergeFunction<>(value, remappingFunction, metadata), keyPartitioner.getSegment(key), Params.fromFlagsBitSet(flags), getKeyDataConversion(), getValueDataConversion()); return executeCommandAndCommitIfNeeded(contextBuilder, command, 1); } public interface Cache extends AsyncCache, ConcurrentMap {

Slide 27

Slide 27 text

MAD · NOV 23-24 · 2018 Item 21: Design interfaces for posterity

Slide 28

Slide 28 text

MAD · NOV 23-24 · 2018 Item 21: Design interfaces for posterity

Slide 29

Slide 29 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 30

Slide 30 text

MAD · NOV 23-24 · 2018 La promesa ... “Use optional as a better alternative to null”

Slide 31

Slide 31 text

MAD · NOV 23-24 · 2018 Item 55: Return optionals judiciously

Slide 32

Slide 32 text

MAD · NOV 23-24 · 2018 Patterns for adopting optionals - getting

Slide 33

Slide 33 text

MAD · NOV 23-24 · 2018 Patterns for adopting optionals - getting

Slide 34

Slide 34 text

MAD · NOV 23-24 · 2018 Patterns for adopting optionals - extracting with map I

Slide 35

Slide 35 text

MAD · NOV 23-24 · 2018 Patterns for adopting optionals - extracting with map II

Slide 36

Slide 36 text

MAD · NOV 23-24 · 2018 Patterns for adopting optionals - extracting with map III

Slide 37

Slide 37 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 38

Slide 38 text

MAD · NOV 23-24 · 2018 Patterns for adopting optionals - getting

Slide 39

Slide 39 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 40

Slide 40 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 41

Slide 41 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 42

Slide 42 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 43

Slide 43 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 44

Slide 44 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 45

Slide 45 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 46

Slide 46 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 47

Slide 47 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 48

Slide 48 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 49

Slide 49 text

MAD · NOV 23-24 · 2018 Optionals - API Improvements

Slide 50

Slide 50 text

MAD · NOV 23-24 · 2018 Anti - patterns

Slide 51

Slide 51 text

MAD · NOV 23-24 · 2018 Anti - patterns

Slide 52

Slide 52 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 53

Slide 53 text

MAD · NOV 23-24 · 2018 Collections.sort(words, new Comparator() { public int compare(String s1, String s2) { return Integer.compare(s1.length(), s2.length()); }); Collections.sort(words, (s1, s2) -> Integer.compare(s1.length(), s2.length()));

Slide 54

Slide 54 text

MAD · NOV 23-24 · 2018 Item 42: Prefer lambdas to anonymous classes Lambdas - where use it

Slide 55

Slide 55 text

MAD · NOV 23-24 · 2018 Execute around pattern

Slide 56

Slide 56 text

MAD · NOV 23-24 · 2018 Execute around pattern

Slide 57

Slide 57 text

MAD · NOV 23-24 · 2018 Execute around pattern 1

Slide 58

Slide 58 text

MAD · NOV 23-24 · 2018 Execute around pattern 1 2

Slide 59

Slide 59 text

MAD · NOV 23-24 · 2018 Execute around pattern 1 2 3

Slide 60

Slide 60 text

MAD · NOV 23-24 · 2018 Item 44: Favor the use of standard functional interfaces

Slide 61

Slide 61 text

MAD · NOV 23-24 · 2018 Common functional interfaces added in java 8

Slide 62

Slide 62 text

MAD · NOV 23-24 · 2018 Execute around pattern with standard FI 1

Slide 63

Slide 63 text

MAD · NOV 23-24 · 2018 Execute around pattern with standard FI 1 2

Slide 64

Slide 64 text

MAD · NOV 23-24 · 2018 Execute around pattern with standard FI 1 2 3

Slide 65

Slide 65 text

MAD · NOV 23-24 · 2018 Item 43: Prefer method references to lambdas

Slide 66

Slide 66 text

MAD · NOV 23-24 · 2018 Execute around pattern

Slide 67

Slide 67 text

MAD · NOV 23-24 · 2018 Execute around pattern

Slide 68

Slide 68 text

MAD · NOV 23-24 · 2018

Slide 69

Slide 69 text

MAD · NOV 23-24 · 2018 Conditional Deferred Execution if (trace) { log.trace(format("LOCK[%s] Result is null on request %s",getName(),toString())); } Infinispan 1200

Slide 70

Slide 70 text

MAD · NOV 23-24 · 2018 Conditional Deferred Execution trace(format("LOCK[%s] Result is null on request %s", getName(), toString())); public void trace(String message){ if (trace) { log.trace(message); } }

Slide 71

Slide 71 text

MAD · NOV 23-24 · 2018 Conditional Deferred Execution trace(() -> format("LOCK[%s] Result is null on request %s", getName(),toString())); public void trace(Supplier messageSupplier){ if (trace) { log.trace(messageSupplier.get()); } }

Slide 72

Slide 72 text

MAD · NOV 23-24 · 2018 Factory Pattern Offer offer1 = OfferFactory.createOffer(OfferType.ADSL); Offer offer2 = OfferFactory.createOffer(OfferType.FIBER); Offer offer3 = OfferFactory.createOffer(OfferType.PHONE); List offers = Arrays.asList(offer1, offer2, offer3); for (Offer offer: offers) { System.out.println(offer.price()); }

Slide 73

Slide 73 text

MAD · NOV 23-24 · 2018 Factory Pattern public class OfferFactory { public static Offer createOffer(OfferType offerType) { Offer offer; switch (offerType) { case ADSL: offer = new InternetADSL(); break; case FIBER: offer = new InternetFiber(); break; ... default: throw new IllegalArgumentException("Offer type wrong"); } return offer; } }

Slide 74

Slide 74 text

MAD · NOV 23-24 · 2018 Factory Pattern Offer offer1 = OfferFactory.createOffer(OfferType.ADSL); Offer offer2 = OfferFactory.createOffer(OfferType.FIBER); Offer offer3 = OfferFactory.createOffer(OfferType.PHONE); List offers = Arrays.asList(offer1, offer2, offer3); for (Offer offer: offers) { System.out.println(offer.price()); }

Slide 75

Slide 75 text

MAD · NOV 23-24 · 2018 Factory Pattern - Java 11 var offer1 = OfferFactory.createOffer(OfferType.ADSL); var offer2 = OfferFactory.createOffer(OfferType.FIBER); var offer3 = OfferFactory.createOffer(OfferType.PHONE); List offers = List.of(offer1, offer2, offer3); for (var offer: offers) { System.out.println(offer.price()); }

Slide 76

Slide 76 text

MAD · NOV 23-24 · 2018 Factory Pattern - Java 11 var offer1 = OfferFactory.createOffer(OfferType.ADSL); var offer2 = OfferFactory.createOffer(OfferType.FIBER); var offer3 = OfferFactory.createOffer(OfferType.PHONE); List offers = List.of(offer1, offer2, offer3); for (var offer: offers) { System.out.println(offer.price()); }

Slide 77

Slide 77 text

MAD · NOV 23-24 · 2018 Factory Pattern - Java 11 OfferFactory factory = OfferFactory.factory(builder -> { builder.add(OfferType.ADSL, () -> new InternetADSL(20)); builder.add(OfferType.FIBER, InternetFiber::new); builder.add(OfferType.PHONE, Phone::new); }); var offer1 = factory.createOffer(OfferType.ADSL); var offer2 = factory.createOffer(OfferType.FIBER); var offer3 = factory.createOffer(OfferType.PHONE); for (var offer: offers) { System.out.println(offer.price()); }

Slide 78

Slide 78 text

MAD · NOV 23-24 · 2018 Factory Pattern - Java 11 public interface OfferFactory { interface Builder { void add(OfferType offerType, Supplier supplier); } static OfferFactory factory(Consumer consumer) { Map> map = new HashMap<>(); consumer.accept(map::put); return offerType -> map.get(offerType).get(); } Offer createOffer(OfferType offerType); }

Slide 79

Slide 79 text

MAD · NOV 23-24 · 2018 Factory Pattern - Java 11 public interface OfferFactory { interface Builder { void add(OfferType offerType, Supplier supplier); } static OfferFactory factory(Consumer consumer) { Map> map = new HashMap<>(); consumer.accept(map::put); return offerType -> map.get(offerType).get(); } Offer createOffer(OfferType offerType); } public interface Function { R apply(T t); ...

Slide 80

Slide 80 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 81

Slide 81 text

MAD · NOV 23-24 · 2018

Slide 82

Slide 82 text

MAD · NOV 23-24 · 2018 Item 46: Prefer side-effect-free functions in streams

Slide 83

Slide 83 text

MAD · NOV 23-24 · 2018 Streams anti pattern

Slide 84

Slide 84 text

MAD · NOV 23-24 · 2018 Streams anti pattern

Slide 85

Slide 85 text

MAD · NOV 23-24 · 2018 Streams anti pattern

Slide 86

Slide 86 text

MAD · NOV 23-24 · 2018 Item 47: Prefer Collection to Stream as a return type

Slide 87

Slide 87 text

MAD · NOV 23-24 · 2018 Stream API improvements takeWhile() dropWhile() iterate() ofNullable()

Slide 88

Slide 88 text

MAD · NOV 23-24 · 2018 Stream API improvements

Slide 89

Slide 89 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 90

Slide 90 text

MAD · NOV 23-24 · 2018 Jigsaw - 10 años - < 9 = Classpath Hell - Móviles, Cloud …

Slide 91

Slide 91 text

MAD · NOV 23-24 · 2018 module-info.java module org.infinispan.multimap { exports org.infinispan.multimap.api.embedded; requires org.infinispan.common; requires org.infinispan.core; }

Slide 92

Slide 92 text

MAD · NOV 23-24 · 2018 module-info.java module org.infinispan.multimap { exports org.infinispan.multimap.api.embedded; requires transitive org.infinispan.core; }

Slide 93

Slide 93 text

MAD · NOV 23-24 · 2018 module-info.java open module org.infinispan.multimap { exports org.infinispan.multimap.api.embedded; requires transitive org.infinispan.core; }

Slide 94

Slide 94 text

MAD · NOV 23-24 · 2018 module-info.java module org.infinispan.core { exports org.infinispan.manager.impl to org.infinispan.multimap.impl; requires org.infinispan.common; }

Slide 95

Slide 95 text

MAD · NOV 23-24 · 2018 Item 15: Minimize the accessibility of classes and members

Slide 96

Slide 96 text

MAD · NOV 23-24 · 2018 Item 15: Minimize the accessibility of classes and members

Slide 97

Slide 97 text

MAD · NOV 23-24 · 2018

Slide 98

Slide 98 text

MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces Optionals Lambdas Streams Modules Conclusions

Slide 99

Slide 99 text

MAD · NOV 23-24 · 2018 Una lista no exhaustiva de más Java 11 (+9+10) CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid=400793 CORBA, SOAP ...

Slide 100

Slide 100 text

MAD · NOV 23-24 · 2018 Una lista no exhaustiva de más Java 11 (+9+10)

Slide 101

Slide 101 text

MAD · NOV 23-24 · 2018 Java 12 String literals (http://openjdk.java.net/jeps/326) Switch statements -> Pattern matching … (http://openjdk.java.net/jeps/325) String html = `

Hello World.

`; String html = "\n" + " \n" + "

Hello World.

\n" + " \n" + "\n"; int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; };

Slide 102

Slide 102 text

MAD · NOV 23-24 · 2018

Slide 103

Slide 103 text

MAD · NOV 23-24 · 2018 1 ? 2 ? 3 ? +3 ?

Slide 104

Slide 104 text

MAD · NOV 23-24 · 2018 Feedback @karesti @karesti_codes @aureamunoz @auritamh