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.NonNullGenerator; 31 import com.rapiddweller.common.BeanUtil; 32 33 import java.util.Collection; 34 35 /** 36 * {@link Generator} which takes one or more products from a source generator and wraps them with a {@link Collection}. 37 * <br/><br/> 38 * Created: 24.02.2012 19:52:24 39 * 40 * @param <I> the type parameter 41 * @param <C> the type parameter 42 * @author Volker Bergmann 43 * @since 0.7.6 44 */ 45 public class SingleSourceCollectionGenerator<I, C extends Collection<I>> extends CardinalGenerator<I, C> implements NonNullGenerator<C> { 46 47 private final Class<C> collectionType; 48 49 /** 50 * Instantiates a new Single source collection generator. 51 * 52 * @param source the source 53 * @param collectionType the collection type 54 * @param lengthGenerator the length generator 55 */ 56 public SingleSourceCollectionGenerator(Generator<I> source, Class<C> collectionType, 57 NonNullGenerator<Integer> lengthGenerator) { 58 super(source, false, lengthGenerator); 59 this.collectionType = collectionType; 60 } 61 62 // configuration properties ---------------------------------------------------------------------------------------- 63 64 @Override 65 public Class<C> getGeneratedType() { 66 return collectionType; 67 } 68 69 @Override 70 public ProductWrapper<C> generate(ProductWrapper<C> wrapper) { 71 return wrapper.wrap(generate()); 72 } 73 74 @Override 75 public C generate() { 76 ProductWrapper<Integer> sizeWrapper = generateCardinalWrapper(); 77 if (sizeWrapper == null) { 78 return null; 79 } 80 Integer size = sizeWrapper.unwrap(); 81 // the following works for primitive types as well as for objects 82 C collection; 83 if (size != null) { 84 collection = BeanUtil.newInstance(collectionType, new Object[] {size}); 85 } else { 86 collection = BeanUtil.newInstance(collectionType); 87 } 88 for (int i = 0; size == null || i < size; i++) { 89 ProductWrapper<I> component = generateFromSource(); 90 if (component == null) { 91 getSource().reset(); 92 break; 93 } 94 collection.add(component.unwrap()); 95 } 96 return collection; 97 } 98 99 }