ItEr33S14CUCreacionUnidadesPlanificacion: Adding DateConstraint
This commit is contained in:
parent
977b9b3217
commit
d089a52d10
4 changed files with 162 additions and 0 deletions
|
|
@ -53,6 +53,24 @@ public abstract class Constraint<T> {
|
|||
return result;
|
||||
}
|
||||
|
||||
private static final Constraint<Object> VOID_CONSTRAINT = new Constraint<Object>() {
|
||||
|
||||
@Override
|
||||
protected Object applyConstraintTo(Object currentValue) {
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSatisfiedBy(Object value) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Constraint<T> voidConstraint() {
|
||||
return (Constraint<T>) VOID_CONSTRAINT;
|
||||
}
|
||||
|
||||
private WeakReferencedListeners<IConstraintViolationListener<T>> weakListeners = WeakReferencedListeners
|
||||
.create();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* This file is part of ###PROJECT_NAME###
|
||||
*
|
||||
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
|
||||
* Desenvolvemento Tecnolóxico de Galicia
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.zkoss.ganttz.data.constraint;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* A constraint applied to {@link Date dates} <br />
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public abstract class DateConstraint extends Constraint<Date> {
|
||||
|
||||
public static Constraint<Date> biggerOrEqualThan(Date date) {
|
||||
if (date == null) {
|
||||
return Constraint.voidConstraint();
|
||||
}
|
||||
return new BiggerOrEqualThan(date);
|
||||
}
|
||||
|
||||
private long value;
|
||||
|
||||
protected long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
protected DateConstraint(Date date) {
|
||||
this.value = asMilliseconds(date);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Date applyConstraintTo(Date currentValue) {
|
||||
return new Date(applyConstraintTo(asMilliseconds(currentValue)));
|
||||
}
|
||||
|
||||
private Long asMilliseconds(Date date) {
|
||||
return date != null ? date.getTime() : null;
|
||||
}
|
||||
|
||||
protected abstract long applyConstraintTo(Long time);
|
||||
|
||||
@Override
|
||||
public boolean isSatisfiedBy(Date value) {
|
||||
return isSatisfiedBy(asMilliseconds(value));
|
||||
}
|
||||
|
||||
protected abstract boolean isSatisfiedBy(Long time);
|
||||
|
||||
static class BiggerOrEqualThan extends DateConstraint {
|
||||
|
||||
protected BiggerOrEqualThan(Date date) {
|
||||
super(date);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long applyConstraintTo(Long time) {
|
||||
if (time == null) {
|
||||
return getValue();
|
||||
}
|
||||
return Math.max(getValue(), time);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSatisfiedBy(Long time) {
|
||||
return time >= getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -112,6 +112,7 @@ public class ConstraintTest {
|
|||
assertThat(result, equalTo(4));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void theViolatedConstraintsNotifiesItsListeners() {
|
||||
final Constraint<Integer>[] constraintViolated = new Constraint[1];
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* This file is part of ###PROJECT_NAME###
|
||||
*
|
||||
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
|
||||
* Desenvolvemento Tecnolóxico de Galicia
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.zkoss.ganttz.data.constraint;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateConstraintTest {
|
||||
|
||||
private Date now = new Date();
|
||||
|
||||
private Constraint<Date> biggerOrEqualThanNow = DateConstraint
|
||||
.biggerOrEqualThan(now);
|
||||
|
||||
@Test
|
||||
public void canCreateConstraintBiggerThan() {
|
||||
assertThat(biggerOrEqualThanNow.applyTo(now), equalTo(new Date(now
|
||||
.getTime())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void biggerOrEqualThanNullLeaveValuesUnmodified() {
|
||||
Constraint<Date> biggerThanNull = DateConstraint.biggerOrEqualThan(null);
|
||||
Date eraStart = new Date(0);
|
||||
assertThat(biggerThanNull.applyConstraintTo(new Date(0)),
|
||||
equalTo(eraStart));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyingBiggerOrEqualThanConstraintToNullNotFails() {
|
||||
assertThat(biggerOrEqualThanNow.applyTo(null), equalTo(new Date(now
|
||||
.getTime())));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue