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.benerator.GeneratorContext; 31 import com.rapiddweller.benerator.NonNullGenerator; 32 import com.rapiddweller.benerator.distribution.Distribution; 33 import com.rapiddweller.benerator.distribution.SequenceManager; 34 import com.rapiddweller.benerator.util.WrapperProvider; 35 36 /** 37 * Combines a a random number a source generator's products into a collection.<br/> 38 * <br/> 39 * Created: 06.03.2008 16:08:22 40 * 41 * @param <S> the type parameter 42 * @param <P> the type parameter 43 * @author Volker Bergmann 44 */ 45 public abstract class CardinalGenerator<S, P> extends GeneratorWrapper<S, P> { 46 47 /** 48 * Generator that determines the cardinality of generation 49 */ 50 protected NonNullGenerator<Integer> cardinalGenerator; 51 /** 52 * The Resetting cardinal. 53 */ 54 final boolean resettingCardinal; 55 56 /** 57 * The Min cardinal. 58 */ 59 int minCardinal; 60 /** 61 * The Max cardinal. 62 */ 63 int maxCardinal; 64 /** 65 * The Cardinal granularity. 66 */ 67 int cardinalGranularity; 68 /** 69 * The Cardinal distribution. 70 */ 71 Distribution cardinalDistribution; 72 /** 73 * The Cardinal wrapper provider. 74 */ 75 final WrapperProvider<Integer> cardinalWrapperProvider = new WrapperProvider<>(); 76 77 // constructors ---------------------------------------------------------------------------------------------------- 78 79 /** 80 * Instantiates a new Cardinal generator. 81 * 82 * @param source the source 83 * @param resettingCardinal the resetting cardinal 84 * @param cardinalGenerator the cardinal generator 85 */ 86 public CardinalGenerator(Generator<S> source, boolean resettingCardinal, NonNullGenerator<Integer> cardinalGenerator) { 87 super(source); 88 this.cardinalGenerator = cardinalGenerator; 89 this.resettingCardinal = resettingCardinal; 90 } 91 92 /** 93 * Instantiates a new Cardinal generator. 94 * 95 * @param source the source 96 * @param resettingCardinalGenerator the resetting cardinal generator 97 */ 98 public CardinalGenerator(Generator<S> source, boolean resettingCardinalGenerator) { 99 this(source, resettingCardinalGenerator, 0, 30, 1, SequenceManager.RANDOM_SEQUENCE); 100 } 101 102 /** 103 * Instantiates a new Cardinal generator. 104 * 105 * @param source the source 106 * @param resettingCardinalGenerator the resetting cardinal generator 107 * @param minCardinal the min cardinal 108 * @param maxCardinal the max cardinal 109 * @param cardinalGranularity the cardinal granularity 110 * @param cardinalDistribution the cardinal distribution 111 */ 112 public CardinalGenerator(Generator<S> source, boolean resettingCardinalGenerator, 113 int minCardinal, int maxCardinal, int cardinalGranularity, Distribution cardinalDistribution) { 114 super(source); 115 this.minCardinal = minCardinal; 116 this.maxCardinal = maxCardinal; 117 this.cardinalGranularity = cardinalGranularity; 118 this.cardinalDistribution = (cardinalDistribution != null ? cardinalDistribution : SequenceManager.RANDOM_SEQUENCE); 119 this.resettingCardinal = resettingCardinalGenerator; 120 } 121 122 // Generator interface --------------------------------------------------------------------------------------------- 123 124 /** 125 * ensures consistency of the state 126 */ 127 @Override 128 public void init(GeneratorContext context) { 129 if (cardinalGenerator == null) { 130 cardinalGenerator = cardinalDistribution.createNumberGenerator(Integer.class, minCardinal, maxCardinal, cardinalGranularity, false); 131 } 132 cardinalGenerator.init(context); 133 super.init(context); 134 } 135 136 @Override 137 public void reset() { 138 assertInitialized(); 139 if (resettingCardinal) { 140 cardinalGenerator.reset(); 141 } 142 super.reset(); 143 } 144 145 // helpers --------------------------------------------------------------------------------------------------------- 146 147 /** 148 * Generate cardinal integer. 149 * 150 * @return the integer 151 */ 152 protected Integer generateCardinal() { 153 ProductWrapper<Integer> wrapper = generateCardinalWrapper(); 154 if (wrapper == null) { 155 return null; 156 } 157 return wrapper.unwrap(); 158 } 159 160 /** 161 * Generate cardinal wrapper product wrapper. 162 * 163 * @return the product wrapper 164 */ 165 protected ProductWrapper<Integer> generateCardinalWrapper() { 166 return cardinalGenerator.generate(cardinalWrapperProvider.get()); 167 } 168 169 }