Execute-Around-Method (factoring out Allocate-Use-Release).
- By Alan Griffiths
- February 24, 2001
public class ExecuteAroundMethod {
private ListSelectionModel sel;
private interface Adjustment {
public void
use(ListSelectionModel sel);
}
private void
executeAround(
Adjustment adjustment) {
// Consider the 'lock' on
// notifications as a
// 'resource'
sel. setValueIsAdjusting(
true);
try {
adjustment.use(sel);
}
finally {
sel.setValueIsAdjusting(
false);
}
}
public void removeOdd() {
executeAround(
new Adjustment() {
public void
use(ListSelectionModel s) {
for (int i = entries;
--i != 0;)
if (0 != i % 2)
s.removeIndexInterval(
i, i);
}
});
}
}