BigIntegerValidator.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.math.BigInteger;
  19. import java.text.Format;
  20. import java.util.Locale;

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

  64.     private static final long serialVersionUID = 6713144356347139988L;

  65.     private static final BigIntegerValidator VALIDATOR = new BigIntegerValidator();

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

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

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

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

  115.     /**
  116.      * Check if the value is less than or equal to a maximum.
  117.      *
  118.      * @param value The value validation is being performed on.
  119.      * @param max The maximum value.
  120.      * @return {@code true} if the value is less than
  121.      *         or equal to the maximum.
  122.      */
  123.     public boolean maxValue(final BigInteger value, final long max) {
  124.         return value.longValue() <= max;
  125.     }

  126.     /**
  127.      * Check if the value is greater than or equal to a minimum.
  128.      *
  129.      * @param value The value validation is being performed on.
  130.      * @param min The minimum value.
  131.      * @return {@code true} if the value is greater than
  132.      *         or equal to the minimum.
  133.      */
  134.     public boolean minValue(final BigInteger value, final long min) {
  135.         return value.longValue() >= min;
  136.     }

  137.     /**
  138.      * Convert the parsed value to a <code>BigInteger</code>.
  139.      *
  140.      * @param value The parsed <code>Number</code> object created.
  141.      * @param formatter The Format used to parse the value with.
  142.      * @return The parsed <code>Number</code> converted to a
  143.      *         <code>BigInteger</code>.
  144.      */
  145.     @Override
  146.     protected Object processParsedValue(final Object value, final Format formatter) {
  147.         return BigInteger.valueOf(((Number) value).longValue());
  148.     }

  149.     /**
  150.      * <p>Validate/convert a <code>BigInteger</code> using the default
  151.      *    <code>Locale</code>.
  152.      *
  153.      * @param value The value validation is being performed on.
  154.      * @return The parsed <code>BigInteger</code> if valid or {@code null}
  155.      *  if invalid.
  156.      */
  157.     public BigInteger validate(final String value) {
  158.         return (BigInteger) parse(value, (String) null, (Locale) null);
  159.     }

  160.     /**
  161.      * <p>Validate/convert a <code>BigInteger</code> using the
  162.      *    specified <code>Locale</code>.
  163.      *
  164.      * @param value The value validation is being performed on.
  165.      * @param locale The locale to use for the number format, system default if null.
  166.      * @return The parsed <code>BigInteger</code> if valid or {@code null} if invalid.
  167.      */
  168.     public BigInteger validate(final String value, final Locale locale) {
  169.         return (BigInteger) parse(value, (String) null, locale);
  170.     }

  171.     /**
  172.      * <p>Validate/convert a <code>BigInteger</code> using the
  173.      *    specified <i>pattern</i>.
  174.      *
  175.      * @param value The value validation is being performed on.
  176.      * @param pattern The pattern used to validate the value against.
  177.      * @return The parsed <code>BigInteger</code> if valid or {@code null} if invalid.
  178.      */
  179.     public BigInteger validate(final String value, final String pattern) {
  180.         return (BigInteger) parse(value, pattern, (Locale) null);
  181.     }

  182.     /**
  183.      * <p>Validate/convert a <code>BigInteger</code> using the
  184.      *    specified pattern and/ or <code>Locale</code>.
  185.      *
  186.      * @param value The value validation is being performed on.
  187.      * @param pattern The pattern used to validate the value against, or the
  188.      *        default for the <code>Locale</code> if {@code null}.
  189.      * @param locale The locale to use for the date format, system default if null.
  190.      * @return The parsed <code>BigInteger</code> if valid or {@code null} if invalid.
  191.      */
  192.     public BigInteger validate(final String value, final String pattern, final Locale locale) {
  193.         return (BigInteger) parse(value, pattern, locale);
  194.     }
  195. }