org.holmes.evaluator.DateEvaluator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of holmes-validation Show documentation
Show all versions of holmes-validation Show documentation
Holmes is a library that provides a simple and fluent API for writing business rules validations on Java projects.
The newest version!
package org.holmes.evaluator;
import java.util.Date;
import org.holmes.Evaluator;
import org.holmes.Joint;
import org.holmes.evaluator.support.Diff;
import org.holmes.evaluator.support.FutureNumber;
import org.holmes.evaluator.support.Interval;
/**
* An {@link Evaluator} for the {@link Date} type.
*
* @author diegossilveira
*/
public class DateEvaluator extends ObjectEvaluator {
private static final int ZERO = 0;
public DateEvaluator(Date target) {
super(target);
}
/**
* Applies a diff between the target and a past or future date.
*
* @param diff
* The diff configuration.
* @return {@link NumberEvaluator}
*/
public NumberEvaluator applying(final Diff diff) {
final FutureNumber futureNumber = new FutureNumber();
final NumberEvaluator evaluator = new NumberEvaluator(futureNumber);
setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
diff.setTarget(target);
futureNumber.wrap(diff.calculate());
return evaluator.evaluate();
}
});
evaluator.setJoint(getJoint());
return evaluator;
}
/**
* Ensures that the target is a date in the past.
*
* @return an instance of {@link Joint} class
*/
public Joint isPast() {
return isBeforeThan(new Date());
}
/**
* Ensures that the target is a date in the future.
*
* @return an instance of {@link Joint} class
*/
public Joint isFuture() {
return isAfterThan(new Date());
}
/**
* Ensures that the target is after than the argument date.
*
* @param date
* the date to compare the target to
* @return an instance of {@link Joint} class
*/
public Joint isAfterThan(final Date date) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return target.compareTo(date) > ZERO;
}
}).getJoint();
}
/**
* Ensures that the target is after than or equal to the argument date.
*
* @param date
* the date to compare the target to
* @return an instance of {@link Joint} class
*/
public Joint isAfterThanOrEqualTo(final Date date) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return target.compareTo(date) >= ZERO;
}
}).getJoint();
}
/**
* Ensures that the target is before than the argument date.
*
* @param date
* the date to compare the target to
* @return an instance of {@link Joint} class
*/
public Joint isBeforeThan(final Date date) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return target.compareTo(date) < ZERO;
}
}).getJoint();
}
/**
* Ensures that the target is before than or equal to the argument date.
*
* @param date
* the date to compare the target to
* @return an instance of {@link Joint} class
*/
public Joint isBeforeThanOrEqualTo(final Date date) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return target.compareTo(date) <= ZERO;
}
}).getJoint();
}
/**
* Ensures that the target belongs to the closed interval [leftBoundary,
* rightBoundary].
*
* @param leftBoundary
* the left boundary, inclusive.
* @param rightBoundary
* the right boundary, inclusive.
* @return an instance of {@link Joint} class
*/
public Joint belongsToInterval(final Date leftBoundary, final Date rightBoundary) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return Interval.closedInterval(leftBoundary, rightBoundary).contains(target);
}
}).getJoint();
}
/**
* Ensures that the target belongs to the left-open interval (leftBoundary,
* rightBoundary].
*
* @param leftBoundary
* the left boundary, exclusive.
* @param rightBoundary
* the right boundary, inclusive.
* @return an instance of {@link Joint} class
*/
public Joint belongsToLeftOpenInterval(final Date leftBoundary, final Date rightBoundary) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return Interval.leftOpenInterval(leftBoundary, rightBoundary).contains(target);
}
}).getJoint();
}
/**
* Ensures that the target belongs to the right-open interval [leftBoundary,
* rightBoundary).
*
* @param leftBoundary
* the left boundary, inclusive.
* @param rightBoundary
* the right boundary, exclusive.
* @return an instance of {@link Joint} class
*/
public Joint belongsToRightOpenInterval(final Date leftBoundary, final Date rightBoundary) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return Interval.rightOpenInterval(leftBoundary, rightBoundary).contains(target);
}
}).getJoint();
}
/**
* Ensures that the target belongs to the open interval (leftBoundary,
* rightBoundary).
*
* @param leftBoundary
* the left boundary, exclusive.
* @param rightBoundary
* the right boundary, exclusive.
* @return an instance of {@link Joint} class
*/
public Joint belongsToOpenInterval(final Date leftBoundary, final Date rightBoundary) {
return setEvaluation(new Evaluation() {
public boolean evaluate(Date target) {
return Interval.openInterval(leftBoundary, rightBoundary).contains(target);
}
}).getJoint();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy