1 | |
package com.sun.javafx.runtime.location; |
2 | |
|
3 | |
import com.sun.javafx.runtime.BindingException; |
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | 175 | public abstract class AbstractVariable<T_VALUE, T_LOCATION extends ObjectLocation<T_VALUE>, T_BINDING extends AbstractBindingExpression> |
11 | |
extends AbstractLocation |
12 | |
implements ObjectLocation<T_VALUE>, BindableLocation<T_VALUE, T_BINDING> { |
13 | |
|
14 | |
protected T_BINDING binding; |
15 | |
protected boolean isLazy, everInitialized, everValid; |
16 | |
protected DeferredInitializer deferredLiteral; |
17 | |
|
18 | 10211 | protected AbstractVariable() { } |
19 | |
|
20 | |
protected void setInitialized() { |
21 | 30638 | everInitialized = true; |
22 | 30638 | } |
23 | |
|
24 | |
public boolean isInitialized() { |
25 | 16983 | return everInitialized; |
26 | |
} |
27 | |
|
28 | |
protected boolean isEverValid() { |
29 | 6727 | return everValid; |
30 | |
} |
31 | |
|
32 | |
protected void setValid() { |
33 | 27605 | super.setValid(); |
34 | 27605 | everValid = true; |
35 | 27605 | setInitialized(); |
36 | 27605 | } |
37 | |
|
38 | |
protected void ensureBindable() { |
39 | 3100 | if (isBound()) |
40 | 2 | throw new BindingException("Cannot rebind variable"); |
41 | |
|
42 | |
|
43 | |
|
44 | 3098 | } |
45 | |
|
46 | |
public void bijectiveBind(ObjectLocation<T_VALUE> other) { |
47 | 10 | ensureBindable(); |
48 | 10 | super.invalidate(); |
49 | 10 | setInitialized(); |
50 | 10 | Bindings.bijectiveBind(this, other); |
51 | 10 | } |
52 | |
|
53 | |
public void bijectiveBindFromLiteral(final ObjectLocation<T_VALUE> other) { |
54 | 2 | deferredLiteral = new DeferredInitializer() { |
55 | |
public void apply() { |
56 | 2 | bijectiveBind(other); |
57 | 2 | } |
58 | |
}; |
59 | 2 | } |
60 | |
|
61 | |
protected abstract T_BINDING makeBindingExpression(T_LOCATION location); |
62 | |
|
63 | |
public void bind(T_LOCATION otherLocation) { |
64 | 667 | bind(false, makeBindingExpression(otherLocation), otherLocation); |
65 | 667 | } |
66 | |
|
67 | |
public void bindFromLiteral(final T_LOCATION otherLocation) { |
68 | 86 | deferredLiteral = new DeferredInitializer() { |
69 | |
public void apply() { |
70 | 86 | bind(otherLocation); |
71 | 86 | } |
72 | |
}; |
73 | 86 | } |
74 | |
|
75 | |
public void bind(boolean lazy, T_BINDING binding, Location... dependencies) { |
76 | 3025 | ensureBindable(); |
77 | 3023 | super.invalidate(); |
78 | 3023 | setInitialized(); |
79 | 3023 | this.binding = binding; |
80 | 3023 | binding.setLocation(this); |
81 | 3023 | isLazy = lazy; |
82 | 3023 | addDependencies(dependencies); |
83 | 3023 | if (!isLazy) |
84 | 2514 | update(); |
85 | 3023 | } |
86 | |
|
87 | |
public void bindFromLiteral(final boolean lazy, final T_BINDING binding, final Location... dependencies) { |
88 | 0 | deferredLiteral = new DeferredInitializer() { |
89 | |
public void apply() { |
90 | 0 | bind(lazy, binding, dependencies); |
91 | 0 | } |
92 | |
}; |
93 | 0 | } |
94 | |
|
95 | |
public T_VALUE setFromLiteral(final T_VALUE value) { |
96 | 402 | deferredLiteral = new DeferredInitializer() { |
97 | |
public void apply() { |
98 | 402 | set(value); |
99 | 402 | } |
100 | |
}; |
101 | 402 | return value; |
102 | |
} |
103 | |
|
104 | |
public boolean isBound() { |
105 | 92438 | return binding != null; |
106 | |
} |
107 | |
|
108 | |
public boolean isLazy() { |
109 | 3428 | return isBound() && isLazy; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
public boolean needDefault() { |
115 | 5189 | if (deferredLiteral != null) { |
116 | 2778 | deferredLiteral.apply(); |
117 | 2778 | deferredLiteral = null; |
118 | 2778 | return false; |
119 | |
} |
120 | |
else |
121 | 2411 | return !isInitialized(); |
122 | |
} |
123 | |
|
124 | |
public void initialize() { |
125 | |
|
126 | 5751 | assert(deferredLiteral == null); |
127 | 5751 | deferredLiteral = null; |
128 | 5751 | if (isBound() && !isLazy()) |
129 | 594 | update(); |
130 | 5751 | } |
131 | |
|
132 | |
@Override |
133 | |
public void invalidate() { |
134 | 2832 | if (isBound()) { |
135 | 2831 | super.invalidate(); |
136 | 2831 | if (!isLazy()) |
137 | 2244 | update(); |
138 | |
} |
139 | |
else |
140 | 1 | throw new BindingException("Cannot invalidate non-bound variable"); |
141 | 2831 | } |
142 | |
|
143 | |
public boolean isMutable() { |
144 | 60 | return !isBound(); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
interface DeferredInitializer { |
149 | |
public void apply(); |
150 | |
} |