| 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 | |
|
| 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 | |
|
| 32 | |
|
| 33 | |
public void init(ParameterPair[] initParams) { |
| 34 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public Object generateInstance(final Class<?> type) throws TypeNotSupportedException { |
| 78 | |
|
| 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 | |
|
| 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 | |
|
| 105 | 0 | return null; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public Object generateInstance(Class<?> type, int modus) throws TypeNotSupportedException { |
| 113 | 0 | return generateInstance(type); |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 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 | |
|
| 137 | |
|
| 138 | |
|