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.GeneratorContext;
30 import com.rapiddweller.benerator.InvalidGeneratorSetupException;
31 import com.rapiddweller.benerator.util.ThreadSafeGenerator;
32 import com.rapiddweller.benerator.wrapper.ProductWrapper;
33
34 /**
35 * Returns a value only once and then becomes unavailable immediately.<br/>
36 * <br/>
37 * Created at 23.09.2009 00:20:03
38 *
39 * @param <E> the type parameter
40 * @author Volker Bergmann
41 * @since 0.6.0
42 */
43 public class OneShotGenerator<E> extends ThreadSafeGenerator<E> {
44
45 private E value;
46 private final Class<E> generatedType;
47 private boolean used;
48
49 /**
50 * Instantiates a new One shot generator.
51 *
52 * @param value the value
53 */
54 @SuppressWarnings("unchecked")
55 public OneShotGenerator(E value) {
56 this(value, (Class<E>) value.getClass());
57 }
58
59 /**
60 * Instantiates a new One shot generator.
61 *
62 * @param value the value
63 * @param generatedType the generated type
64 */
65 public OneShotGenerator(E value, Class<E> generatedType) {
66 this.value = value;
67 this.generatedType = generatedType;
68 this.used = false;
69 }
70
71 @Override
72 public void close() {
73 used = true;
74 value = null;
75 super.close();
76 }
77
78 @Override
79 public ProductWrapper<E> generate(ProductWrapper<E> wrapper) {
80 if (used) {
81 return null;
82 }
83 used = true;
84 return wrapper.wrap(value);
85 }
86
87 @Override
88 public Class<E> getGeneratedType() {
89 return generatedType;
90 }
91
92 @Override
93 public void init(GeneratorContext context) throws InvalidGeneratorSetupException {
94 super.init(context);
95 }
96
97 @Override
98 public void reset() {
99 used = false;
100 super.reset();
101 }
102
103 @Override
104 public String toString() {
105 return getClass().getSimpleName() + '[' + value + ']';
106 }
107
108 }