Alexis Seigneurin - @aseigneurin
Ippon Technologies - @ippontech
(Flèches droite/gauche et haut/bas pour naviguer, ESC pour tout afficher)
@FunctionalInterface
public interface ExampleInterface {
void doSomething();
default int methodWithDefaultImpl() {
return 0;
}
}
Unexpected @FunctionalInterface annotation
com.seigneurin.ExampleInterface is not a functional interface
multiple non-overriding abstract methods found in interface com.seigneurin.ExampleInterface
Button btn = new Button();
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
public class NameParser {
public <T> T parse(String name, Creator<T> creator) {
String[] tokens = name.split(" ");
String firstName = tokens[0];
String lastName = tokens[1];
return creator.create(firstName, lastName);
}
}
public interface Creator<T> {
T create(String firstName, String lastName);
}
public class Name {
private String firstName;
private String lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
}
NameParser parser = new NameParser();
Name res = parser.parse("Eric Clapton", new Creator<Name>() {
@Override
public Name create(String firstName, String lastName) {
return new Name(firstName, lastName);
}
});
@FunctionalInterface
public interface Creator<T> {
T create(String firstName, String lastName);
}
NameParser parser = new NameParser();
Name res = parser.parse("Eric Clapton", Name::new);
public class Factory {
public static Name createName(String firstName, String lastName) {
return new Name(firstName, lastName);
}
}
NameParser parser = new NameParser();
Name res = parser.parse("Eric Clapton", Factory::createName);
public class Factory {
public Name createName(String firstName, String lastName) {
return new Name(firstName, lastName);
}
}
Factory factory = new Factory();
NameParser parser = new NameParser();
Name res = parser.parse("Eric Clapton", factory::createName);
Name res = parser.parse("Eric Clapton", (s1, s2) -> new Name(s1, s2));
Name res = parser.parse("Eric Clapton", (s1, s2) -> Factory.createName(s1, s2));
Factory factory = new Factory();
Name res = parser.parse("Eric Clapton", (s1, s2) -> factory.createName(s1, s2));
public interface Operation<T>
{
T function();
void onSuccess(T res);
void onError(Exception ex);
}
public <T> void doSomething(Operation<T> operation) {
try {
T res = operation.function();
operation.onSuccess(res);
} catch (Exception ex) {
operation.onError(ex);
}
}
doSomething(new Operation<Object>() {
@Override
public Object function() {
return 42;
}
@Override
public void onSuccess(Object res) {
System.out.println(res);
}
@Override
public void onError(Exception ex) {
System.err.println("Error: " + ex.getMessage());
}
});
public <T> void doSomething(Supplier<T> function, Consumer<T> onSuccess, Consumer<Exception> onError) {
try {
T res = function.get();
onSuccess.accept(res);
} catch (Exception ex) {
onError.accept(ex);
}
}
doSomething(
() -> 42,
System.out::println,
ex -> System.err.println("Error: " + ex.getMessage()));
Code : github.com/aseigneurin/pres-java8-breizhcamp
Slides : aseigneurin.github.io/downloads/pres-java8-breizhcamp