1 /* 2 * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, is permitted under the terms of the 6 * GNU General Public License. 7 * 8 * For redistributing this software or a derivative work under a license other 9 * than the GPL-compatible Free Software License as defined by the Free 10 * Software Foundation or approved by OSI, you must first obtain a commercial 11 * license to this software product from rapiddweller GmbH & Volker Bergmann. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, 15 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE 17 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 package com.rapiddweller.benerator.wrapper; 28 29 import com.rapiddweller.benerator.Generator; 30 import com.rapiddweller.common.MathUtil; 31 32 import java.math.BigDecimal; 33 import java.math.MathContext; 34 35 /** 36 * Converts the {@link Number} products of another {@link Generator} to {@link BigDecimal}.<br/> 37 * <br/> 38 * Created at 23.06.2009 22:58:26 39 * 40 * @param <E> the type parameter 41 * @author Volker Bergmann 42 * @since 0.6.0 43 */ 44 public class AsBigDecimalGeneratorWrapper<E extends Number> extends GeneratorWrapper<E, BigDecimal> { 45 46 private int fractionDigits; 47 48 /** 49 * Instantiates a new As big decimal generator wrapper. 50 * 51 * @param source the source 52 */ 53 public AsBigDecimalGeneratorWrapper(Generator<E> source) { 54 this(source, null, null); 55 } 56 57 /** 58 * Instantiates a new As big decimal generator wrapper. 59 * 60 * @param source the source 61 * @param min the min 62 * @param granularity the granularity 63 */ 64 public AsBigDecimalGeneratorWrapper(Generator<E> source, BigDecimal min, BigDecimal granularity) { 65 super(source); 66 if (granularity != null) { 67 this.fractionDigits = MathUtil.fractionDigits(granularity.doubleValue()); 68 if (min != null) { 69 this.fractionDigits = Math.max(this.fractionDigits, MathUtil.fractionDigits(min.doubleValue())); 70 } 71 } else if (min != null) { 72 this.fractionDigits = MathUtil.fractionDigits(min.doubleValue()); 73 } else { 74 this.fractionDigits = 0; 75 } 76 } 77 78 @Override 79 public Class<BigDecimal> getGeneratedType() { 80 return BigDecimal.class; 81 } 82 83 @Override 84 public ProductWrapper<BigDecimal> generate(ProductWrapper<BigDecimal> wrapper) { 85 ProductWrapper<E> tmp = generateFromSource(); 86 if (tmp == null) { 87 return null; 88 } 89 E feed = tmp.unwrap(); 90 double d = feed.doubleValue(); 91 int prefixDigits = (Math.floor(d) == 0. ? 0 : MathUtil.prefixDigitCount(d)); 92 MathContext mathcontext = new MathContext(prefixDigits + fractionDigits); 93 return wrapper.wrap(new BigDecimal(d, mathcontext)); 94 } 95 96 }