org.snapscript.core.scope.ModelState Maven / Gradle / Ivy
package org.snapscript.core.scope;
import java.util.Iterator;
import java.util.Set;
import org.snapscript.common.Cache;
import org.snapscript.common.CompoundIterator;
import org.snapscript.common.HashCache;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.error.InternalStateException;
import org.snapscript.core.variable.Value;
public class ModelState implements State {
private final Cache constraints;
private final Cache values;
private final Scope scope;
private final Model model;
public ModelState() {
this(null);
}
public ModelState(Model model) {
this(model, null);
}
public ModelState(Model model, Scope scope) {
this.constraints = new HashCache();
this.values = new HashCache();
this.model = model;
this.scope = scope;
}
@Override
public Iterator iterator() {
Set keys = values.keySet();
Iterator inner = keys.iterator();
if(scope != null) {
State state = scope.getState();
Iterator outer = state.iterator();
return new CompoundIterator(inner, outer);
}
return inner;
}
@Override
public Value getValue(String name) {
Value value = values.fetch(name);
if(value == null && scope != null) {
State state = scope.getState();
if(state == null) {
throw new InternalStateException("Scope for '" + name + "' does not exist");
}
value = state.getValue(name);
}
if(value == null && model != null) {
Object object = model.getAttribute(name);
if(object != null) {
return Value.getConstant(object);
}
}
return value;
}
@Override
public void addValue(String name, Value value) {
Value variable = values.fetch(name);
if(variable != null) {
throw new InternalStateException("Variable '" + name + "' already exists");
}
values.cache(name, value);
}
@Override
public Constraint getConstraint(String name) {
Constraint constraint = constraints.fetch(name);
if(constraint == null && scope != null) {
State state = scope.getState();
if(state == null) {
throw new InternalStateException("Scope for '" + name + "' does not exist");
}
return state.getConstraint(name);
}
return constraint;
}
@Override
public void addConstraint(String name, Constraint constraint) {
Constraint existing = constraints.fetch(name);
if(existing != null) {
throw new InternalStateException("Constraint '" + name + "' already exists");
}
constraints.cache(name, constraint);
}
@Override
public String toString() {
return String.valueOf(values);
}
}