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.Generator; 30 import com.rapiddweller.benerator.GeneratorContext; 31 import com.rapiddweller.benerator.NonNullGenerator; 32 import com.rapiddweller.benerator.wrapper.CardinalGenerator; 33 import com.rapiddweller.benerator.wrapper.ProductWrapper; 34 35 /** 36 * {@link Generator} which generates {@link String}s by first generating a part and a part count 37 * and the repeating the part the generated number of times.<br/><br/> 38 * Created: 08.07.2011 06:20:42 39 * 40 * @param <E> the type parameter 41 * @author Volker Bergmann 42 * @since 0.7.0 43 */ 44 public class EquivalenceStringGenerator<E> extends CardinalGenerator<E, String> implements NonNullGenerator<String> { 45 46 /** 47 * The Current length. 48 */ 49 protected Integer currentLength; 50 51 /** 52 * Instantiates a new Equivalence string generator. 53 * 54 * @param charGenerator the char generator 55 * @param lengthGenerator the length generator 56 */ 57 public EquivalenceStringGenerator(Generator<E> charGenerator, NonNullGenerator<Integer> lengthGenerator) { 58 super(charGenerator, true, lengthGenerator); 59 } 60 61 @Override 62 public Class<String> getGeneratedType() { 63 return String.class; 64 } 65 66 @Override 67 public synchronized void init(GeneratorContext context) { 68 super.init(context); 69 currentLength = generateCardinal(); 70 } 71 72 @Override 73 public ProductWrapper<String> generate(ProductWrapper<String> wrapper) { 74 String result = generate(); 75 return (result != null ? wrapper.wrap(result) : null); 76 } 77 78 @Override 79 public String generate() { 80 assertInitialized(); 81 if (currentLength == null) { 82 return null; 83 } 84 ProductWrapper<E> part = generateFromSource(); // try to select a new character and keep the previous length 85 if (part == null) { // if you are through with the characters, ... 86 currentLength = generateCardinal(); // ...choose the next length value... 87 if (currentLength == null) { 88 return null; 89 } 90 getSource().reset(); // ...and reset character selection 91 part = generateFromSource(); 92 } 93 String result = createString(part.unwrap(), currentLength); 94 if (currentLength == 0) { 95 while (generateFromSource() != null) { 96 // for length 0, any character repetition is "", 97 // so get rid of the remaining characters and proceed to the next length value 98 } 99 } 100 return result; 101 } 102 103 @Override 104 public void reset() { 105 super.reset(); 106 currentLength = generateCardinal(); 107 } 108 109 private String createString(E part, Integer length) { 110 return String.valueOf(part).repeat(Math.max(0, length)); 111 } 112 113 }