LongValidator.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.Format;
  19. import java.util.Locale;

  20. /**
  21.  * <p><b>Long Validation</b> and Conversion routines (<code>java.lang.Long</code>).</p>
  22.  *
  23.  * <p>This validator provides a number of methods for
  24.  *    validating/converting a <code>String</code> value to
  25.  *    a <code>Long</code> using <code>java.text.NumberFormat</code>
  26.  *    to parse either:</p>
  27.  *    <ul>
  28.  *       <li>using the default format for the default <code>Locale</code></li>
  29.  *       <li>using a specified pattern with the default <code>Locale</code></li>
  30.  *       <li>using the default format for a specified <code>Locale</code></li>
  31.  *       <li>using a specified pattern with a specified <code>Locale</code></li>
  32.  *    </ul>
  33.  *
  34.  * <p>Use one of the <code>isValid()</code> methods to just validate or
  35.  *    one of the <code>validate()</code> methods to validate and receive a
  36.  *    <i>converted</i> <code>Long</code> value.</p>
  37.  *
  38.  * <p>Once a value has been successfully converted the following
  39.  *    methods can be used to perform minimum, maximum and range checks:</p>
  40.  *    <ul>
  41.  *       <li><code>minValue()</code> checks whether the value is greater
  42.  *           than or equal to a specified minimum.</li>
  43.  *       <li><code>maxValue()</code> checks whether the value is less
  44.  *           than or equal to a specified maximum.</li>
  45.  *       <li><code>isInRange()</code> checks whether the value is within
  46.  *           a specified range of values.</li>
  47.  *    </ul>
  48.  *
  49.  * <p>So that the same mechanism used for parsing an <i>input</i> value
  50.  *    for validation can be used to format <i>output</i>, corresponding
  51.  *    <code>format()</code> methods are also provided. That is you can
  52.  *    format either:</p>
  53.  *    <ul>
  54.  *       <li>using a specified pattern</li>
  55.  *       <li>using the format for a specified <code>Locale</code></li>
  56.  *       <li>using the format for the <i>default</i> <code>Locale</code></li>
  57.  *    </ul>
  58.  *
  59.  * @since 1.3.0
  60.  */
  61. public class LongValidator extends AbstractNumberValidator {

  62.     private static final long serialVersionUID = -5117231731027866098L;

  63.     private static final LongValidator VALIDATOR = new LongValidator();

  64.     /**
  65.      * Gets the singleton instance of this validator.
  66.      * @return A singleton instance of the LongValidator.
  67.      */
  68.     public static LongValidator getInstance() {
  69.         return VALIDATOR;
  70.     }

  71.     /**
  72.      * Constructs a <i>strict</i> instance.
  73.      */
  74.     public LongValidator() {
  75.         this(true, STANDARD_FORMAT);
  76.     }

  77.     /**
  78.      * <p>Construct an instance with the specified strict setting
  79.      *    and format type.</p>
  80.      *
  81.      * <p>The <code>formatType</code> specified what type of
  82.      *    <code>NumberFormat</code> is created - valid types
  83.      *    are:</p>
  84.      *    <ul>
  85.      *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
  86.      *           <i>standard</i> number formats (the default).</li>
  87.      *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
  88.      *           <i>currency</i> number formats.</li>
  89.      *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
  90.      *           <i>percent</i> number formats (the default).</li>
  91.      *    </ul>
  92.      *
  93.      * @param strict {@code true} if strict
  94.      *        <code>Format</code> parsing should be used.
  95.      * @param formatType The <code>NumberFormat</code> type to
  96.      *        create for validation, default is STANDARD_FORMAT.
  97.      */
  98.     public LongValidator(final boolean strict, final int formatType) {
  99.         super(strict, formatType, false);
  100.     }

  101.     /**
  102.      * Check if the value is within a specified range.
  103.      *
  104.      * @param value The <code>Number</code> value to check.
  105.      * @param min The minimum value of the range.
  106.      * @param max The maximum value of the range.
  107.      * @return {@code true} if the value is within the
  108.      *         specified range.
  109.      */
  110.     public boolean isInRange(final long value, final long min, final long max) {
  111.         return value >= min && value <= max;
  112.     }

  113.     /**
  114.      * Check if the value is within a specified range.
  115.      *
  116.      * @param value The <code>Number</code> value to check.
  117.      * @param min The minimum value of the range.
  118.      * @param max The maximum value of the range.
  119.      * @return {@code true} if the value is within the
  120.      *         specified range.
  121.      */
  122.     public boolean isInRange(final Long value, final long min, final long max) {
  123.         return isInRange(value.longValue(), min, max);
  124.     }

  125.     /**
  126.      * Check if the value is less than or equal to a maximum.
  127.      *
  128.      * @param value The value validation is being performed on.
  129.      * @param max The maximum value.
  130.      * @return {@code true} if the value is less than
  131.      *         or equal to the maximum.
  132.      */
  133.     public boolean maxValue(final long value, final long max) {
  134.         return value <= max;
  135.     }

  136.     /**
  137.      * Check if the value is less than or equal to a maximum.
  138.      *
  139.      * @param value The value validation is being performed on.
  140.      * @param max The maximum value.
  141.      * @return {@code true} if the value is less than
  142.      *         or equal to the maximum.
  143.      */
  144.     public boolean maxValue(final Long value, final long max) {
  145.         return maxValue(value.longValue(), max);
  146.     }

  147.     /**
  148.      * Check if the value is greater than or equal to a minimum.
  149.      *
  150.      * @param value The value validation is being performed on.
  151.      * @param min The minimum value.
  152.      * @return {@code true} if the value is greater than
  153.      *         or equal to the minimum.
  154.      */
  155.     public boolean minValue(final long value, final long min) {
  156.         return value >= min;
  157.     }

  158.     /**
  159.      * Check if the value is greater than or equal to a minimum.
  160.      *
  161.      * @param value The value validation is being performed on.
  162.      * @param min The minimum value.
  163.      * @return {@code true} if the value is greater than
  164.      *         or equal to the minimum.
  165.      */
  166.     public boolean minValue(final Long value, final long min) {
  167.         return minValue(value.longValue(), min);
  168.     }

  169.     /**
  170.      * Convert the parsed value to a <code>Long</code>.
  171.      *
  172.      * @param value The parsed <code>Number</code> object created.
  173.      * @param formatter The Format used to parse the value with.
  174.      * @return The parsed <code>Number</code> converted to a
  175.      *         <code>Long</code>.
  176.      */
  177.     @Override
  178.     protected Object processParsedValue(final Object value, final Format formatter) {

  179.         // Parsed value will be Long if it fits in a long and is not fractional
  180.         if (value instanceof Long) {
  181.             return value;
  182.         }
  183.         return null;

  184.     }

  185.     /**
  186.      * <p>Validate/convert a <code>Long</code> using the default
  187.      *    <code>Locale</code>.
  188.      *
  189.      * @param value The value validation is being performed on.
  190.      * @return The parsed <code>Long</code> if valid or {@code null}
  191.      *  if invalid.
  192.      */
  193.     public Long validate(final String value) {
  194.         return (Long) parse(value, (String) null, (Locale) null);
  195.     }

  196.     /**
  197.      * <p>Validate/convert a <code>Long</code> using the
  198.      *    specified <code>Locale</code>.
  199.      *
  200.      * @param value The value validation is being performed on.
  201.      * @param locale The locale to use for the number format, system default if null.
  202.      * @return The parsed <code>Long</code> if valid or {@code null} if invalid.
  203.      */
  204.     public Long validate(final String value, final Locale locale) {
  205.         return (Long) parse(value, (String) null, locale);
  206.     }

  207.     /**
  208.      * <p>Validate/convert a <code>Long</code> using the
  209.      *    specified <i>pattern</i>.
  210.      *
  211.      * @param value The value validation is being performed on.
  212.      * @param pattern The pattern used to validate the value against.
  213.      * @return The parsed <code>Long</code> if valid or {@code null} if invalid.
  214.      */
  215.     public Long validate(final String value, final String pattern) {
  216.         return (Long) parse(value, pattern, (Locale) null);
  217.     }

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