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.sample;
28
29 import com.rapiddweller.benerator.util.ThreadSafeGenerator;
30 import com.rapiddweller.benerator.wrapper.ProductWrapper;
31 import com.rapiddweller.common.CollectionUtil;
32 import org.apache.logging.log4j.LogManager;
33 import org.apache.logging.log4j.Logger;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.List;
38
39 /**
40 * Creates a predefined sequence of objects.<br/>
41 * <br/>
42 * Created: 19.11.2007 15:21:24
43 *
44 * @param <E> the type parameter
45 * @author Volker Bergmann
46 */
47 public class SequenceGenerator<E> extends ThreadSafeGenerator<E> {
48
49 private static final Logger logger = LogManager.getLogger(SequenceGenerator.class);
50
51 private final Class<E> productType;
52 private List<E> values;
53 private int cursor;
54
55 /**
56 * Instantiates a new Sequence generator.
57 *
58 * @param productType the product type
59 * @param values the values
60 */
61 @SafeVarargs
62 public SequenceGenerator(Class<E> productType, E... values) {
63 this(productType, (values != null ? CollectionUtil.toList(values) : null));
64 }
65
66 /**
67 * Instantiates a new Sequence generator.
68 *
69 * @param productType the product type
70 * @param values the values
71 */
72 public SequenceGenerator(Class<E> productType, Collection<? extends E> values) {
73 this.productType = productType;
74 this.values = (values != null ? new ArrayList<>(values) : new ArrayList<>());
75 this.cursor = 0;
76 }
77
78 /**
79 * Add value.
80 *
81 * @param value the value
82 */
83 public void addValue(E value) {
84 this.values.add(value);
85 }
86
87 // Generator interface ---------------------------------------------------------------------------------------------
88
89 @Override
90 public Class<E> getGeneratedType() {
91 return productType;
92 }
93
94 @Override
95 public synchronized ProductWrapper<E> generate(ProductWrapper<E> wrapper) {
96 if (cursor < 0) {
97 return null;
98 }
99 E result = values.get(cursor);
100 if (cursor < values.size() - 1) {
101 cursor++;
102 } else {
103 cursor = -1;
104 }
105 if (logger.isDebugEnabled()) {
106 logger.debug("created: " + result);
107 }
108 return wrapper.wrap(result);
109 }
110
111 @Override
112 public synchronized void reset() {
113 cursor = 0;
114 super.reset();
115 }
116
117 @Override
118 public synchronized void close() {
119 values = null;
120 cursor = -1;
121 super.close();
122 }
123
124 @Override
125 public String toString() {
126 return getClass().getSimpleName() + values;
127 }
128
129 }