package net.sourceforge.javautil.groovy.builder.standard;
import java.util.Map;
import net.sourceforge.javautil.groovy.builder.DefaultGroovyBuilder;
import net.sourceforge.javautil.groovy.builder.GroovyBuildingContext;
import groovy.lang.Binding;
import groovy.util.BuilderSupport;
/**
* This is an abstract builder implementing the GroovyBuilder classes in order to provide a basic
* foundation for builders following the {@link groovy.lang.BuilderSupport} concept.
*
* @author elponderador
*
*/
public abstract class StandardBuilder<T> extends DefaultGroovyBuilder<T> {
private StandardStack<T> stack;
/**
* This will provide a new instance of {@link groovy.lang.Binding} for
* variable setting/resolution.
*/
public StandardBuilder() { this(new Binding()); }
/**
* You can provide your own binding for variable resolution.
*
* @param binding A binding for resolving undeclared variables.
*/
public StandardBuilder(Binding binding) {
super(binding, new StandardInterceptor());
this.stack = new StandardStack<T>();
this.context = new StandardBuildingContext();
}
/**
* This will be called by the interceptor for each node created.
*
* @param name The name of the node
* @param value The value, null if none passed
* @param parameters The key: value parameters passed to the node, null if none passed
*
* @return The node instance created by the implementation
*/
public abstract T startNode (Object name, Object value, Map<Object, Object> parameters);
/**
* This will be called after {@link #startNode(Object, Object, Map)} but before calling any closures
* so as to set the parent of the node.
*
* @param node The new node returned by the {@link #startNode(Object, Object, Map)} method.
* @param parent The parent instance previously returned by the {@link #startNode(Object, Object, Map)}, null if this is the first node.
*/
public void setParent (T node, T parent) {}
/**
* This allows code to be ran once the node has been completely ran, including any sub closures and sub nodes.
*
* @param node The node instance returned by {@link #startNode(Object, Object, Map)} for the current level of the stack.
*/
public void finishNode (T node) {}
@Override protected GroovyBuildingContext createNewContext() { return new StandardBuildingContext(); }
}