Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring to (effective and modern) Java 11

Katia Aresti
November 24, 2018

Refactoring to (effective and modern) Java 11

¡Java 11 ya esta aquí! Y Joshua Bloch ha publicado su tercera edición de “effective java”. Partiendo de ejemplos de proyectos en producción, y basándonos en las recomendaciones de Bloch y otros expertos, repasaremos algunos de los items que más aparecen en el día a día. Haremos especial hincapié en los patterns y 'best practices' a utilizar para no incurrir en un código vulnerable. Si buscas explicaciones detalladas sobre las nuevas funcionalidades de java, cómo usarlas, cuándo elegirlas frente a otras, ¡esta es tu charla!

Video: https://youtu.be/NCvW8-HZIGg

Katia Aresti

November 24, 2018
Tweet

More Decks by Katia Aresti

Other Decks in Programming

Transcript

  1. 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
  2. MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces

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

    Optionals Lambdas Streams Modules Conclusions
  4. 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
  5. 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
  6. MAD · NOV 23-24 · 2018 Migrar a java 11

    Compilar … compila, pero...
  7. MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces

    Optionals Lambdas Streams Modules Conclusions
  8. MAD · NOV 23-24 · 2018 default void mySuperMethod() {

    // Enjoy the 1000 lines of code } Java 9: Private methods!!
  9. MAD · NOV 23-24 · 2018 Interfaces - default methods

    java 8 default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> 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; }
  10. MAD · NOV 23-24 · 2018 Interfaces - default methods

    java 8 default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> 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<K, V> extends AsyncCache<K, V>, ConcurrentMap<K, V> {
  11. MAD · NOV 23-24 · 2018 Interfaces - default methods

    java 8 V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { assertKeyNotNull(key); assertValueNotNull(value); assertFunctionNotNull(remappingFunction); ReadWriteKeyCommand<K, V, V> 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<K, V> extends AsyncCache<K, V>, ConcurrentMap<K, V> {
  12. MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces

    Optionals Lambdas Streams Modules Conclusions
  13. MAD · NOV 23-24 · 2018 La promesa ... “Use

    optional as a better alternative to null”
  14. MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces

    Optionals Lambdas Streams Modules Conclusions
  15. MAD · NOV 23-24 · 2018 Collections.sort(words, new Comparator<String>() {

    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()));
  16. MAD · NOV 23-24 · 2018 Item 42: Prefer lambdas

    to anonymous classes Lambdas - where use it
  17. MAD · NOV 23-24 · 2018 Item 44: Favor the

    use of standard functional interfaces
  18. 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
  19. 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); } }
  20. MAD · NOV 23-24 · 2018 Conditional Deferred Execution trace(()

    -> format("LOCK[%s] Result is null on request %s", getName(),toString())); public void trace(Supplier<String> messageSupplier){ if (trace) { log.trace(messageSupplier.get()); } }
  21. 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<Offer> offers = Arrays.asList(offer1, offer2, offer3); for (Offer offer: offers) { System.out.println(offer.price()); }
  22. 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; } }
  23. 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<Offer> offers = Arrays.asList(offer1, offer2, offer3); for (Offer offer: offers) { System.out.println(offer.price()); }
  24. 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<Offer> offers = List.of(offer1, offer2, offer3); for (var offer: offers) { System.out.println(offer.price()); }
  25. 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<Offer> offers = List.of(offer1, offer2, offer3); for (var offer: offers) { System.out.println(offer.price()); }
  26. 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()); }
  27. MAD · NOV 23-24 · 2018 Factory Pattern - Java

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

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

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

    Optionals Lambdas Streams Modules Conclusions
  31. MAD · NOV 23-24 · 2018 Jigsaw - 10 años

    - < 9 = Classpath Hell - Móviles, Cloud …
  32. 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; }
  33. MAD · NOV 23-24 · 2018 module-info.java module org.infinispan.multimap {

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

    { exports org.infinispan.multimap.api.embedded; requires transitive org.infinispan.core; }
  35. 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; }
  36. MAD · NOV 23-24 · 2018 Item 15: Minimize the

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

    accessibility of classes and members
  38. MAD · NOV 23-24 · 2018 Agenda Java 11 Interfaces

    Optionals Lambdas Streams Modules Conclusions
  39. 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 ...
  40. 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 = `<html> <body> <p>Hello World.</p> </body> </html> `; String html = "<html>\n" + " <body>\n" + " <p>Hello World.</p>\n" + " </body>\n" + "</html>\n"; int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; };