org.snapscript.tree.construct.MapEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
package org.snapscript.tree.construct;
import java.util.Map.Entry;
import org.snapscript.core.Evaluation;
import org.snapscript.core.error.InternalStateException;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.variable.Value;
public class MapEntry {
private final Evaluation value;
private final Evaluation key;
public MapEntry(Evaluation key, Evaluation value) {
this.value = value;
this.key = key;
}
public void define(Scope scope) throws Exception{
key.define(scope);
value.define(scope);
}
public void compile(Scope scope) throws Exception{
key.compile(scope, null);
value.compile(scope, null);
}
public Entry create(Scope scope) throws Exception{
Value valueResult = value.evaluate(scope, null);
Value keyResult = key.evaluate(scope, null);
Object valueObject = valueResult.getValue();
Object keyObject = keyResult.getValue();
return new Pair(keyObject, valueObject);
}
private class Pair implements Entry {
private final Object value;
private final Object key;
public Pair(Object key, Object value) {
this.value = value;
this.key = key;
}
@Override
public Object getKey() {
return key;
}
@Override
public Object getValue() {
return value;
}
@Override
public Object setValue(Object value) {
throw new InternalStateException("Modification of constant entry '" + key + "'");
}
}
}