Coverage report

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

 1  
 /*
 2  
  * $Id: FloatValidator.java 386637 2006-03-17 13:22:26Z niallp $
 3  
  * $Revision: 386637 $
 4  
  * $Date: 2006-03-17 13:22:26 +0000 (Fri, 17 Mar 2006) $
 5  
  *
 6  
  * ====================================================================
 7  
  * Copyright 2006 The Apache Software Foundation
 8  
  *
 9  
  * Licensed under the Apache License, Version 2.0 (the "License");
 10  
  * you may not use this file except in compliance with the License.
 11  
  * You may obtain a copy of the License at
 12  
  *
 13  
  *     http://www.apache.org/licenses/LICENSE-2.0
 14  
  *
 15  
  * Unless required by applicable law or agreed to in writing, software
 16  
  * distributed under the License is distributed on an "AS IS" BASIS,
 17  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18  
  * See the License for the specific language governing permissions and
 19  
  * limitations under the License.
 20  
  */
 21  
 package org.apache.commons.validator.routines;
 22  
 
 23  
 import java.text.Format;
 24  
 import java.util.Locale;
 25  
 
 26  
 /**
 27  
  * <p><b>Float Validation</b> and Conversion routines (<code>java.lang.Float</code>).</p>
 28  
  *
 29  
  * <p>This validator provides a number of methods for
 30  
  *    validating/converting a <code>String</code> value to
 31  
  *    a <code>Float</code> using <code>java.text.NumberFormat</code>
 32  
  *    to parse either:</p>
 33  
  *    <ul>
 34  
  *       <li>using the default format for the default <code>Locale</code></li>
 35  
  *       <li>using a specified pattern with the default <code>Locale</code></li>
 36  
  *       <li>using the default format for a specified <code>Locale</code></li>
 37  
  *       <li>using a specified pattern with a specified <code>Locale</code></li>
 38  
  *    </ul>
 39  
  *    
 40  
  * <p>Use one of the <code>isValid()</code> methods to just validate or
 41  
  *    one of the <code>validate()</code> methods to validate and receive a
 42  
  *    <i>converted</i> <code>Float</code> value.</p>
 43  
  * 
 44  
  * <p>Once a value has been sucessfully converted the following
 45  
  *    methods can be used to perform minimum, maximum and range checks:</p>
 46  
  *    <ul>
 47  
  *       <li><code>minValue()</code> checks whether the value is greater
 48  
  *           than or equal to a specified minimum.</li>
 49  
  *       <li><code>maxValue()</code> checks whether the value is less
 50  
  *           than or equal to a specified maximum.</li>
 51  
  *       <li><code>isInRange()</code> checks whether the value is within
 52  
  *           a specified range of values.</li>
 53  
  *    </ul>
 54  
  * 
 55  
  * <p>So that the same mechanism used for parsing an <i>input</i> value 
 56  
  *    for validation can be used to format <i>output</i>, corresponding
 57  
  *    <code>format()</code> methods are also provided. That is you can 
 58  
  *    format either:</p>
 59  
  *    <ul>
 60  
  *       <li>using the default format for the default <code>Locale</code></li>
 61  
  *       <li>using a specified pattern with the default <code>Locale</code></li>
 62  
  *       <li>using the default format for a specified <code>Locale</code></li>
 63  
  *       <li>using a specified pattern with a specified <code>Locale</code></li>
 64  
  *    </ul>
 65  
  *
 66  
  * @version $Revision: 386637 $ $Date: 2006-03-17 13:22:26 +0000 (Fri, 17 Mar 2006) $
 67  
  * @since Validator 1.3.0
 68  
  */
 69  
 public class FloatValidator extends AbstractNumberValidator {
 70  
 
 71  1
     private static final FloatValidator VALIDATOR = new FloatValidator();
 72  
 
 73  
     /**
 74  
      * Return a singleton instance of this validator.
 75  
      * @return A singleton instance of the FloatValidator.
 76  
      */
 77  
     public static FloatValidator getInstance() {
 78  20
         return VALIDATOR;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Construct a <i>strict</i> instance.
 83  
      */
 84  
     public FloatValidator() {
 85  13
         this(true, STANDARD_FORMAT);
 86  13
     }
 87  
 
 88  
     /**
 89  
      * <p>Construct an instance with the specified strict setting
 90  
      *    and format type.</p>
 91  
      *    
 92  
      * <p>The <code>formatType</code> specified what type of
 93  
      *    <code>NumberFormat</code> is created - valid types
 94  
      *    are:</p>
 95  
      *    <ul>
 96  
      *       <li>AbstractNumberValidator.STANDARD_FORMAT -to create
 97  
      *           <i>standard</i> number formats (the default).</li>
 98  
      *       <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
 99  
      *           <i>currency</i> number formats.</li>
 100  
      *       <li>AbstractNumberValidator.PERCENT_FORMAT -to create
 101  
      *           <i>percent</i> number formats (the default).</li>
 102  
      *    </ul>
 103  
      * 
 104  
      * @param strict <code>true</code> if strict 
 105  
      *        <code>Format</code> parsing should be used.
 106  
      * @param formatType The <code>NumberFormat</code> type to
 107  
      *        create for validation, default is STANDARD_FORMAT.
 108  
      */
 109  
     public FloatValidator(boolean strict, int formatType) {
 110  25
         super(strict, formatType, true);
 111  25
     }
 112  
 
 113  
     /**
 114  
      * <p>Validate/convert a <code>Float</code> using the default
 115  
      *    <code>Locale</code>. 
 116  
      *
 117  
      * @param value The value validation is being performed on.
 118  
      * @return The parsed <code>Float</code> if valid or <code>null</code>
 119  
      *  if invalid.
 120  
      */
 121  
     public Float validate(String value) {
 122  2
         return (Float)parse(value, (String)null, (Locale)class="keyword">null);
 123  
     }
 124  
 
 125  
     /**
 126  
      * <p>Validate/convert a <code>Float</code> using the
 127  
      *    specified <i>pattern</i>. 
 128  
      *
 129  
      * @param value The value validation is being performed on.
 130  
      * @param pattern The pattern used to validate the value against.
 131  
      * @return The parsed <code>Float</code> if valid or <code>null</code> if invalid.
 132  
      */
 133  
     public Float validate(String value, String pattern) {
 134  10
         return (Float)parse(value, pattern, (Locale)null);
 135  
     }
 136  
 
 137  
     /**
 138  
      * <p>Validate/convert a <code>Float</code> using the
 139  
      *    specified <code>Locale</code>. 
 140  
      *
 141  
      * @param value The value validation is being performed on.
 142  
      * @param locale The locale to use for the number format, system default if null.
 143  
      * @return The parsed <code>Float</code> if valid or <code>null</code> if invalid.
 144  
      */
 145  
     public Float validate(String value, Locale locale) {
 146  2
         return (Float)parse(value, (String)null, locale);
 147  
     }
 148  
 
 149  
     /**
 150  
      * <p>Validate/convert a <code>Float</code> using the
 151  
      *    specified pattern and/ or <code>Locale</code>. 
 152  
      *
 153  
      * @param value The value validation is being performed on.
 154  
      * @param pattern The pattern used to validate the value against, or the
 155  
      *        default for the <code>Locale</code> if <code>null</code>.
 156  
      * @param locale The locale to use for the date format, system default if null.
 157  
      * @return The parsed <code>Float</code> if valid or <code>null</code> if invalid.
 158  
      */
 159  
     public Float validate(String value, String pattern, Locale locale) {
 160  2
         return (Float)parse(value, pattern, locale);
 161  
     }
 162  
 
 163  
     /**
 164  
      * Check if the value is within a specified range.
 165  
      * 
 166  
      * @param value The <code>Number</code> value to check.
 167  
      * @param min The minimum value of the range.
 168  
      * @param max The maximum value of the range.
 169  
      * @return <code>true</code> if the value is within the
 170  
      *         specified range.
 171  
      */
 172  
     public boolean isInRange(float value, class="keyword">float min, class="keyword">float max) {
 173  5
         return (value >= min && value <= max);
 174  
     }
 175  
 
 176  
     /**
 177  
      * Check if the value is within a specified range.
 178  
      * 
 179  
      * @param value The <code>Number</code> value to check.
 180  
      * @param min The minimum value of the range.
 181  
      * @param max The maximum value of the range.
 182  
      * @return <code>true</code> if the value is within the
 183  
      *         specified range.
 184  
      */
 185  
     public boolean isInRange(Float value, float min, class="keyword">float max) {
 186  5
         return isInRange(value.floatValue(), min, max);
 187  
     }
 188  
 
 189  
     /**
 190  
      * Check if the value is greater than or equal to a minimum.
 191  
      * 
 192  
      * @param value The value validation is being performed on.
 193  
      * @param min The minimum value.
 194  
      * @return <code>true</code> if the value is greater than
 195  
      *         or equal to the minimum.
 196  
      */
 197  
     public boolean minValue(float value, class="keyword">float min) {
 198  3
         return (value >= min);
 199  
     }
 200  
 
 201  
     /**
 202  
      * Check if the value is greater than or equal to a minimum.
 203  
      * 
 204  
      * @param value The value validation is being performed on.
 205  
      * @param min The minimum value.
 206  
      * @return <code>true</code> if the value is greater than
 207  
      *         or equal to the minimum.
 208  
      */
 209  
     public boolean minValue(Float value, float min) {
 210  3
         return minValue(value.floatValue(), min);
 211  
     }
 212  
 
 213  
     /**
 214  
      * Check if the value is less than or equal to a maximum.
 215  
      * 
 216  
      * @param value The value validation is being performed on.
 217  
      * @param max The maximum value.
 218  
      * @return <code>true</code> if the value is less than
 219  
      *         or equal to the maximum.
 220  
      */
 221  
     public boolean maxValue(float value, class="keyword">float max) {
 222  3
         return (value <= max);
 223  
     }
 224  
 
 225  
     /**
 226  
      * Check if the value is less than or equal to a maximum.
 227  
      * 
 228  
      * @param value The value validation is being performed on.
 229  
      * @param max The maximum value.
 230  
      * @return <code>true</code> if the value is less than
 231  
      *         or equal to the maximum.
 232  
      */
 233  
     public boolean maxValue(Float value, float max) {
 234  3
         return maxValue(value.floatValue(), max);
 235  
     }
 236  
 
 237  
     /**
 238  
      * <p>Perform further validation and convert the <code>Number</code> to
 239  
      * a <code>Float</code>.</p>
 240  
      * 
 241  
      * @param value The parsed <code>Number</code> object created.
 242  
      * @param formatter The Format used to parse the value with.
 243  
      * @return The parsed <code>Number</code> converted to a 
 244  
      *   <code>Float</code> if valid or <code>null</code> if invalid.
 245  
      */
 246  
     protected Object processParsedValue(Object value, Format formatter) {
 247  
 
 248  57
         double class="keyword">doubleValue = ((Number)value).class="keyword">doubleValue();
 249  
 
 250  57
         if (doubleValue > 0) {
 251  45
             if (doubleValue < Float.MIN_VALUE) {
 252  1
                 return null;
 253  
             }
 254  44
             if (doubleValue > Float.MAX_VALUE) {
 255  1
                 return null;
 256  
             }
 257  12
         } else  if (doubleValue < 0){
 258  4
             double posDouble = class="keyword">doubleValue * -1;
 259  4
             if (posDouble < Float.MIN_VALUE) {
 260  1
                 return null;
 261  
             }
 262  3
             if (posDouble > Float.MAX_VALUE) {
 263  1
                 return null;
 264  
             }
 265  
         }
 266  
 
 267  53
         return new Float((float)doubleValue);
 268  
 
 269  
     }
 270  
 
 271  
 }

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