88import java .util .LinkedList ;
99import java .util .List ;
1010import java .util .function .Function ;
11- import java .util .stream . Collectors ;
11+ import java .util .function . UnaryOperator ;
1212
1313public class Composition {
1414 /**
1515 * Dado que las funciones son tipos, podemos almacenarlas como datos en nuestra clase
1616 */
17- private static Function <String , File > createFile = path -> new File (path );
17+ private static final Function <String , File > createFile = File :: new ; // path -> new File(path)
1818
1919 /**
2020 * Las funciones pueden ser generadas a partir de una referencia.
2121 */
22- private static Function <File , List <String >> linesFromFile = Composition ::getLinesFromFile ;
22+ private static final Function <File , List <String >> linesFromFile = Composition ::getLinesFromFile ;
2323
24- private static Function <List <String >, List < String >> filter = list -> {
24+ private static final UnaryOperator <List <String >> filter = list -> { //Function<List<String>, List<String>>
2525 List <String > resultList = new LinkedList <>();
2626 list .forEach (s -> addIfNotEmpty (resultList , s ));
2727 return resultList ;
@@ -55,15 +55,16 @@ static List<String> stepsGetLinesWithContentCompose(String pathToFile) {
5555 Function <String , List <String >> createFileAndGetLines = linesFromFile .compose (createFile );
5656
5757 Function <String , List <String >> createFileGetLinesFilter = filter .compose (createFileAndGetLines );
58-
5958 return createFileGetLinesFilter .apply (pathToFile );
6059
6160 //Tambien podriamos haber ejecutado la primer funcion y ejecutar filter con el resultado:
62-
63- // List<String> lines = createFileAndGetLines.apply(pathToFile);
64- // return filter.apply(lines);
61+ //List<String> lines = createFileAndGetLines.apply(pathToFile)
62+ //return filter.apply(lines)
6563 }
6664
65+ /**
66+ * versión con andThen()
67+ */
6768 static List <String > getLinesWithContent (String pathToFile ) {
6869 return createFile
6970 .andThen (linesFromFile )
@@ -80,24 +81,20 @@ static List<String> stepsGetLinesWithContentAndThen(String pathToFile) {
8081 }
8182
8283 public static void main (String [] args ) {
83- String pathToFile = "/path/to/file.extension" ;
84-
85- System .out .println (
86- getLinesWithContentCompose (pathToFile )
87- );
84+ String pathToFile = "C:/Users/Martin F - PC Desk/Desktop/file.txt" ; // String pathToFile = "/path/to/file.extension"
85+ System .out .println (getLinesWithContentCompose (pathToFile ));
8886 }
8987
90-
9188 private static List <String > getLinesFromFile (File file ) {
9289 try (BufferedReader br = new BufferedReader (new FileReader (file ))) {
93- return br .lines ().collect ( Collectors . toList () );
90+ return br .lines ().toList ();
9491 } catch (IOException fileNotFoundEx ) {
9592 fileNotFoundEx .printStackTrace ();
9693 }
9794 return Collections .emptyList ();
9895 }
9996
10097 private static void addIfNotEmpty (List <String > list , String s ) {
101- if (s != null && s . length () > 0 && s . trim ().length () > 0 ) list .add (s );
98+ if (s != null && ! s . trim ().isEmpty () ) list .add (s );
10299 }
103100}
0 commit comments