flabbergast.ReflectedFrame Maven / Gradle / Ivy
package flabbergast;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* A Frame wrapper over a Java object.
*/
public class ReflectedFrame extends Frame {
private final Object backing;
private final TreeMap attributes;
public interface Transform {
Object invoke(T src);
}
public static ReflectedFrame create(TaskMaster task_master, T backing,
Map> accessors) {
TreeMap attributes = new TreeMap();
for (Entry> entry : accessors.entrySet()) {
Object result = entry.getValue().invoke(backing);
if (result == null) {
result = Unit.NULL;
} else if (result instanceof Boolean || result instanceof Double
|| result instanceof Long || result instanceof Frame
|| result instanceof Stringish
|| result instanceof Template || result instanceof Unit) {
} else if (result instanceof String) {
result = new SimpleStringish((String) result);
} else {
throw new ClassCastException("Value for " + entry.getKey()
+ " is non-Flabbergast type "
+ result.getClass().getSimpleName() + ".");
}
attributes.put(entry.getKey(), result);
}
return new ReflectedFrame(task_master, new JavaSourceReference(),
backing, attributes);
}
private ReflectedFrame(TaskMaster task_master, SourceReference source_ref,
Object backing, TreeMap attributes) {
super(task_master, source_ref, null, null);
this.backing = backing;
this.attributes = attributes;
}
@Override
public int count() {
return attributes.size();
}
/**
* Access the functions in the frames. Frames should not be mutated, but
* this policy is not enforced by this class; it must be done in the calling
* code.
*/
@Override
public Object get(String name) {
return attributes.containsKey(name) ? attributes.get(name) : null;
}
public Object getBacking() {
return backing;
}
/**
* Check if an attribute name is present in the frame.
*/
@Override
public boolean has(String name) {
return attributes.containsKey(name);
}
@Override
public Iterator iterator() {
return attributes.keySet().iterator();
}
public void set(String name, Object value) {
if (attributes.containsKey(name)) {
throw new IllegalArgumentException();
}
attributes.put(name, value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy