    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.validator.routines;
    018
    019import java.text.DateFormat;
    020import java.text.Format;
    021import java.util.Calendar;
    022import java.util.Locale;
    023import java.util.TimeZone;
    024
    025/**
    026 * <p><b>Time Validation</b> and Conversion routines (<code>java.util.Calendar</code>).</p>
    027 *
    028 * <p>This validator provides a number of methods for validating/converting
    029 *    a <code>String</code> time value to a <code>java.util.Calendar</code> using
    030 *    <code>java.text.DateFormat</code> to parse either:</p>
    031 *    <ul>
    032 *       <li>using the default format for the default <code>Locale</code></li>
    033 *       <li>using a specified pattern with the default <code>Locale</code></li>
    034 *       <li>using the default format for a specified <code>Locale</code></li>
    035 *       <li>using a specified pattern with a specified <code>Locale</code></li>
    036 *    </ul>
    037 *
    038 * <p>For each of the above mechanisms, conversion method (i.e the
    039 *    <code>validate</code> methods) implementations are provided which
    040 *    either use the default <code>TimeZone</code> or allow the
    041 *    <code>TimeZone</code> to be specified.</p>
    042 *
    043 * <p>Use one of the <code>isValid()</code> methods to just validate or
    044 *    one of the <code>validate()</code> methods to validate and receive a
    045 *    <i>converted</i> <code>Calendar</code> value for the time.</p>
    046 *
    047 * <p>Implementations of the <code>validate()</code> method are provided
    048 *    to create <code>Calendar</code> objects for different <i>time zones</i>
    049 *    if the system default is not appropriate.</p>
    050 *
    051 * <p>Alternatively the CalendarValidator's <code>adjustToTimeZone()</code> method
    052 *    can be used to adjust the <code>TimeZone</code> of the <code>Calendar</code>
    053 *    object afterwards.</p>
    054 *
    055 * <p>Once a value has been successfully converted the following
    056 *    methods can be used to perform various time comparison checks:</p>
    057 *    <ul>
    058 *       <li><code>compareTime()</code> compares the hours, minutes, seconds
    059 *           and milliseconds of two calendars, returning 0, -1 or +1 indicating
    060 *           whether the first time is equal, before or after the second.</li>
    061 *       <li><code>compareSeconds()</code> compares the hours, minutes and
    062 *           seconds of two times, returning 0, -1 or +1 indicating
    063 *           whether the first is equal to, before or after the second.</li>
    064 *       <li><code>compareMinutes()</code> compares the hours and minutes
    065 *           two times, returning 0, -1 or +1 indicating
    066 *           whether the first is equal to, before or after the second.</li>
    067 *       <li><code>compareHours()</code> compares the hours
    068 *           of two times, returning 0, -1 or +1 indicating
    069 *           whether the first is equal to, before or after the second.</li>
    070 *    </ul>
    071 *
    072 * <p>So that the same mechanism used for parsing an <i>input</i> value
    073 *    for validation can be used to format <i>output</i>, corresponding
    074 *    <code>format()</code> methods are also provided. That is you can
    075 *    format either:</p>
    076 *    <ul>
    077 *       <li>using a specified pattern</li>
    078 *       <li>using the format for a specified <code>Locale</code></li>
    079 *       <li>using the format for the <i>default</i> <code>Locale</code></li>
    080 *    </ul>
    081 *
    082 * @since 1.3.0
    083 */
    084public class TimeValidator extends AbstractCalendarValidator {
    085
    086    private static final long serialVersionUID = 3494007492269691581L;
    087
    088    private static final TimeValidator VALIDATOR = new TimeValidator();
    089
    090    /**
    091     * Gets the singleton instance of this validator.
    092     * @return A singleton instance of the TimeValidator.
    093     */
    094    public static TimeValidator getInstance() {
    095        return VALIDATOR;
    096    }
    097
    098    /**
    099     * Constructs a <i>strict</i> instance with <i>short</i>
    100     * time style.
    101     */
    102    public TimeValidator() {
    103        this(true, DateFormat.SHORT);
    104    }
    105
    106    /**
    107     * Constructs an instance with the specified <i>strict</i>
    108     * and <i>time style</i> parameters.
    109     *
    110     * @param strict {@code true} if strict
    111     *        <code>Format</code> parsing should be used.
    112     * @param timeStyle the time style to use for Locale validation.
    113     */
    114    public TimeValidator(final boolean strict, final int timeStyle) {
    115        super(strict, -1, timeStyle);
    116    }
    117
    118    /**
    119     * <p>Compare Hours.</p>
    120     *
    121     * @param value The <code>Calendar</code> value to check.
    122     * @param compare The <code>Calendar</code> to compare the value to.
    123     * @return Zero if the hours are equal, -1 if first
    124     * parameter's hour is less than the seconds and +1 if the first
    125     * parameter's hour is greater than.
    126     */
    127    public int compareHours(final Calendar value, final Calendar compare) {
    128        return compareTime(value, compare, Calendar.HOUR_OF_DAY);
    129    }
    130
    131    /**
    132     * <p>Compare Minutes (hours and minutes).</p>
    133     *
    134     * @param value The <code>Calendar</code> value to check.
    135     * @param compare The <code>Calendar</code> to compare the value to.
    136     * @return Zero if the hours are equal, -1 if first
    137     * parameter's minutes are less than the seconds and +1 if the first
    138     * parameter's minutes are greater than.
    139     */
    140    public int compareMinutes(final Calendar value, final Calendar compare) {
    141        return compareTime(value, compare, Calendar.MINUTE);
    142    }
    143
    144    /**
    145     * <p>Compare Seconds (hours, minutes and seconds).</p>
    146     *
    147     * @param value The <code>Calendar</code> value to check.
    148     * @param compare The <code>Calendar</code> to compare the value to.
    149     * @return Zero if the hours are equal, -1 if first
    150     * parameter's seconds are less than the seconds and +1 if the first
    151     * parameter's seconds are greater than.
    152     */
    153    public int compareSeconds(final Calendar value, final Calendar compare) {
    154        return compareTime(value, compare, Calendar.SECOND);
    155    }
    156
    157    /**
    158     * <p>Compare Times (hour, minute, second and millisecond - not date).</p>
    159     *
    160     * @param value The <code>Calendar</code> value to check.
    161     * @param compare The <code>Calendar</code> to compare the value to.
    162     * @return Zero if the hours are equal, -1 if first
    163     * time is less than the seconds and +1 if the first
    164     * time is greater than.
    165     */
    166    public int compareTime(final Calendar value, final Calendar compare) {
    167        return compareTime(value, compare, Calendar.MILLISECOND);
    168    }
    169
    170    /**
    171     * <p>Convert the parsed <code>Date</code> to a <code>Calendar</code>.</p>
    172     *
    173     * @param value The parsed <code>Date</code> object created.
    174     * @param formatter The Format used to parse the value with.
    175     * @return The parsed value converted to a <code>Calendar</code>.
    176     */
    177    @Override
    178    protected Object processParsedValue(final Object value, final Format formatter) {
    179        return ((DateFormat) formatter).getCalendar();
    180    }
    181
    182    /**
    183     * <p>Validate/convert a time using the default <code>Locale</code>
    184     *    and <code>TimeZone</code>.
    185     *
    186     * @param value The value validation is being performed on.
    187     * @return The parsed <code>Calendar</code> if valid or {@code null}
    188     *  if invalid.
    189     */
    190    public Calendar validate(final String value) {
    191        return (Calendar) parse(value, (String) null, (Locale) null, (TimeZone) null);
    192    }
    193
    194    /**
    195     * <p>Validate/convert a time using the specified <code>Locale</code>
    196     *    default <code>TimeZone</code>.
    197     *
    198     * @param value The value validation is being performed on.
    199     * @param locale The locale to use for the time format, system default if null.
    200     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    201     */
    202    public Calendar validate(final String value, final Locale locale) {
    203        return (Calendar) parse(value, (String) null, locale, (TimeZone) null);
    204    }
    205
    206    /**
    207     * <p>Validate/convert a time using the specified <code>Locale</code>
    208     *    and <code>TimeZone</code>.
    209     *
    210     * @param value The value validation is being performed on.
    211     * @param locale The locale to use for the time format, system default if null.
    212     * @param timeZone The Time Zone used to parse the time, system default if null.
    213     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    214     */
    215    public Calendar validate(final String value, final Locale locale, final TimeZone timeZone) {
    216        return (Calendar) parse(value, (String) null, locale, timeZone);
    217    }
    218
    219    /**
    220     * <p>Validate/convert a time using the specified <i>pattern</i> and
    221     *    default <code>TimeZone</code>.
    222     *
    223     * @param value The value validation is being performed on.
    224     * @param pattern The pattern used to validate the value against.
    225     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    226     */
    227    public Calendar validate(final String value, final String pattern) {
    228        return (Calendar) parse(value, pattern, (Locale) null, (TimeZone) null);
    229    }
    230
    231    /**
    232     * <p>Validate/convert a time using the specified pattern and <code>Locale</code>
    233     *    and the default <code>TimeZone</code>.
    234     *
    235     * @param value The value validation is being performed on.
    236     * @param pattern The pattern used to validate the value against, or the
    237     *        default for the <code>Locale</code> if {@code null}.
    238     * @param locale The locale to use for the date format, system default if null.
    239     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    240     */
    241    public Calendar validate(final String value, final String pattern, final Locale locale) {
    242        return (Calendar) parse(value, pattern, locale, (TimeZone) null);
    243    }
    244
    245    /**
    246     * <p>Validate/convert a time using the specified pattern, <code>Locale</code>
    247     *    and <code>TimeZone</code>.
    248     *
    249     * @param value The value validation is being performed on.
    250     * @param pattern The pattern used to validate the value against, or the
    251     *        default for the <code>Locale</code> if {@code null}.
    252     * @param locale The locale to use for the date format, system default if null.
    253     * @param timeZone The Time Zone used to parse the date, system default if null.
    254     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    255     */
    256    public Calendar validate(final String value, final String pattern, final Locale locale, final TimeZone timeZone) {
    257        return (Calendar) parse(value, pattern, locale, timeZone);
    258    }
    259
    260    /**
    261     * <p>Validate/convert a time using the specified <i>pattern</i>
    262     *    and <code>TimeZone</code>.
    263     *
    264     * @param value The value validation is being performed on.
    265     * @param pattern The pattern used to validate the value against.
    266     * @param timeZone The Time Zone used to parse the time, system default if null.
    267     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    268     */
    269    public Calendar validate(final String value, final String pattern, final TimeZone timeZone) {
    270        return (Calendar) parse(value, pattern, (Locale) null, timeZone);
    271    }
    272
    273    /**
    274     * <p>Validate/convert a time using the specified <code>TimeZone</code>
    275     *    and default <code>Locale</code>.
    276     *
    277     * @param value The value validation is being performed on.
    278     * @param timeZone The Time Zone used to parse the time, system default if null.
    279     * @return The parsed <code>Calendar</code> if valid or {@code null} if invalid.
    280     */
    281    public Calendar validate(final String value, final TimeZone timeZone) {
    282        return (Calendar) parse(value, (String) null, (Locale) null, timeZone);
    283    }
    284}