-
Notifications
You must be signed in to change notification settings - Fork 0
Java Con Patron
DiegoS edited this page Dec 15, 2017
·
8 revisions
public void testReduceCollectionToOneWithoutTackingCareAboutTheOrder() {
Competitor[]arrayOfEntities = {
new EntityBar(),
new EntityFoo()
};
EntityCollection ec = new EntityCollection(arrayOfEntities);
assertEquals(new EntityFoo(), ec.winner());
}Esta es la porción de código que hace la reducción:
public Competitor winner() {
return this.competitors.stream()
.reduce(
new NullEntity(),
(Competitor a, Competitor b) -> a.challenge(b)
);
};Para saber que hace el challenge tenemos que mirar cada una de las instancias que componen la colección:
public class EntityBar implements Competitor {
public Competitor challenge(Competitor another) {
return another;
}
}public class EntityFoo implements Competitor {
public Competitor challenge(Competitor another) {
// It allways prevails
return this;
}
// ...
}public class NullEntity implements Competitor {
public Competitor challenge(Competitor another) {
return another;
}
}- El reduce comienza con un valore inicial (NullEntity).
- NullEntity desafía a EntityBar y siempre pierde.
- EntityBar desafía a EntityFoo y siempre gana este último.
- EntityFoo prevalece!
Código completo aquí.
Como resultado de implementar esto obtuvimos el siguiente reporte de cobertura.
