View Javadoc
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.InvalidGeneratorSetupException;
32  import com.rapiddweller.benerator.distribution.Distribution;
33  import com.rapiddweller.benerator.distribution.SequenceManager;
34  import com.rapiddweller.common.BeanUtil;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashSet;
39  import java.util.List;
40  import java.util.Set;
41  
42  /**
43   * Combines a a random number a source generator's products into a collection.<br/>
44   * <br/>
45   * Created: 07.07.2006 19:13:22
46   *
47   * @param <C> the type parameter
48   * @param <I> the type parameter
49   * @author Volker Bergmann
50   * @since 0.1
51   */
52  @SuppressWarnings({"unchecked", "rawtypes"})
53  public class CollectionGenerator<C extends Collection, I> extends CardinalGenerator<I, C> {
54  
55    /**
56     * The collection type to create
57     */
58    private Class<C> collectionType;
59  
60    // constructors ----------------------------------------------------------------------------------------------------
61  
62    /**
63     * Instantiates a new Collection generator.
64     */
65    public CollectionGenerator() {
66      this(((Class<C>) List.class), null, 0, 30, SequenceManager.RANDOM_SEQUENCE);
67    }
68  
69    /**
70     * Instantiates a new Collection generator.
71     *
72     * @param collectionType   the collection type
73     * @param source           the source
74     * @param minSize          the min size
75     * @param maxSize          the max size
76     * @param sizeDistribution the size distribution
77     */
78    public CollectionGenerator(Class<C> collectionType, Generator<I> source,
79                               int minSize, int maxSize, Distribution sizeDistribution) {
80      super(source, false, minSize, maxSize, 1, sizeDistribution);
81      this.collectionType = mapCollectionType(collectionType);
82    }
83  
84    // configuration properties ----------------------------------------------------------------------------------------
85  
86    /**
87     * Gets collection type.
88     *
89     * @return the collection type
90     */
91    public Class<C> getCollectionType() {
92      return collectionType;
93    }
94  
95    /**
96     * Sets collection type.
97     *
98     * @param collectionType the collection type
99     */
100   public void setCollectionType(Class<C> collectionType) {
101     this.collectionType = collectionType;
102   }
103 
104   // Generator interface ---------------------------------------------------------------------------------------------
105 
106   /**
107    * ensures consistency of the state
108    */
109   @Override
110   public void init(GeneratorContext context) {
111     if (collectionType == null) {
112       throw new InvalidGeneratorSetupException("collectionType", "undefined");
113     }
114     super.init(context);
115   }
116 
117   @Override
118   public Class<C> getGeneratedType() {
119     return collectionType;
120   }
121 
122   @Override
123   public ProductWrapper<C> generate(ProductWrapper<C> wrapper) {
124     assertInitialized();
125     Integer size = generateCardinal();
126     if (size == null) {
127       return null;
128     }
129     C collection = BeanUtil.newInstance(collectionType);
130     for (int i = 0; i < size; i++) {
131       ProductWrapper<I> item = generateFromSource();
132       if (item == null) {
133         return null;
134       }
135       collection.add(item.unwrap());
136     }
137     return wrapper.wrap(collection);
138   }
139 
140   // implementation --------------------------------------------------------------------------------------------------
141 
142   /**
143    * maps abstract collection types to concrete ones
144    */
145   private static <C extends Collection> Class<C> mapCollectionType(Class<C> collectionType) {
146     if (List.class.equals(collectionType)) {
147       return (Class<C>) ArrayList.class;
148     } else if (Set.class.equals(collectionType)) {
149       return (Class<C>) HashSet.class;
150     } else {
151       return collectionType;
152     }
153   }
154 
155 }