01: package gnu.mapping;
02:
03: /** A Constraint is used to control the values in a Binding.
04: * This is a very general mechanism, since you can change the Constaint
05: * associated with a Binding as needed. */
06:
07: public abstract class Constraint {
08: public abstract Object get(Binding binding, Object defaultValue);
09:
10: public final Object get(Binding binding) {
11: Object value = get(binding, Binding.UNBOUND);
12: if (value == Binding.UNBOUND)
13: throw new UnboundSymbol(binding.getName());
14: return value;
15: }
16:
17: public abstract void set(Binding binding, Object value);
18:
19: public boolean isBound(Binding binding) {
20: try {
21: get(binding);
22: return true;
23: } catch (UnboundSymbol ex) {
24: return false;
25: }
26: }
27:
28: public Procedure getProcedure(Binding binding) {
29: return (Procedure) get(binding);
30: }
31:
32: /** Return Environment containing a given Binding.
33: * Returns null if unknown. */
34: public Environment getEnvironment(Binding binding) {
35: return null;
36: }
37:
38: protected final static Object getValue(Binding binding) {
39: return binding.value;
40: }
41:
42: protected final static void setValue(Binding binding, Object value) {
43: binding.value = value;
44: }
45:
46: protected final static Constraint getConstraint(Binding binding) {
47: return binding.constraint;
48: }
49:
50: protected final static void setConstraint(Binding binding,
51: Constraint constraint) {
52: binding.constraint = constraint;
53: }
54:
55: /** Get value of "function binding" of a Binding.
56: * Some languages (including Common Lisp and Emacs Lisp) associate both
57: * a value binding and a function binding with a symbol.
58: * @return the function value, or Binding.UNBOUND if no function binding.
59: */
60: public Object getFunctionValue(Binding binding) {
61: return binding.getProperty(Binding.FUNCTION, Binding.UNBOUND);
62: }
63:
64: public void setFunctionValue(Binding binding, Object value) {
65: if (value == Binding.UNBOUND)
66: binding.removeProperty(Binding.FUNCTION);
67: else
68: binding.setProperty(Binding.FUNCTION, value);
69: }
70: }
|