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.engine.Statement; 30 import com.rapiddweller.common.Element; 31 import com.rapiddweller.common.Visitor; 32 33 import java.io.Closeable; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Combines other statements to a composite statement.<br/><br/> 40 * Created: 27.10.2009 15:59:21 41 * 42 * @author Volker Bergmann 43 * @since 0.6.0 44 */ 45 public abstract class CompositeStatement extends AbstractStatement implements Closeable, Element<Statement> { 46 47 /** 48 * The Sub statements. 49 */ 50 protected List<Statement> subStatements = new ArrayList<>(); 51 52 /** 53 * Instantiates a new Composite statement. 54 */ 55 public CompositeStatement() { 56 this(null); 57 } 58 59 /** 60 * Instantiates a new Composite statement. 61 * 62 * @param subStatements the sub statements 63 */ 64 public CompositeStatement(List<Statement> subStatements) { 65 this.subStatements = (subStatements != null ? subStatements : new ArrayList<>()); 66 } 67 68 /** 69 * Gets sub statements. 70 * 71 * @return the sub statements 72 */ 73 public List<Statement> getSubStatements() { 74 return subStatements; 75 } 76 77 /** 78 * Add sub statement. 79 * 80 * @param subStatement the sub statement 81 */ 82 public void addSubStatement(Statement subStatement) { 83 subStatements.add(subStatement); 84 } 85 86 /** 87 * Sets sub statements. 88 * 89 * @param subStatements the sub statements 90 */ 91 public void setSubStatements(List<Statement> subStatements) { 92 this.subStatements = subStatements; 93 } 94 95 @Override 96 @SuppressWarnings({"unchecked", "rawtypes"}) 97 public void accept(Visitor<Statement> visitor) { 98 visitor.visit(this); 99 for (Statement subStatement : subStatements) { 100 if (subStatement instanceof Element) { 101 ((Element) subStatement).accept(visitor); 102 } else { 103 visitor.visit(subStatement); 104 } 105 } 106 } 107 108 @Override 109 public void close() throws IOException { 110 for (Statement subStatement : subStatements) { 111 if (subStatement instanceof Closeable) { 112 ((Closeable) subStatement).close(); 113 } 114 } 115 } 116 117 }