Coverage Report - nl.beesting.beangenerator.generator.FloatingPointNumberInstanceGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
FloatingPointNumberInstanceGenerator
69%
31/45
69%
18/26
0
 
 1  
 package nl.beesting.beangenerator.generator;
 2  
 
 3  
 import java.math.BigDecimal;
 4  
 
 5  
 import nl.beesting.beangenerator.Constants;
 6  
 import nl.beesting.beangenerator.TypeNotSupportedException;
 7  
 import nl.beesting.beangenerator.util.ParameterDescriptor;
 8  
 import nl.beesting.beangenerator.util.ParameterPair;
 9  
 import nl.beesting.beangenerator.util.RandomUtil;
 10  
 
 11  
 import org.apache.commons.logging.Log;
 12  
 import org.apache.commons.logging.LogFactory;
 13  
 
 14  
 /**
 15  
  * Instance generator for floating point number types
 16  
  */
 17  
 public class FloatingPointNumberInstanceGenerator extends AbstractInstanceGenerator {
 18  1
         private static transient Log logger = LogFactory.getLog(FloatingPointNumberInstanceGenerator.class);
 19  
 
 20  1
         private double fromDouble = Double.MIN_VALUE;
 21  1
         private double untilDouble = Double.MAX_VALUE;
 22  
         
 23  1
         public FloatingPointNumberInstanceGenerator() {
 24  1
                 addSupportedTypes(new Class[] { Float.class, float.class, Double.class, double.class, BigDecimal.class });
 25  1
                 setSupportedParameterDescriptors(new ParameterDescriptor[] {
 26  
                                 new ParameterDescriptor(FROM_PARAMETER, String.class, "From. Left bound to generate floating point numbers."),
 27  
                                 new ParameterDescriptor(UNTIL_PARAMETER, String.class, "Until. Right bound to generate floating point numbers.") });
 28  1
         }
 29  
 
 30  
         /**
 31  
          * Initializes the instnce generator with the given array of objects.
 32  
          */
 33  
         public void init(ParameterPair[] initParams) {
 34  
                 // there is a possibility for the user to have specified between values
 35  5
                 if (initParams != null) {
 36  4
                         this.fromDouble = initializeParameterDouble(initParams, FROM_PARAMETER, false);
 37  4
                         this.untilDouble = initializeParameterDouble(initParams, UNTIL_PARAMETER, true);
 38  
 
 39  
                         // check that fromLong <= untilDouble
 40  4
                         if (fromDouble > untilDouble) {
 41  0
                                 String msg = "Given from value " + fromDouble + " is greater than the given until value "
 42  
                                                 + untilDouble + ".";
 43  
                                 if (!Constants.SLOPPY_MODE) {
 44  
                                         throw new InstantiationError(msg);
 45  
                                 } else {
 46  0
                                         logger.error(msg + " But sloppy mode therefore setting from value to Long.MIN_VALUE.");
 47  0
                                         fromDouble = Double.MIN_VALUE;
 48  
                                 }
 49  0
                         }
 50  
                 } else {
 51  
                         // default
 52  1
                         this.fromDouble = Double.MIN_VALUE;
 53  1
                         this.untilDouble = Double.MAX_VALUE;
 54  
                 }
 55  5
         }
 56  
 
 57  
         protected double getFromDouble() {
 58  0
                 return fromDouble;
 59  
         }
 60  
         
 61  
         protected double getUntilDouble() {
 62  0
                 return untilDouble;
 63  
         }
 64  
         
 65  
         /**
 66  
          * Create an instance for the given class. The returned object is of the given class. Note the following about the usage
 67  
          * of between init parameters (@see #init(ParameterPair[])) if for a supported type the from or until value lies out of
 68  
          * the boundaries of that type, the between values are set to the boundaries of the type.
 69  
          * 
 70  
          * @param type
 71  
          *            the type for which a random instance must be generated
 72  
          * @return a random floating point object between the defined from and until value
 73  
          * @see #init(ParameterPair[])
 74  
          * @throws TypeNotSupportedException
 75  
          *             if the given <code>type</code> is not supported.
 76  
          */
 77  
         public Object generateInstance(final Class<?> type) throws TypeNotSupportedException {
 78  
                 // preconditions
 79  10000
                 if (type == null) {
 80  0
                         throw new IllegalArgumentException("Input parameter type cannot be null.");
 81  
                 }
 82  10000
                 if (!checkSupport(type)) {
 83  0
                         throw new TypeNotSupportedException(type.getName(), "generateInstance");
 84  
                 }
 85  
                 // type
 86  10000
                 if (Float.class.equals(type) || float.class.equals(type)) {
 87  4000
                         if (fromDouble < Float.MIN_VALUE)
 88  0
                                 fromDouble = Float.MIN_VALUE;
 89  4000
                         else if (fromDouble > Float.MAX_VALUE)
 90  0
                                 fromDouble = Float.MIN_VALUE;
 91  4000
                         if (untilDouble < Float.MIN_VALUE)
 92  0
                                 untilDouble = Float.MAX_VALUE;
 93  4000
                         else if (untilDouble > Float.MAX_VALUE)
 94  0
                                 untilDouble = Float.MAX_VALUE;
 95  4000
             return (float) RandomUtil.initRandomDoubleBetween(fromDouble, untilDouble);
 96  
                 }
 97  6000
                 if (Double.class.equals(type) || double.class.equals(type)) {
 98  4000
             return RandomUtil.initRandomDoubleBetween(fromDouble, untilDouble);
 99  
                 }
 100  2000
                 if (BigDecimal.class.equals(type)) {
 101  2000
                         final double fp = RandomUtil.initRandomDoubleBetween(fromDouble, untilDouble);
 102  2000
                         return new BigDecimal(fp);
 103  
                 }
 104  
                 // only possible if a type is added but no generate code is provided
 105  0
                 return null;
 106  
         }
 107  
 
 108  
         /**
 109  
          * Create an instance for the given class with an instance generator specific modus. The returned object is of the given
 110  
          * class.
 111  
          */
 112  
         public Object generateInstance(Class<?> type, int modus) throws TypeNotSupportedException {
 113  0
                 return generateInstance(type);
 114  
         }
 115  
         
 116  
         /* For testing */
 117  
         protected Object getFromValue() {
 118  10005
                 return fromDouble;
 119  
         }
 120  
         
 121  
         protected Object getUntilValue() {
 122  10005
                 return untilDouble;
 123  
         }
 124  
         
 125  
         protected Object getDefaultFromValue() {
 126  2
                 return Double.MIN_VALUE;
 127  
         }
 128  
 
 129  
         protected Object getDefaultUntilValue() {
 130  2
                 return Double.MAX_VALUE;
 131  
         }
 132  
 
 133  
 }
 134  
 
 135  
 /*
 136  
  * $Log: FloatingPointNumberInstanceGenerator.java,v $ Revision 1.3 2004/10/29 11:19:23 gerard resolved compiler errors
 137  
  * Revision 1.2 2004/10/28 10:06:40 gerard added cvs log information tag
 138  
  */