01: // Copyright (c) 2001 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.mapping;
05:
06: /** A constraint for an immutable binding. */
07:
08: public class ConstantConstraint extends Constraint {
09: Environment environment;
10:
11: static ConstantConstraint instance;
12:
13: public static ConstantConstraint getInstance(Environment environment) {
14: ConstantConstraint result;
15: if (environment != null) {
16: result = environment.constantConstraint;
17: if (result == null) {
18: result = new ConstantConstraint(environment);
19: environment.constantConstraint = result;
20: }
21: } else {
22: result = instance;
23: if (result == null) {
24: result = new ConstantConstraint(null);
25: instance = result;
26: }
27: }
28: return result;
29: }
30:
31: public static ConstantConstraint getInstance(Binding binding) {
32: Constraint constraint = binding.constraint;
33: if (constraint instanceof ConstantConstraint)
34: return (ConstantConstraint) binding.constraint;
35: return getInstance(constraint.getEnvironment(binding));
36: }
37:
38: public ConstantConstraint(Environment environment) {
39: this .environment = environment;
40: }
41:
42: public boolean isBound(Binding binding) {
43: return true;
44: }
45:
46: public Object get(Binding binding, Object defaultValue) {
47: return binding.value;
48: }
49:
50: public void set(Binding binding, Object value) {
51: if (binding.value == value)
52: return; // No change - ignore
53: throw new IllegalStateException(
54: "attempt to modify read-only variable: "
55: + binding.getName());
56: }
57:
58: public Environment getEnvironment(Binding binding) {
59: return environment;
60: }
61: }
|