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.benerator.engine.expression.CachedExpression; 31 import com.rapiddweller.common.Context; 32 import com.rapiddweller.common.ErrorHandler; 33 import com.rapiddweller.common.Level; 34 import com.rapiddweller.script.Expression; 35 36 /** 37 * Abstract implementation of the Statement interface.<br/><br/> 38 * Created: 27.10.2009 20:16:20 39 * 40 * @author Volker Bergmann 41 * @since 0.5.0 42 */ 43 public abstract class AbstractStatement implements Statement { 44 45 private final Expression<ErrorHandler> errorHandler; 46 47 // constructors ---------------------------------------------------------------------------------------------------- 48 49 /** 50 * Instantiates a new Abstract statement. 51 */ 52 protected AbstractStatement() { 53 this(null); 54 } 55 56 /** 57 * Instantiates a new Abstract statement. 58 * 59 * @param errorHandler the error handler 60 */ 61 protected AbstractStatement(Expression<ErrorHandler> errorHandler) { 62 this.errorHandler = errorHandler; 63 } 64 65 // Task interface -------------------------------------------------------------------------------------------------- 66 67 /** 68 * Gets error handler. 69 * 70 * @param context the context 71 * @return the error handler 72 */ 73 public ErrorHandler getErrorHandler(Context context) { 74 if (errorHandler == null) { 75 return new ErrorHandler(getClass().getName(), Level.fatal); 76 } 77 return errorHandler.evaluate(context); 78 } 79 80 // helpers --------------------------------------------------------------------------------------------------------- 81 82 /** 83 * Handle error. 84 * 85 * @param message the message 86 * @param context the context 87 */ 88 protected void handleError(String message, Context context) { 89 getErrorHandler(context).handleError(message); 90 } 91 92 /** 93 * Handle error. 94 * 95 * @param message the message 96 * @param context the context 97 * @param t the t 98 */ 99 protected void handleError(String message, Context context, Throwable t) { 100 getErrorHandler(context).handleError(message, t); 101 } 102 103 /** 104 * Cache expression. 105 * 106 * @param <T> the type parameter 107 * @param expression the expression 108 * @return the expression 109 */ 110 protected static <T> Expression<T> cache(Expression<T> expression) { 111 return (expression != null ? new CachedExpression<>(expression) : null); 112 } 113 114 @Override 115 public String toString() { 116 return getClass().getSimpleName(); 117 } 118 119 }