
lt.repl.scripting.LatteScope Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of latte-compiler Show documentation
Show all versions of latte-compiler Show documentation
The latte-lang compiler project, which contains compiler and runtime required library.
The newest version!
package lt.repl.scripting;
import javax.script.Bindings;
import java.util.*;
/**
* eval scope
*/
public class LatteScope implements Bindings {
private final Map> types = new HashMap>();
private final Map objects = new LinkedHashMap();
public LatteScope() {
}
@Override
public synchronized Object put(String name, Object value) {
if (name == null || name.isEmpty()) throw new IllegalArgumentException("invalid name, empty");
if (types.containsKey(name)) {
Class> type = types.get(name);
if (type.isPrimitive()) {
if (value == null) {
throw new NullPointerException("assign null to primitive type variable");
}
if (type == int.class) {
type = Integer.class;
} else if (type == long.class) {
type = Long.class;
} else if (type == float.class) {
type = Float.class;
} else if (type == double.class) {
type = Double.class;
} else if (type == short.class) {
type = Short.class;
} else if (type == byte.class) {
type = Byte.class;
} else if (type == char.class) {
type = Character.class;
} else if (type == boolean.class) {
type = Boolean.class;
} else throw new IllegalArgumentException("unknown primitive type " + type);
}
if (value == null || type.isInstance(value)) {
return objects.put(name, value);
} else {
throw new IllegalArgumentException(name + " is not of type " + type.getName());
}
} else {
return putNew(name, value, Object.class);
}
}
public synchronized Object putNew(String name, Object value, Class> type) {
types.put(name, type);
return objects.put(name, value);
}
@Override
public synchronized void putAll(Map extends String, ?> toMerge) {
for (Entry extends String, ?> e : toMerge.entrySet()) {
put(e.getKey(), e.getValue());
}
}
@Override
public void clear() {
objects.clear();
}
@Override
public Set keySet() {
return objects.keySet();
}
@Override
public Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy