Coverage report

  %line %branch
org.apache.commons.validator.routines.CurrencyValidator
100% 
100% 

 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: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
 43  
  * @since Validator 1.3.0
 44  
  */
 45  
 public class CurrencyValidator extends BigDecimalValidator {
 46  
 
 47  1
     private static final CurrencyValidator VALIDATOR = new CurrencyValidator();
 48  
 
 49  
     /** DecimalFormat's currency symbol */
 50  
     private static final char CURRENCY_SYMBOL = '\u00A4';
 51  
 
 52  
     /**
 53  
      * Return a singleton instance of this validator.
 54  
      * @return A singleton instance of the CurrencyValidator.
 55  
      */
 56  
     public static BigDecimalValidator getInstance() {
 57  5
         return VALIDATOR;
 58  
     }
 59  
 
 60  
     /**
 61  
      * Construct a <i>strict</i> instance.
 62  
      */
 63  
     public CurrencyValidator() {
 64  2
         this(true, class="keyword">true);
 65  2
     }
 66  
 
 67  
     /**
 68  
      * Construct an instance with the specified strict setting.
 69  
      * 
 70  
      * @param strict <code>true</code> if strict 
 71  
      *        <code>Format</code> parsing should be used.
 72  
      * @param allowFractions <code>true</code> if fractions are
 73  
      *        allowed or <code>false</code> if integers only.
 74  
      */
 75  
     public CurrencyValidator(boolean strict, class="keyword">boolean allowFractions) {
 76  3
         super(strict, CURRENCY_FORMAT, allowFractions);
 77  3
     }
 78  
 
 79  
     /**
 80  
      * <p>Parse the value with the specified <code>Format</code>.</p>
 81  
      * 
 82  
      * <p>This implementation is lenient whether the currency symbol
 83  
      *    is present or not. The default <code>NumberFormat</code>
 84  
      *    behaviour is for the parsing to "fail" if the currency
 85  
      *    symbol is missing. This method re-parses with a format
 86  
      *    without the currency symbol if it fails initially.</p>
 87  
      * 
 88  
      * @param value The value to be parsed.
 89  
      * @param formatter The Format to parse the value with.
 90  
      * @return The parsed value if valid or <code>null</code> if invalid.
 91  
      */
 92  
     protected Object parse(String value, Format formatter) {
 93  
 
 94  
         // Initial parse of the value
 95  36
         Object parsedValue = super.parse(value, formatter);
 96  36
         if (parsedValue != null || !(formatter instanceof DecimalFormat)) {
 97  20
             return parsedValue;
 98  
         }
 99  
 
 100  
         // Re-parse using a pattern without the currency symbol
 101  16
         DecimalFormat decimalFormat = (DecimalFormat)formatter;
 102  16
         String pattern = decimalFormat.toPattern();
 103  16
         if (pattern.indexOf(CURRENCY_SYMBOL) >= 0) {
 104  16
             StringBuffer buffer = new StringBuffer(pattern.length());
 105  304
             for (int i = 0; i < pattern.length(); i++) {
 106  288
                 if (pattern.charAt(i) != CURRENCY_SYMBOL) {
 107  261
                     buffer.append(pattern.charAt(i));
 108  
                 }
 109  
             }
 110  16
             decimalFormat.applyPattern(buffer.toString());
 111  16
             parsedValue = super.parse(value, decimalFormat);
 112  
         }
 113  16
         return parsedValue;
 114  
     }
 115  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.