|
| 1 | +package com.platzi.functional_student_practice._14_optionals; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class C22Optionals { |
| 6 | + |
| 7 | + static Random randomGenerator = new Random(); |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + System.out.println("////////////////////////////////////////////////////////////////////////////////////"); |
| 11 | + System.out.println("\nCLASS 22\n"); |
| 12 | + |
| 13 | + // 1. ejemplo iterando con una lista posiblemente vacia |
| 14 | + List<String> names = getNames(); |
| 15 | + System.out.println(names.isEmpty()? "1. Lista vacia" : "1. "+names.getFirst()); |
| 16 | + |
| 17 | + // 2. Por cada retorno de un posible null, tenemos que validar que el dato este efecticamente presente |
| 18 | + String name = mostValuablePlayer(); |
| 19 | + System.out.println("2. "+((name != null)? name : "Valor 'null' invalido")); |
| 20 | + |
| 21 | + // 3a. ejemplo con Optional java 8 |
| 22 | + Optional<List<String>> optionalNames = getOptionalNames(); |
| 23 | + if (optionalNames.isPresent()) optionalNames.get().forEach(x -> System.out.println("3: " + x)); |
| 24 | + else System.out.println("3: Optional vacio"); |
| 25 | + |
| 26 | + // 3b. ejemplo con Optional java 9+ |
| 27 | + Optional<List<String>> optionalNames2 = getOptionalNames(); |
| 28 | + optionalNames2.ifPresentOrElse( |
| 29 | + namesValue -> namesValue.forEach(x -> System.out.println("3b: " + x)), |
| 30 | + () -> System.out.println("3b: Optional vacio")); |
| 31 | + |
| 32 | + // 4. segundo ejemplo Optional |
| 33 | + System.out.println("4. "+optionalValuablePlayer().orElse("No player")); |
| 34 | + } |
| 35 | + |
| 36 | + // 1. Al retornar collecctions (lists, hashs, maps, etc), es viable devolver un collection vacio (un wrap a datos inexistentes) |
| 37 | + static List<String> getNames() { |
| 38 | + List<String> list = new LinkedList<>(); |
| 39 | + String word = randomStringGenerator(); |
| 40 | + if (word != null) list.add(word); |
| 41 | + if (list.isEmpty()) return Collections.emptyList(); |
| 42 | + return list; |
| 43 | + } |
| 44 | + |
| 45 | + // 2. Es mas problematico si tenemos que retornar String null, o un primitivo que represente un "no valor". |
| 46 | + static String mostValuablePlayer() { |
| 47 | + return randomStringGenerator(); |
| 48 | + } |
| 49 | + |
| 50 | + // 2b. ejemplo con primitivo int |
| 51 | + static int mostExpensiveItem() { |
| 52 | + return -1; |
| 53 | + //no se puede devolver un primitivo "sin valor" |
| 54 | + } |
| 55 | + |
| 56 | + // 3. Optional de lista. en caso de estar vacia se devuelve un Observable vacio (en reemplazo de la lista vacia) |
| 57 | + static Optional<List<String>> getOptionalNames() { |
| 58 | + List<String> namesList = new LinkedList<>(); |
| 59 | + for (int i = 0; i < 2; i++) { |
| 60 | + String word = randomStringGenerator(); |
| 61 | + if (word != null) namesList.add(word); |
| 62 | + } |
| 63 | + if (namesList.isEmpty()) return Optional.empty(); |
| 64 | + return Optional.of(namesList); |
| 65 | + } |
| 66 | + |
| 67 | + // 4. Optional de valor potencialmente null |
| 68 | + static Optional<String> optionalValuablePlayer() { |
| 69 | + //en caso de contener un valor nulo, evita generar un NullPointerException como haría el metodo Optional.of(...) |
| 70 | + return Optional.ofNullable(randomStringGenerator()); |
| 71 | + } |
| 72 | + |
| 73 | + static String randomStringGenerator() { |
| 74 | + double random = randomGenerator.nextDouble(); |
| 75 | + if (random < 0.25d) return "Señor Pepe"; |
| 76 | + if (random < 0.50d) return "Señor Anderson"; |
| 77 | + return null; |
| 78 | + } |
| 79 | +} |
0 commit comments