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.GeneratorState;
32  import com.rapiddweller.benerator.InvalidGeneratorSetupException;
33  import com.rapiddweller.benerator.util.AbstractGenerator;
34  import com.rapiddweller.benerator.util.WrapperProvider;
35  import com.rapiddweller.common.BeanUtil;
36  import com.rapiddweller.common.IOUtil;
37  import com.rapiddweller.common.NullSafeComparator;
38  
39  /**
40   * Abstract generator class that wraps another generator object (in a <i>source</i> property)
41   * and delegates life cycle control to it.<br/>
42   * <br/>
43   * Created: 12.12.2006 19:13:55
44   *
45   * @param <S> the type parameter
46   * @param <P> the type parameter
47   * @author Volker Bergmann
48   * @since 0.1
49   */
50  public abstract class GeneratorWrapper<S, P> extends AbstractGenerator<P> {
51  
52    private Generator<S> source;
53    private final WrapperProvider<S> sourceWrapperProvider = new WrapperProvider<>();
54  
55    /**
56     * Instantiates a new Generator wrapper.
57     *
58     * @param source the source
59     */
60    public GeneratorWrapper(Generator<S> source) {
61      this.source = source;
62    }
63  
64    // config properties -----------------------------------------------------------------------------------------------
65  
66    /**
67     * Returns the source generator
68     *
69     * @return the source
70     */
71    public Generator<S> getSource() {
72      return source;
73    }
74  
75    /**
76     * Sets the source generator
77     *
78     * @param source the source
79     */
80    public void setSource(Generator<S> source) {
81      this.source = source;
82    }
83  
84    /**
85     * Generate from source product wrapper.
86     *
87     * @return the product wrapper
88     */
89    protected ProductWrapper<S> generateFromSource() {
90      return source.generate(getSourceWrapper());
91    }
92  
93    /**
94     * Gets source wrapper.
95     *
96     * @return the source wrapper
97     */
98    protected ProductWrapper<S> getSourceWrapper() {
99      return sourceWrapperProvider.get();
100   }
101 
102   // Generator interface implementation ------------------------------------------------------------------------------
103 
104   @Override
105   public boolean isThreadSafe() {
106     return source.isThreadSafe();
107   }
108 
109   @Override
110   public boolean isParallelizable() {
111     return source.isParallelizable();
112   }
113 
114   @Override
115   public synchronized void init(GeneratorContext context) {
116     assertNotInitialized();
117     if (source == null) {
118       throw new InvalidGeneratorSetupException("source", "is null");
119     }
120     synchronized (source) {
121       if (!source.wasInitialized()) {
122         source.init(context);
123       }
124     }
125     super.init(context);
126   }
127 
128   @Override
129   public void reset() {
130     super.reset();
131     source.reset();
132   }
133 
134   @Override
135   public void close() {
136     super.close();
137     IOUtil.close(source);
138   }
139 
140   // java.lang.Object overrides --------------------------------------------------------------------------------------
141 
142   @Override
143   public boolean equals(Object other) {
144     if (this == other) {
145       return true;
146     }
147     if (other == null || getClass() != other.getClass()) {
148       return false;
149     }
150     GeneratorWrapper<?, ?> that = (GeneratorWrapper<?, ?>) other;
151     return NullSafeComparator.equals(this.source, that.source);
152   }
153 
154   @Override
155   public int hashCode() {
156     return source.hashCode();
157   }
158 
159   @Override
160   public String toString() {
161     return (state != GeneratorState.CREATED ? BeanUtil.toString(this) : getClass().getSimpleName());
162   }
163 
164 }