TimeValidator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.validator.routines;

  18. import java.text.DateFormat;
  19. import java.text.Format;
  20. import java.util.Calendar;
  21. import java.util.Locale;
  22. import java.util.TimeZone;

  23. /**
  24.  * <p><b>Time Validation</b> and Conversion routines (<code>java.util.Calendar</code>).</p>
  25.  *
  26.  * <p>This validator provides a number of methods for validating/converting
  27.  *    a <code>String</code> time value to a <code>java.util.Calendar</code> using
  28.  *    <code>java.text.DateFormat</code> to parse either:</p>
  29.  *    <ul>
  30.  *       <li>using the default format for the default <code>Locale</code></li>
  31.  *       <li>using a specified pattern with the default <code>Locale</code></li>
  32.  *       <li>using the default format for a specified <code>Locale</code></li>
  33.  *       <li>using a specified pattern with a specified <code>Locale</code></li>
  34.  *    </ul>
  35.  *
  36.  * <p>For each of the above mechanisms, conversion method (i.e the
  37.  *    <code>validate</code> methods) implementations are provided which
  38.  *    either use the default <code>TimeZone</code> or allow the
  39.  *    <code>TimeZone</code> to be specified.</p>
  40.  *
  41.  * <p>Use one of the <code>isValid()</code> methods to just validate or
  42.  *    one of the <code>validate()</code> methods to validate and receive a
  43.  *    <i>converted</i> <code>Calendar</code> value for the time.</p>
  44.  *
  45.  * <p>Implementations of the <code>validate()</code> method are provided
  46.  *    to create <code>Calendar</code> objects for different <i>time zones</i>
  47.  *    if the system default is not appropriate.</p>
  48.  *
  49.  * <p>Alternatively the CalendarValidator's <code>adjustToTimeZone()</code> method
  50.  *    can be used to adjust the <code>TimeZone</code> of the <code>Calendar</code>
  51.  *    object afterwards.</p>
  52.  *
  53.  * <p>Once a value has been successfully converted the following
  54.  *    methods can be used to perform various time comparison checks:</p>
  55.  *    <ul>
  56.  *       <li><code>compareTime()</code> compares the hours, minutes, seconds
  57.  *           and milliseconds of two calendars, returning 0, -1 or +1 indicating
  58.  *           whether the first time is equal, before or after the second.</li>
  59.  *       <li><code>compareSeconds()</code> compares the hours, minutes and
  60.  *           seconds of two times, returning 0, -1 or +1 indicating
  61.  *           whether the first is equal to, before or after the second.</li>
  62.  *       <li><code>compareMinutes()</code> compares the hours and minutes
  63.  *           two times, returning 0, -1 or +1 indicating
  64.  *           whether the first is equal to, before or after the second.</li>
  65.  *       <li><code>compareHours()</code> compares the hours
  66.  *           of two times, returning 0, -1 or +1 indicating
  67.  *           whether the first is equal to, before or after the second.</li>
  68.  *    </ul>
  69.  *
  70.  * <p>So that the same mechanism used for parsing an <i>input</i> value
  71.  *    for validation can be used to format <i>output</i>, corresponding
  72.  *    <code>format()</code> methods are also provided. That is you can
  73.  *    format either:</p>
  74.  *    <ul>
  75.  *       <li>using a specified pattern</li>
  76.  *       <li>using the format for a specified <code>Locale</code></li>
  77.  *       <li>using the format for the <i>default</i> <code>Locale</code></li>
  78.  *    </ul>
  79.  *
  80.  * @since 1.3.0
  81.  */
  82. public class TimeValidator extends AbstractCalendarValidator {

  83.     private static final long serialVersionUID = 3494007492269691581L;

  84.     private static final TimeValidator VALIDATOR = new TimeValidator();

  85.     /**
  86.      * Gets the singleton instance of this validator.
  87.      * @return A singleton instance of the TimeValidator.
  88.      */
  89.     public static TimeValidator getInstance() {
  90.         return VALIDATOR;
  91.     }

  92.     /**
  93.      * Constructs a <i>strict</i> instance with <i>short</i>
  94.      * time style.
  95.      */
  96.     public TimeValidator() {
  97.         this(true, DateFormat.SHORT);
  98.     }

  99.     /**
  100.      * Constructs an instance with the specified <i>strict</i>
  101.      * and <i>time style</i> parameters.
  102.      *
  103.      * @param strict {@code true} if strict
  104.      *        <code>Format</code> parsing should be used.
  105.      * @param timeStyle the time style to use for Locale validation.
  106.      */
  107.     public TimeValidator(final boolean strict, final int timeStyle) {
  108.         super(strict, -1, timeStyle);
  109.     }

  110.     /**
  111.      * <p>Compare Hours.</p>
  112.      *
  113.      * @param value The <code>Calendar</code> value to check.
  114.      * @param compare The <code>Calendar</code> to compare the value to.
  115.      * @return Zero if the hours are equal, -1 if first
  116.      * parameter's hour is less than the seconds and +1 if the first
  117.      * parameter's hour is greater than.
  118.      */
  119.     public int compareHours(final Calendar value, final Calendar compare) {
  120.         return compareTime(value, compare, Calendar.HOUR_OF_DAY);
  121.     }

  122.     /**
  123.      * <p>Compare Minutes (hours and minutes).</p>
  124.      *
  125.      * @param value The <code>Calendar</code> value to check.
  126.      * @param compare The <code>Calendar</code> to compare the value to.
  127.      * @return Zero if the hours are equal, -1 if first
  128.      * parameter's minutes are less than the seconds and +1 if the first
  129.      * parameter's minutes are greater than.
  130.      */
  131.     public int compareMinutes(final Calendar value, final Calendar compare) {
  132.         return compareTime(value, compare, Calendar.MINUTE);
  133.     }

  134.     /**
  135.      * <p>Compare Seconds (hours, minutes and seconds).</p>
  136.      *
  137.      * @param value The <code>Calendar</code> value to check.
  138.      * @param compare The <code>Calendar</code> to compare the value to.
  139.      * @return Zero if the hours are equal, -1 if first
  140.      * parameter's seconds are less than the seconds and +1 if the first
  141.      * parameter's seconds are greater than.
  142.      */
  143.     public int compareSeconds(final Calendar value, final Calendar compare) {
  144.         return compareTime(value, compare, Calendar.SECOND);
  145.     }

  146.     /**
  147.      * <p>Compare Times (hour, minute, second and millisecond - not date).</p>
  148.      *
  149.      * @param value The <code>Calendar</code> value to check.
  150.      * @param compare The <code>Calendar</code> to compare the value to.
  151.      * @return Zero if the hours are equal, -1 if first
  152.      * time is less than the seconds and +1 if the first
  153.      * time is greater than.
  154.      */
  155.     public int compareTime(final Calendar value, final Calendar compare) {
  156.         return compareTime(value, compare, Calendar.MILLISECOND);
  157.     }

  158.     /**
  159.      * <p>Convert the parsed <code>Date</code> to a <code>Calendar</code>.</p>
  160.      *
  161.      * @param value The parsed <code>Date</code> object created.
  162.      * @param formatter The Format used to parse the value with.
  163.      * @return The parsed value converted to a <code>Calendar</code>.
  164.      */
  165.     @Override
  166.     protected Object processParsedValue(final Object value, final Format formatter) {
  167.         return ((DateFormat) formatter).getCalendar();
  168.     }

  169.     /**
  170.      * <p>Validate/convert a time using the default <code>Locale</code>
  171.      *    and <code>TimeZone</code>.
  172.      *
  173.      * @param value The value validation is being performed on.
  174.      * @return The parsed <code>Calendar</code> if valid or {@code null}
  175.      *  if invalid.
  176.      */
  177.     public Calendar validate(final String value) {
  178.         return (Calendar) parse(value, (String) null, (Locale) null, (TimeZone) null);
  179.     }

  180.     /**
  181.      * <p>Validate/convert a time using the specified <code>Locale</code>
  182.      *    default <code>TimeZone</code>.
  183.      *
  184.      * @param value The value validation is being performed on.
  185.      * @param locale The locale to use for the time format, system default if null.
  186.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  187.      */
  188.     public Calendar validate(final String value, final Locale locale) {
  189.         return (Calendar) parse(value, (String) null, locale, (TimeZone) null);
  190.     }

  191.     /**
  192.      * <p>Validate/convert a time using the specified <code>Locale</code>
  193.      *    and <code>TimeZone</code>.
  194.      *
  195.      * @param value The value validation is being performed on.
  196.      * @param locale The locale to use for the time format, system default if null.
  197.      * @param timeZone The Time Zone used to parse the time, system default if null.
  198.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  199.      */
  200.     public Calendar validate(final String value, final Locale locale, final TimeZone timeZone) {
  201.         return (Calendar) parse(value, (String) null, locale, timeZone);
  202.     }

  203.     /**
  204.      * <p>Validate/convert a time using the specified <i>pattern</i> and
  205.      *    default <code>TimeZone</code>.
  206.      *
  207.      * @param value The value validation is being performed on.
  208.      * @param pattern The pattern used to validate the value against.
  209.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  210.      */
  211.     public Calendar validate(final String value, final String pattern) {
  212.         return (Calendar) parse(value, pattern, (Locale) null, (TimeZone) null);
  213.     }

  214.     /**
  215.      * <p>Validate/convert a time using the specified pattern and <code>Locale</code>
  216.      *    and the default <code>TimeZone</code>.
  217.      *
  218.      * @param value The value validation is being performed on.
  219.      * @param pattern The pattern used to validate the value against, or the
  220.      *        default for the <code>Locale</code> if {@code null}.
  221.      * @param locale The locale to use for the date format, system default if null.
  222.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  223.      */
  224.     public Calendar validate(final String value, final String pattern, final Locale locale) {
  225.         return (Calendar) parse(value, pattern, locale, (TimeZone) null);
  226.     }

  227.     /**
  228.      * <p>Validate/convert a time using the specified pattern, <code>Locale</code>
  229.      *    and <code>TimeZone</code>.
  230.      *
  231.      * @param value The value validation is being performed on.
  232.      * @param pattern The pattern used to validate the value against, or the
  233.      *        default for the <code>Locale</code> if {@code null}.
  234.      * @param locale The locale to use for the date format, system default if null.
  235.      * @param timeZone The Time Zone used to parse the date, system default if null.
  236.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  237.      */
  238.     public Calendar validate(final String value, final String pattern, final Locale locale, final TimeZone timeZone) {
  239.         return (Calendar) parse(value, pattern, locale, timeZone);
  240.     }

  241.     /**
  242.      * <p>Validate/convert a time using the specified <i>pattern</i>
  243.      *    and <code>TimeZone</code>.
  244.      *
  245.      * @param value The value validation is being performed on.
  246.      * @param pattern The pattern used to validate the value against.
  247.      * @param timeZone The Time Zone used to parse the time, system default if null.
  248.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  249.      */
  250.     public Calendar validate(final String value, final String pattern, final TimeZone timeZone) {
  251.         return (Calendar) parse(value, pattern, (Locale) null, timeZone);
  252.     }

  253.     /**
  254.      * <p>Validate/convert a time using the specified <code>TimeZone</code>
  255.      *    and default <code>Locale</code>.
  256.      *
  257.      * @param value The value validation is being performed on.
  258.      * @param timeZone The Time Zone used to parse the time, system default if null.
  259.      * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
  260.      */
  261.     public Calendar validate(final String value, final TimeZone timeZone) {
  262.         return (Calendar) parse(value, (String) null, (Locale) null, timeZone);
  263.     }
  264. }