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.primitive; 28 29 import com.rapiddweller.benerator.Consumer; 30 import com.rapiddweller.benerator.consumer.AbstractConsumer; 31 import com.rapiddweller.common.Accessor; 32 import com.rapiddweller.common.Converter; 33 import com.rapiddweller.common.StringUtil; 34 import com.rapiddweller.common.accessor.FeatureAccessor; 35 import com.rapiddweller.common.converter.NumberToNumberConverter; 36 import com.rapiddweller.script.PrimitiveType; 37 import com.rapiddweller.script.math.ArithmeticEngine; 38 39 /** 40 * {@link Consumer} implementation which sums up the values of a 'feature' of all objects it consumes 41 * and return the sum as 'sum' property of type 'type'.<br/><br/> 42 * Created: 03.04.2010 07:41:42 43 * 44 * @author Volker Bergmann 45 * @since 0.6.0 46 */ 47 public class AddingConsumer extends AbstractConsumer { 48 49 private Accessor<Object, Number> accessor; 50 51 private Converter<Number, ? extends Number> converter; 52 53 private Number sum; 54 55 /** 56 * Instantiates a new Adding consumer. 57 */ 58 public AddingConsumer() { 59 this(null, null); 60 } 61 62 /** 63 * Instantiates a new Adding consumer. 64 * 65 * @param feature the feature 66 * @param type the type 67 */ 68 public AddingConsumer(String feature, String type) { 69 setFeature(feature); 70 setType(type); 71 } 72 73 /** 74 * Sets feature. 75 * 76 * @param feature the feature 77 */ 78 public void setFeature(String feature) { 79 this.accessor = (feature != null ? new FeatureAccessor<>(feature, true) : null); 80 } 81 82 /** 83 * Sets type. 84 * 85 * @param typeName the type name 86 */ 87 @SuppressWarnings({"unchecked", "rawtypes"}) 88 public void setType(String typeName) { 89 if (StringUtil.isEmpty(typeName)) { 90 typeName = "double"; 91 } 92 Class<? extends Number> numberType = (Class<? extends Number>) PrimitiveType.getInstance(typeName).getJavaType(); 93 this.converter = new NumberToNumberConverter(Number.class, numberType); 94 this.sum = converter.convert(0); 95 } 96 97 /** 98 * Gets sum. 99 * 100 * @return the sum 101 */ 102 public Number getSum() { 103 return this.sum; 104 } 105 106 @Override 107 public void startProductConsumption(Object object) { 108 Number addend = converter.convert(accessor.getValue(object)); 109 this.sum = (Number) ArithmeticEngine.defaultInstance().add(sum, addend); 110 } 111 112 }