org.etlunit.context.VariableContextImpl Maven / Gradle / Ivy
package org.etlunit.context;
import org.etlunit.parser.ETLTestParser;
import org.etlunit.parser.ETLTestValueObject;
import org.etlunit.parser.ETLTestValueObjectImpl;
import org.etlunit.parser.ParseException;
import java.util.*;
public class VariableContextImpl implements VariableContext
{
private final Map variableMap = new HashMap();
private final VariableContext enclosingContext;
class MapRepresentation implements Map
{
@Override
public int size()
{
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty()
{
throw new UnsupportedOperationException();
}
@Override
public boolean containsKey(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public boolean containsValue(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public ETLTestValueObject get(Object o)
{
return getValue(String.valueOf(o));
}
@Override
public ETLTestValueObject put(String s, ETLTestValueObject o)
{
setValue(s, o);
return null;
}
@Override
public ETLTestValueObject remove(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map extends String, ? extends ETLTestValueObject> map)
{
throw new UnsupportedOperationException();
}
@Override
public void clear()
{
throw new UnsupportedOperationException();
}
@Override
public Set keySet()
{
throw new UnsupportedOperationException();
}
@Override
public Collection values()
{
throw new UnsupportedOperationException();
}
@Override
public Set> entrySet()
{
throw new UnsupportedOperationException();
}
}
private final MapRepresentation mapRepresentation = new MapRepresentation();
private static final VariableContext NULL_CONTEXT = new VariableContext()
{
@Override
public void declareVariable(String variableName)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public boolean hasVariableBeenDeclared(String variableName)
{
return false;
}
@Override
public void removeVariable(String variableName)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public ETLTestValueObject query(String variablePath)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public ETLTestValueObject getValue(String variableName)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public void setValue(String variableName, ETLTestValueObject value)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public void setStringValue(String variableName, String value)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public void setJSONValue(String variableName, String value)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public void declareAndSetValue(String variableName, ETLTestValueObject value)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public void declareAndSetStringValue(String variableName, String value)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public void declareAndSetJSONValue(String variableName, String value)
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public VariableContext createNestedScope()
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public VariableContext getEnclosingScope()
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public VariableContext getTopLevelScope()
{
throw new IllegalStateException("NULL Context does not operate");
}
@Override
public Map getMapRepresentation()
{
return Collections.emptyMap();
}
@Override
public Map getVelocityWrapper()
{
throw new UnsupportedOperationException();
}
};
private static final ETLTestValueObject NULL = new ETLTestValueObjectImpl("null");
private VariableVelocityWrapper variableVelocityWrapper = new VariableVelocityWrapper(this);
private VariableContextImpl(VariableContext context)
{
enclosingContext = context;
}
public VariableContextImpl()
{
enclosingContext = NULL_CONTEXT;
}
@Override
public ETLTestValueObject getValue(String variableName)
{
if (!variableMap.containsKey(variableName))
{
if (!enclosingContext.hasVariableBeenDeclared(variableName))
{
throw new IllegalArgumentException("Variable " + variableName + " is undeclared");
}
return enclosingContext.getValue(variableName);
}
ETLTestValueObject v = variableMap.get(variableName);
if (v == NULL)
{
return null;
}
//somehow convert the value using the velocity wrapper
return v;
}
@Override
public void setStringValue(String variableName, String value)
{
setValue(variableName, value == null ? null : new ETLTestValueObjectImpl(value));
}
@Override
public void setJSONValue(String variableName, String value)
{
try
{
setValue(variableName, ETLTestParser.loadObject(value));
}
catch (ParseException e)
{
throw new IllegalArgumentException(e);
}
}
@Override
public void declareAndSetValue(String variableName, ETLTestValueObject value)
{
if (!hasVariableBeenDeclared(variableName))
{
declareVariable(variableName);
}
setValue(variableName, value);
}
@Override
public void declareAndSetStringValue(String variableName, String value)
{
if (!hasVariableBeenDeclared(variableName))
{
declareVariable(variableName);
}
setStringValue(variableName, value);
}
@Override
public void declareAndSetJSONValue(String variableName, String value)
{
if (!hasVariableBeenDeclared(variableName))
{
declareVariable(variableName);
}
setJSONValue(variableName, value);
}
@Override
public void setValue(String variableName, ETLTestValueObject value)
{
if (!variableMap.containsKey(variableName))
{
if (!enclosingContext.hasVariableBeenDeclared(variableName))
{
throw new IllegalArgumentException("Variable " + variableName + " is undeclared");
}
enclosingContext.setValue(variableName, value);
}
else
{
variableMap.put(variableName, value == null ? NULL : value);
}
}
@Override
public void declareVariable(String variableName)
{
if (variableMap.containsKey(variableName))
{
throw new IllegalArgumentException("Variable " + variableName + " already exists in this scope");
}
variableMap.put(variableName, NULL);
}
@Override
public boolean hasVariableBeenDeclared(String variableName)
{
return variableMap.containsKey(variableName) || enclosingContext.hasVariableBeenDeclared(variableName);
}
@Override
public void removeVariable(String variableName)
{
if (!variableMap.containsKey(variableName))
{
if (enclosingContext.hasVariableBeenDeclared(variableName))
{
enclosingContext.removeVariable(variableName);
}
else
{
throw new IllegalArgumentException("Variable " + variableName + " not declared in this scope");
}
}
variableMap.remove(variableName);
}
@Override
public ETLTestValueObject query(String variablePath)
{
String[] pathElements = variablePath.split("\\.");
ETLTestValueObject value = getValue(pathElements[0]);
if (pathElements.length == 1)
{
return value;
}
else
{
return value.query(variablePath.substring(pathElements[0].length() + 1));
}
}
@Override
public VariableContext createNestedScope()
{
return new VariableContextImpl(this);
}
@Override
public VariableContext getEnclosingScope()
{
return enclosingContext == NULL_CONTEXT ? null : enclosingContext;
}
@Override
public VariableContext getTopLevelScope()
{
VariableContext scope = this;
VariableContext nextScope = this;
while ((nextScope = scope.getEnclosingScope()) != null)
{
scope = nextScope;
}
return scope;
}
@Override
public Map getMapRepresentation()
{
return mapRepresentation;
}
@Override
public Map getVelocityWrapper()
{
return variableVelocityWrapper;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy