JDK 7: Proposition 6 : Invocations chainées
Aujourd'hui :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
class Builder {
void setSomething(Something x) {
}
void setOther(Other x) {
}
Thing result() {
}
}
Builder builder = new Builder();
builder.setSomething(something);
builder.setOther(other);
Thing thing = builder.result(); |
Demain :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
class Builder {
void setSomething(Something x) {
}
void setOther(Other x) {
}
Thing result() {
}
}
Thing thing = new Builder()
.setSomething(something)
.setOther(other)
.result(); |
En combinant cette proposition avec la proposition 5, il serait possible d'écrire du code de ce style :
Code:
1 2 3 4 5 6 7 8 9 10
|
import static java.util.Collections.sort;
List<String> strings = ...;
strings
.filter(isCountryName) // could be a closure
.sort()
.uniq()
.each(printString); // ditto |