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.IllegalGeneratorStateException;
32  import com.rapiddweller.benerator.InvalidGeneratorSetupException;
33  import com.rapiddweller.benerator.util.AbstractGenerator;
34  import com.rapiddweller.common.IOUtil;
35  import com.rapiddweller.common.ThreadAware;
36  import com.rapiddweller.format.DataContainer;
37  import com.rapiddweller.format.DataIterator;
38  import com.rapiddweller.format.DataSource;
39  import com.rapiddweller.format.util.ThreadLocalDataContainer;
40  
41  /**
42   * {@link Generator} implementation which reads and forwards data from a {@link DataSource}.<br/><br/>
43   * Created: 24.07.2011 08:58:09
44   *
45   * @param <E> the type parameter
46   * @author Volker Bergmann
47   * @since 0.7.0
48   */
49  public class DataSourceGenerator<E> extends AbstractGenerator<E> {
50  
51    private DataSource<E> source;
52    private DataIterator<E> iterator;
53    private final ThreadLocalDataContainer<E> container = new ThreadLocalDataContainer<>();
54  
55    // constructors ----------------------------------------------------------------------------------------------------
56  
57    /**
58     * Instantiates a new Data source generator.
59     */
60    public DataSourceGenerator() {
61      this(null);
62    }
63  
64    /**
65     * Instantiates a new Data source generator.
66     *
67     * @param source the source
68     */
69    public DataSourceGenerator(DataSource<E> source) {
70      this.source = source;
71      this.iterator = null;
72    }
73  
74    // properties ------------------------------------------------------------------------------------------------------
75  
76    /**
77     * Gets source.
78     *
79     * @return the source
80     */
81    public DataSource<E> getSource() {
82      return source;
83    }
84  
85    /**
86     * Sets source.
87     *
88     * @param source the source
89     */
90    public void setSource(DataSource<E> source) {
91      if (this.source != null) {
92        throw new IllegalGeneratorStateException("Mutating an initialized generator");
93      }
94      this.source = source;
95    }
96  
97    // Generator interface ---------------------------------------------------------------------------------------------
98  
99    @Override
100   public boolean isParallelizable() {
101     return false;
102   }
103 
104   @Override
105   public boolean isThreadSafe() {
106     return (source instanceof ThreadAware && ((ThreadAware) source).isThreadSafe());
107   }
108 
109   @Override
110   public Class<E> getGeneratedType() {
111     return source.getType();
112   }
113 
114   @Override
115   public void init(GeneratorContext context) {
116     if (source == null) {
117       throw new InvalidGeneratorSetupException("source", "is null");
118     }
119     super.init(context);
120   }
121 
122   @Override
123   public ProductWrapper<E> generate(ProductWrapper<E> wrapper) {
124     try {
125       assertInitialized();
126       if (iterator == null) {
127         iterator = source.iterator(); // iterator initialized lazily to reflect context state at invocation
128       }
129       DataContainer<E> tmp = iterator.next(container.get());
130       if (tmp == null) {
131         IOUtil.close(iterator);
132         return null;
133       }
134       return wrapper.wrap(tmp.getData());
135     } catch (Exception e) {
136       throw new IllegalGeneratorStateException("Generation failed: ", e);
137     }
138   }
139 
140   @Override
141   public void reset() {
142     IOUtil.close(iterator);
143     iterator = null;
144     super.reset();
145   }
146 
147   @Override
148   public void close() {
149     IOUtil.close(iterator);
150     super.close();
151     IOUtil.close(source);
152   }
153 
154   // java.lang.Object overrides --------------------------------------------------------------------------------------
155 
156   @Override
157   public String toString() {
158     return getClass().getSimpleName() + "[" + source + ']';
159   }
160 
161 }