On second thought, let's just type our delegate operations and only apply them

to delegates that can handle them. Turns out we already even had this option in
the PlaceController.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5224 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2008-07-10 13:36:52 +00:00
parent 950fb6c4aa
commit 531f0c9ffe
2 changed files with 55 additions and 65 deletions
@@ -37,6 +37,25 @@ import com.threerings.crowd.util.CrowdContext;
*/
public abstract class PlaceController extends Controller
{
/**
* Used to call methods in delegates.
*/
public static abstract class DelegateOp
{
public DelegateOp (Class<? extends PlaceControllerDelegate> delegateClass) {
_delegateClass = delegateClass;
}
/** Applies an operation to the supplied delegate. */
public abstract void apply (PlaceControllerDelegate delegate);
public boolean shouldApply (PlaceControllerDelegate delegate) {
return _delegateClass.isInstance(delegate);
}
protected Class<? extends PlaceControllerDelegate> _delegateClass;
}
/**
* Initializes this place controller with a reference to the context
* that they can use to access client services and to the
@@ -60,7 +79,7 @@ public abstract class PlaceController extends Controller
_view = createPlaceView(_ctx);
// initialize our delegates
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
public void apply (PlaceControllerDelegate delegate) {
delegate.init(_ctx, _config);
}
@@ -142,7 +161,7 @@ public abstract class PlaceController extends Controller
}
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
public void apply (PlaceControllerDelegate delegate) {
delegate.willEnterPlace(plobj);
}
@@ -163,7 +182,7 @@ public abstract class PlaceController extends Controller
public void mayLeavePlace (final PlaceObject plobj)
{
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
public void apply (PlaceControllerDelegate delegate) {
delegate.mayLeavePlace(plobj);
}
@@ -180,7 +199,7 @@ public abstract class PlaceController extends Controller
public void didLeavePlace (final PlaceObject plobj)
{
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
public void apply (PlaceControllerDelegate delegate) {
delegate.didLeavePlace(plobj);
}
@@ -206,7 +225,7 @@ public abstract class PlaceController extends Controller
final boolean[] handled = new boolean[1];
// let our delegates have a crack at the action
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
public void apply (PlaceControllerDelegate delegate) {
// we take advantage of short-circuiting here
handled[0] = handled[0] || delegate.handleAction(action);
@@ -222,59 +241,27 @@ public abstract class PlaceController extends Controller
*/
protected void addDelegate (PlaceControllerDelegate delegate)
{
ratifyDelegate(delegate);
if (_delegates == null) {
_delegates = new ArrayList<PlaceControllerDelegate>();
}
_delegates.add(delegate);
}
/**
* Used to call methods in delegates.
*/
protected static interface DelegateOp
{
public void apply (PlaceControllerDelegate delegate);
}
/**
* Applies the supplied operation to the registered delegates.
*/
protected void applyToDelegates (DelegateOp op)
{
if (_delegates != null) {
int dcount = _delegates.size();
for (int i = 0; i < dcount; i++) {
op.apply(_delegates.get(i));
}
}
}
/**
* Applies the supplied operation to the registered delegates that
* derive from the specified class.
*/
protected void applyToDelegates (Class<?> dclass, DelegateOp op)
{
if (_delegates != null) {
int dcount = _delegates.size();
for (int i = 0; i < dcount; i++) {
PlaceControllerDelegate delegate = _delegates.get(i);
if (dclass.isAssignableFrom(delegate.getClass())) {
for (int ii = 0, ll = _delegates.size(); ii < ll; ii++) {
PlaceControllerDelegate delegate = _delegates.get(ii);
if (op.shouldApply(delegate)) {
op.apply(delegate);
}
}
}
}
/**
* Protects against programmer error.
*/
protected void ratifyDelegate (PlaceControllerDelegate delegate)
{
// nothing to do here, all PlaceControllerDelegates are safe for us
}
/** A reference to the active client context. */
protected CrowdContext _ctx;
@@ -99,9 +99,20 @@ public class PlaceManager
/**
* Used to call methods on this place manager's delegates.
*/
public static interface DelegateOp
public static abstract class DelegateOp
{
public void apply (PlaceManagerDelegate delegate);
public DelegateOp (Class<? extends PlaceManagerDelegate> delegateClass) {
_delegateClass = delegateClass;
}
/** Applies an operation to the supplied delegate. */
public abstract void apply (PlaceManagerDelegate delegate);
public boolean shouldApply (PlaceManagerDelegate delegate) {
return _delegateClass.isInstance(delegate);
}
protected Class<? extends PlaceManagerDelegate> _delegateClass;
}
/**
@@ -143,9 +154,8 @@ public class PlaceManager
public void applyToOccupants (OccupantOp op)
{
if (_plobj != null) {
Iterator iter = _plobj.occupantInfo.iterator();
while (iter.hasNext()) {
op.apply((OccupantInfo)iter.next());
for (OccupantInfo info : _plobj.occupantInfo) {
op.apply(info);
}
}
}
@@ -183,7 +193,7 @@ public class PlaceManager
_config = config;
// initialize our delegates
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.init(PlaceManager.this, _omgr, _invmgr);
}
@@ -198,7 +208,6 @@ public class PlaceManager
*/
public void addDelegate (PlaceManagerDelegate delegate)
{
ratifyDelegate(delegate);
if (_delegates == null) {
_delegates = Lists.newArrayList();
}
@@ -215,9 +224,11 @@ public class PlaceManager
public void applyToDelegates (DelegateOp op)
{
if (_delegates != null) {
int dcount = _delegates.size();
for (int i = 0; i < dcount; i++) {
op.apply(_delegates.get(i));
for (int ii = 0, ll = _delegates.size(); ii < ll; ii++) {
PlaceManagerDelegate delegate = _delegates.get(ii);
if (op.shouldApply(delegate)) {
op.apply(delegate);
}
}
}
}
@@ -472,7 +483,7 @@ public class PlaceManager
protected void didInit ()
{
// initialize our delegates
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.didInit(_config);
}
@@ -513,7 +524,7 @@ public class PlaceManager
protected void didStartup ()
{
// let our delegates know that we've started up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.didStartup(_plobj);
}
@@ -532,7 +543,7 @@ public class PlaceManager
_plobj.removeListener(_bodyUpdater);
// let our delegates know that we've shut down
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.didShutdown();
}
@@ -557,7 +568,7 @@ public class PlaceManager
log.debug("Body entered", "where", where(), "oid", bodyOid);
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.bodyEntered(bodyOid);
}
@@ -585,7 +596,7 @@ public class PlaceManager
OccupantInfo leaver = _occInfo.remove(bodyOid);
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.bodyLeft(bodyOid);
}
@@ -611,7 +622,7 @@ public class PlaceManager
protected void bodyUpdated (final OccupantInfo info)
{
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.bodyUpdated(info);
}
@@ -626,7 +637,7 @@ public class PlaceManager
protected void placeBecameEmpty ()
{
// let our delegates know what's up
applyToDelegates(new DelegateOp() {
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
public void apply (PlaceManagerDelegate delegate) {
delegate.placeBecameEmpty();
}
@@ -676,14 +687,6 @@ public class PlaceManager
return 5 * 60 * 1000L;
}
/**
* Protects against programmer error.
*/
protected void ratifyDelegate (PlaceManagerDelegate delegate)
{
// nothing to do here, all PlaceManagerDelegates are safe for us
}
/**
* An extensible way to add to the string representation of this class. Override this (being
* sure to call super) and append your info to the buffer.