1 | |
package com.sun.javafx.runtime.location; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import com.sun.javafx.runtime.AssignToBoundException; |
7 | |
import com.sun.javafx.runtime.ErrorHandler; |
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | 1001 | public class IntVariable extends AbstractVariable<Integer, IntLocation, IntBindingExpression> implements IntLocation { |
15 | |
public static final int DEFAULT = 0; |
16 | |
|
17 | 1311 | protected int $value = DEFAULT; |
18 | |
private List<IntChangeListener> replaceListeners; |
19 | |
|
20 | |
public static IntVariable make() { |
21 | 424 | return new IntVariable(); |
22 | |
} |
23 | |
|
24 | |
public static IntVariable make(int value) { |
25 | 713 | return new IntVariable(value); |
26 | |
} |
27 | |
|
28 | |
public static IntVariable make(boolean lazy, IntBindingExpression binding, Location... dependencies) { |
29 | 25 | return new IntVariable(lazy, binding, dependencies); |
30 | |
} |
31 | |
|
32 | |
public static IntVariable make(IntBindingExpression binding, Location... dependencies) { |
33 | 123 | return new IntVariable(false, binding, dependencies); |
34 | |
} |
35 | |
|
36 | |
|
37 | |
public static IntVariable makeBijective(ObjectVariable<Integer> other) { |
38 | 0 | IntVariable me = IntVariable.make(); |
39 | 0 | me.bijectiveBind(other); |
40 | 0 | return me; |
41 | |
} |
42 | |
|
43 | 2622 | protected IntVariable() { } |
44 | |
|
45 | |
protected IntVariable(int value) { |
46 | 713 | this(); |
47 | 713 | $value = value; |
48 | 713 | setValid(); |
49 | 713 | } |
50 | |
|
51 | |
protected IntVariable(boolean lazy, IntBindingExpression binding, Location... dependencies) { |
52 | 148 | this(); |
53 | 148 | bind(lazy, binding); |
54 | 148 | addDependencies(dependencies); |
55 | 148 | } |
56 | |
|
57 | |
public int getAsInt() { |
58 | 12430 | if (isBound() && !isValid()) |
59 | 137 | update(); |
60 | 12430 | return $value; |
61 | |
} |
62 | |
|
63 | |
public Integer get() { |
64 | 849 | return getAsInt(); |
65 | |
} |
66 | |
|
67 | |
public boolean isNull() { |
68 | 0 | return false; |
69 | |
} |
70 | |
|
71 | |
protected int replaceValue(int newValue) { |
72 | 8275 | int oldValue = $value; |
73 | 8275 | if (oldValue != newValue || !isInitialized() || !isEverValid()) { |
74 | 8170 | boolean notifyDependencies = isValid() || !isInitialized() || !isEverValid(); |
75 | 8170 | $value = newValue; |
76 | 8170 | setValid(); |
77 | 8170 | notifyListeners(oldValue, newValue, notifyDependencies); |
78 | 8170 | } |
79 | |
else |
80 | 105 | setValid(); |
81 | 8275 | return newValue; |
82 | |
} |
83 | |
|
84 | |
protected IntBindingExpression makeBindingExpression(final IntLocation otherLocation) { |
85 | 41 | return new IntBindingExpression() { |
86 | |
public int computeValue() { |
87 | 134 | return otherLocation.getAsInt(); |
88 | |
} |
89 | |
}; |
90 | |
} |
91 | |
|
92 | |
public int setAsInt(int value) { |
93 | 7757 | if (isBound() && $value != value) |
94 | 6 | throw new AssignToBoundException("Cannot assign to bound variable"); |
95 | 7751 | return replaceValue(value); |
96 | |
} |
97 | |
|
98 | |
public int setAsIntFromLiteral(final int value) { |
99 | 149 | deferredLiteral = new DeferredInitializer() { |
100 | |
public void apply() { |
101 | 149 | setAsInt(value); |
102 | 149 | } |
103 | |
}; |
104 | 149 | return value; |
105 | |
} |
106 | |
|
107 | |
public void setDefault() { |
108 | 0 | setAsInt(DEFAULT); |
109 | 0 | } |
110 | |
|
111 | |
public Integer set(Integer value) { |
112 | 165 | if (value == null) { |
113 | 0 | ErrorHandler.nullToPrimitiveCoercion("Integer"); |
114 | 0 | setDefault(); |
115 | |
} |
116 | |
else |
117 | 165 | setAsInt(value); |
118 | 162 | return value; |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public void update() { |
123 | |
try { |
124 | 670 | if (isBound() && !isValid()) |
125 | 524 | replaceValue(binding.computeValue()); |
126 | |
} |
127 | 1 | catch (RuntimeException e) { |
128 | 1 | ErrorHandler.bindException(e); |
129 | 1 | if (isInitialized()) |
130 | 1 | replaceValue(DEFAULT); |
131 | 669 | } |
132 | 670 | } |
133 | |
|
134 | |
public void addChangeListener(IntChangeListener listener) { |
135 | 599 | if (replaceListeners == null) |
136 | 463 | replaceListeners = new ArrayList<IntChangeListener>(); |
137 | 599 | replaceListeners.add(listener); |
138 | 599 | } |
139 | |
|
140 | |
public void addChangeListener(final ObjectChangeListener<Integer> listener) { |
141 | 466 | addChangeListener(new IntChangeListener() { |
142 | |
public void onChange(int oldValue, int newValue) { |
143 | 29 | listener.onChange(oldValue, newValue); |
144 | 29 | } |
145 | |
}); |
146 | 466 | } |
147 | |
|
148 | |
private void notifyListeners(int oldValue, int newValue, boolean notifyDependencies) { |
149 | 8170 | if (notifyDependencies) |
150 | 7931 | invalidateDependencies(); |
151 | 8170 | if (replaceListeners != null) { |
152 | 179 | for (IntChangeListener listener : replaceListeners) |
153 | 210 | listener.onChange(oldValue, newValue); |
154 | |
} |
155 | 8170 | } |
156 | |
} |