Coverage Report - org.apache.commons.validator.routines.CurrencyValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
CurrencyValidator
100%
19/19
80%
8/10
2.5
 
 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  
 
 19  
 import java.text.DecimalFormat;
 20  
 import java.text.Format;
 21  
 
 22  
 /**
 23  
  * <p><b>Currency Validation</b> and Conversion routines (<code>java.math.BigDecimal</code>).</p>
 24  
  *
 25  
  * <p>This is one implementation of a currency validator that has the following features:</p>
 26  
  *    <ul>
 27  
  *       <li>It is <i>lenient</i> about the the presence of the <i>currency symbol</i></li>
 28  
  *       <li>It converts the currency to a <code>java.math.BigDecimal</code></li>
 29  
  *    </ul>
 30  
  *
 31  
  * <p>However any of the <i>number</i> validators can be used for <i>currency</i> validation.
 32  
  *    For example, if you wanted a <i>currency</i> validator that converts to a
 33  
  *    <code>java.lang.Integer</code> then you can simply instantiate an
 34  
  *    <code>IntegerValidator</code> with the appropriate <i>format type</i>:</p>
 35  
  *
 36  
  *    <p><code>... = new IntegerValidator(false, IntegerValidator.CURRENCY_FORMAT);</code></p>
 37  
  *
 38  
  * <p>Pick the appropriate validator, depending on the type (e.g Float, Double, Integer, Long etc)
 39  
  *    you want the currency converted to. One thing to note - only the CurrencyValidator
 40  
  *    implements <i>lenient</i> behaviour regarding the currency symbol.</p>
 41  
  *
 42  
  * @version $Revision: 1739356 $
 43  
  * @since Validator 1.3.0
 44  
  */
 45  
 public class CurrencyValidator extends BigDecimalValidator {
 46  
 
 47  
     private static final long serialVersionUID = -4201640771171486514L;
 48  
 
 49  1
     private static final CurrencyValidator VALIDATOR = new CurrencyValidator();
 50  
 
 51  
     /** DecimalFormat's currency symbol */
 52  
     private static final char CURRENCY_SYMBOL = '\u00A4';
 53  
 
 54  
     /**
 55  
      * Return a singleton instance of this validator.
 56  
      * @return A singleton instance of the CurrencyValidator.
 57  
      */
 58  
     public static BigDecimalValidator getInstance() {
 59  5
         return VALIDATOR;
 60  
     }
 61  
 
 62  
     /**
 63  
      * Construct a <i>strict</i> instance.
 64  
      */
 65  
     public CurrencyValidator() {
 66  2
         this(true, true);
 67  2
     }
 68  
 
 69  
     /**
 70  
      * Construct an instance with the specified strict setting.
 71  
      *
 72  
      * @param strict <code>true</code> if strict
 73  
      *        <code>Format</code> parsing should be used.
 74  
      * @param allowFractions <code>true</code> if fractions are
 75  
      *        allowed or <code>false</code> if integers only.
 76  
      */
 77  
     public CurrencyValidator(boolean strict, boolean allowFractions) {
 78  3
         super(strict, CURRENCY_FORMAT, allowFractions);
 79  3
     }
 80  
 
 81  
     /**
 82  
      * <p>Parse the value with the specified <code>Format</code>.</p>
 83  
      *
 84  
      * <p>This implementation is lenient whether the currency symbol
 85  
      *    is present or not. The default <code>NumberFormat</code>
 86  
      *    behaviour is for the parsing to "fail" if the currency
 87  
      *    symbol is missing. This method re-parses with a format
 88  
      *    without the currency symbol if it fails initially.</p>
 89  
      *
 90  
      * @param value The value to be parsed.
 91  
      * @param formatter The Format to parse the value with.
 92  
      * @return The parsed value if valid or <code>null</code> if invalid.
 93  
      */
 94  
     @Override
 95  
     protected Object parse(String value, Format formatter) {
 96  
 
 97  
         // Initial parse of the value
 98  36
         Object parsedValue = super.parse(value, formatter);
 99  36
         if (parsedValue != null || !(formatter instanceof DecimalFormat)) {
 100  20
             return parsedValue;
 101  
         }
 102  
 
 103  
         // Re-parse using a pattern without the currency symbol
 104  16
         DecimalFormat decimalFormat = (DecimalFormat)formatter;
 105  16
         String pattern = decimalFormat.toPattern();
 106  16
         if (pattern.indexOf(CURRENCY_SYMBOL) >= 0) {
 107  16
             StringBuilder buffer = new StringBuilder(pattern.length());
 108  304
             for (int i = 0; i < pattern.length(); i++) {
 109  288
                 if (pattern.charAt(i) != CURRENCY_SYMBOL) {
 110  261
                     buffer.append(pattern.charAt(i));
 111  
                 }
 112  
             }
 113  16
             decimalFormat.applyPattern(buffer.toString());
 114  16
             parsedValue = super.parse(value, decimalFormat);
 115  
         }
 116  16
         return parsedValue;
 117  
     }
 118  
 }