import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target= "_blank"></a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
@FunctionalInterface
public interface FirstInterface {
public void singleMethod(String param);
}
public final static class Employee {
private String name;
private int eta;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEta() {
return eta;
}
public void setEta(int eta) {
this.eta = eta;
}
@Override
public String toString() {
return "Employee [eta=" + eta + ", name=" + name + "]";
}
private Employee(String name, int eta) {
this.name = name;
this.eta = eta;
}
}
private static class UniTest {
/**
* @see Function#apply(Object)
*/
public void function_populate(List<Employee> source) {
// logica - soluzione parziale
Function<Employee, String> EmployToName = (Employee e) -> {
return e.getName();
};
// flusso di ritorno
List<String> returnFlow = new ArrayList<String>();
for (Employee employee : source)
returnFlow.add(EmployToName.apply(employee));
returnFlow.forEach(System.out::println);
}
/**
* @see Function#andThen(Function)
*/
public void function_filter(List<Employee> source) {
// logica - soluzione parziale
Function<Employee, String> EmployToName = (Employee e) -> {
return e.getName();
};
// logica - filtro
Function<String, String> filter = (String s) -> s.substring(0, 1);
List<String> returnFlow = new ArrayList<String>();
for (Employee employee : source)
returnFlow.add(EmployToName.andThen(filter).apply(employee));
returnFlow.forEach(str -> {
System.out.println(" " + str);
});
}
/**
* @see Function#compose(Function)
*/
public void function_compose(List<Employee> source) {
// logica - soluzione parziale
Function<Employee, String> EmployToName = (Employee e) -> {
return e.getName();
};
// logica - soluzione parziale
Function<Employee, Employee> EmployToFirstName = (Employee e) -> {
int index = e.getName().indexOf(" ");
String firstName = e.getName().substring(0, index);
e.setName(firstName);
return e;
};
// flusso di ritorno
List<String> returnFlow = new ArrayList<String>();
for (Employee employee : source)
returnFlow.add(EmployToName.compose(EmployToFirstName).apply(employee));
returnFlow.forEach(str -> {
System.out.println(" " + str);
});
}
/**
* @see Function#identity()
*/
public void function_identity(List<Employee> source) {
for (Employee employee : source)
System.out.println(Function.identity().apply(employee));
}
public static List<Employee> createTest() {
// elementi in serie
return Arrays.asList(new Employee("Tom Jones", 45), new Employee("Harry Major", 25),
new Employee("Ethan Hardy", 65), new Employee("Nancy Smith", 15),
new Employee("Deborah Sprightly", 29));
};
}
public static void main(String args[]) {
// Unit test - popolare un flusso da una sorgente dati
// new UniTest().function_populate(UniTest.createTest());
// Unit test - filtrare un flusso
// new UniTest().function_filter(UniTest.createTest());
// Unit test - composizione di due flussi
//new UniTest().function_compose(UniTest.createTest());
// Unit test - elemento di un flusso
new UniTest().function_identity(UniTest.createTest());
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: