ItEr33S14CUCreacionUnidadesPlanificacion: Applying constraints using fluent interface

This commit is contained in:
Óscar González Fernández 2009-11-07 16:50:33 +01:00
parent 48adfbe914
commit f837510b18
2 changed files with 86 additions and 11 deletions

View file

@ -19,8 +19,10 @@
*/
package org.zkoss.ganttz.data.constraint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.zkoss.ganttz.util.WeakReferencedListeners;
import org.zkoss.ganttz.util.WeakReferencedListeners.IListenerNotification;
@ -35,6 +37,37 @@ public abstract class Constraint<T> {
public void constraintViolated(Constraint<T> constraint, T value);
}
public static class ConstraintBuilder<T> {
private final T value;
private final List<Constraint<T>> constraints = new ArrayList<Constraint<T>>();
public ConstraintBuilder(T value) {
this.value = value;
}
public ConstraintBuilder<T> withConstraints(
Constraint<T>... constraints) {
return withConstraints(Arrays.asList(constraints));
}
public ConstraintBuilder<T> withConstraints(
List<Constraint<T>> constraints) {
this.constraints.addAll(constraints);
return this;
}
public T apply() {
return Constraint.apply(value, constraints);
}
}
public static <T> ConstraintBuilder<T> initialValue(T value) {
return new ConstraintBuilder<T>(value);
}
public static <T> T apply(T initialValue, Constraint<T>... constraints) {
return apply(initialValue, Arrays.asList(constraints));
}

View file

@ -39,23 +39,15 @@ import org.zkoss.ganttz.data.constraint.Constraint.IConstraintViolationListener;
*/
public class ConstraintTest {
private Constraint<Integer> biggerThanFive = new Constraint<Integer>() {
private Constraint<Integer> biggerThanFive = biggerThan(5);
@Override
protected Integer applyConstraintTo(Integer currentValue) {
return Math.max(6, currentValue);
}
private Constraint<Integer> biggerThanSeven = biggerThan(7);
@Override
public boolean isSatisfiedBy(Integer value) {
return value != null && value > 5;
}
};
private Constraint<Integer> lessThanFive = new Constraint<Integer>() {
@Override
public boolean isSatisfiedBy(Integer value) {
return value!=null && value <5;
return value != null && value < 5;
}
@Override
@ -64,6 +56,21 @@ public class ConstraintTest {
}
};
private static Constraint<Integer> biggerThan(final int limit) {
return new Constraint<Integer>() {
@Override
protected Integer applyConstraintTo(Integer currentValue) {
return Math.max(limit + 1, currentValue);
}
@Override
public boolean isSatisfiedBy(Integer value) {
return value != null && value > limit;
}
};
}
@Test
public void ifThereIsNoConstraintsTheOriginalValueIsReturned() {
assertThat(Constraint.apply(2), equalTo(2));
@ -129,4 +136,39 @@ public class ConstraintTest {
assertThat(constraintViolated[0], equalTo(biggerThanFive));
}
@SuppressWarnings("unchecked")
@Test
public void theApplicationCanBeDoneUsingAFluentInterface() {
assertThat(Constraint.initialValue(3)
.withConstraints(biggerThanFive)
.apply(),
equalTo(6));
}
@SuppressWarnings("unchecked")
@Test
public void severalConstraintsCanBeChained() {
assertThat(Constraint.initialValue(3)
.withConstraints(biggerThanFive)
.withConstraints(biggerThanSeven)
.apply(),
equalTo(8));
}
@SuppressWarnings("unchecked")
@Test
public void chainingSeveralConstrainsShowsThePriority() {
assertThat(Constraint.initialValue(5)
.withConstraints(biggerThanFive)
.withConstraints(lessThanFive)
.apply(),
equalTo(4));
assertThat(Constraint.initialValue(5)
.withConstraints(lessThanFive)
.withConstraints(biggerThanFive)
.apply(),
equalTo(6));
}
}