org.snapscript.core.function.ScopeAccessor Maven / Gradle / Ivy
package org.snapscript.core.function;
import org.snapscript.core.error.InternalStateException;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.scope.State;
import org.snapscript.core.variable.Value;
public class ScopeAccessor implements Accessor {
private final String name;
public ScopeAccessor(String name) {
this.name = name;
}
@Override
public Object getValue(Scope source) {
State state = source.getState();
Value field = state.getValue(name);
if(field == null){
throw new InternalStateException("Field '" + name + "' does not exist");
}
return field.getValue();
}
@Override
public void setValue(Scope source, Object value) {
State state = source.getState();
Value field = state.getValue(name);
if(field == null){
throw new InternalStateException("Field '" + name + "' does not exist");
}
field.setValue(value);
}
}