Package com.uphea.domain.constraint

Source Code of com.uphea.domain.constraint.BirthYearConstraint

package com.uphea.domain.constraint;

import jodd.datetime.JDateTime;
import jodd.typeconverter.Convert;
import jodd.vtor.ValidationConstraint;
import jodd.vtor.ValidationConstraintContext;

/**
* Validation constraint for birth year.
* Birth year must be 1900 or after and less or equal to current year.
*/
public class BirthYearConstraint implements ValidationConstraint<BirthYear> {

  /**
   * Configures constraint from annotation. Since annotation doesn't holds
   * any constraint specific values, there is nothing to do here.
   */
  @Override
  public void configure(BirthYear annotation) {
  }

  /**
   * Performs birth year validation.
   */
  @Override
  public boolean isValid(ValidationConstraintContext vcc, Object value) {
    if (value == null) {
      return true;      // null values returns true (by Vtor specification) !!!
    }
    int year = Convert.toIntValue(value, 0);
    if (year < 1900) {
      return false;
    }
    return year <= new JDateTime().getYear();
  }
}
TOP

Related Classes of com.uphea.domain.constraint.BirthYearConstraint

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.