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.engine.statement;
28  
29  import com.rapiddweller.benerator.Consumer;
30  import com.rapiddweller.benerator.GeneratorContext;
31  import com.rapiddweller.benerator.engine.BeneratorContext;
32  import com.rapiddweller.benerator.engine.Statement;
33  import com.rapiddweller.benerator.wrapper.ProductWrapper;
34  
35  /**
36   * {@link Statement} that consumes the current entity of a {@link GeneratorContext} using a {@link Consumer}.<br/><br/>
37   * Created: 01.09.2011 15:51:27
38   *
39   * @author Volker Bergmann
40   * @since 0.7.0
41   */
42  public class ConsumptionStatement implements Statement {
43  
44    private final Consumer consumer;
45    private final boolean start;
46    private final boolean finish;
47  
48    /**
49     * Instantiates a new Consumption statement.
50     *
51     * @param consumer the consumer
52     * @param start    the start
53     * @param finish   the finish
54     */
55    public ConsumptionStatement(Consumer consumer, boolean start, boolean finish) {
56      this.consumer = consumer;
57      this.start = start;
58      this.finish = finish;
59    }
60  
61    @Override
62    public boolean execute(BeneratorContext context) {
63      if (consumer != null) {
64        ProductWrapper<?> product = context.getCurrentProduct();
65        if (start) {
66          consumer.startConsuming(product);
67        }
68        if (finish) {
69          consumer.finishConsuming(product);
70        }
71      }
72      return true;
73    }
74  
75    @Override
76    public String toString() {
77      return getClass().getSimpleName() + "[" + consumer + "]";
78    }
79  
80  }