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.NonNullGenerator;
31  import com.rapiddweller.benerator.distribution.Distribution;
32  import com.rapiddweller.benerator.distribution.SequenceManager;
33  import com.rapiddweller.common.ArrayUtil;
34  
35  import java.lang.reflect.Array;
36  
37  /**
38   * Assembles the output of a source generator into an array of random length.<br/>
39   * <br/>
40   * Created: 26.08.2006 09:37:55
41   *
42   * @param <S> the type parameter
43   * @param <P> the type parameter
44   * @author Volker Bergmann
45   * @since 0.1
46   */
47  public class SingleSourceArrayGenerator<S, P> extends CardinalGenerator<S, P> implements NonNullGenerator<P> {
48  
49    private final Class<S> componentType;
50    private final Class<P> generatedType;
51  
52    /**
53     * Instantiates a new Single source array generator.
54     *
55     * @param source             the source
56     * @param componentType      the component type
57     * @param minLength          the min length
58     * @param maxLength          the max length
59     * @param lengthDistribution the length distribution
60     */
61    @SuppressWarnings("unchecked")
62    public SingleSourceArrayGenerator(Generator<S> source, Class<S> componentType,
63                                      int minLength, int maxLength, Distribution lengthDistribution) {
64      super(source, false, minLength, maxLength, 1, SequenceManager.RANDOM_SEQUENCE);
65      this.componentType = componentType;
66      this.generatedType = ArrayUtil.arrayType(componentType);
67    }
68  
69    /**
70     * Instantiates a new Single source array generator.
71     *
72     * @param source          the source
73     * @param componentType   the component type
74     * @param lengthGenerator the length generator
75     */
76    @SuppressWarnings("unchecked")
77    public SingleSourceArrayGenerator(Generator<S> source, Class<S> componentType,
78                                      NonNullGenerator<Integer> lengthGenerator) {
79      super(source, false, lengthGenerator);
80      this.componentType = componentType;
81      this.generatedType = ArrayUtil.arrayType(componentType);
82    }
83  
84    // configuration properties ----------------------------------------------------------------------------------------
85  
86    @Override
87    public Class<P> getGeneratedType() {
88      return generatedType;
89    }
90  
91    @Override
92    public ProductWrapper<P> generate(ProductWrapper<P> wrapper) {
93      return wrapper.wrap(generate());
94    }
95  
96    @Override
97    public P generate() {
98      Integer size = generateCardinal();
99      if (size == null) {
100       return null;
101     }
102     // the following works for primitive types as well as for objects
103     @SuppressWarnings("unchecked")
104     P array = (P) ArrayUtil.newInstance(componentType, size);
105     for (int i = 0; i < size; i++) {
106       ProductWrapper<S> component = generateFromSource();
107       if (component == null) {
108         return null;
109       }
110       Array.set(array, i, component.unwrap());
111     }
112     return array;
113   }
114 
115 }