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:
@@ -37,6 +37,25 @@ import com.threerings.crowd.util.CrowdContext;
|
|||||||
*/
|
*/
|
||||||
public abstract class PlaceController extends Controller
|
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
|
* Initializes this place controller with a reference to the context
|
||||||
* that they can use to access client services and to the
|
* that they can use to access client services and to the
|
||||||
@@ -60,7 +79,7 @@ public abstract class PlaceController extends Controller
|
|||||||
_view = createPlaceView(_ctx);
|
_view = createPlaceView(_ctx);
|
||||||
|
|
||||||
// initialize our delegates
|
// initialize our delegates
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
|
||||||
public void apply (PlaceControllerDelegate delegate) {
|
public void apply (PlaceControllerDelegate delegate) {
|
||||||
delegate.init(_ctx, _config);
|
delegate.init(_ctx, _config);
|
||||||
}
|
}
|
||||||
@@ -142,7 +161,7 @@ public abstract class PlaceController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
|
||||||
public void apply (PlaceControllerDelegate delegate) {
|
public void apply (PlaceControllerDelegate delegate) {
|
||||||
delegate.willEnterPlace(plobj);
|
delegate.willEnterPlace(plobj);
|
||||||
}
|
}
|
||||||
@@ -163,7 +182,7 @@ public abstract class PlaceController extends Controller
|
|||||||
public void mayLeavePlace (final PlaceObject plobj)
|
public void mayLeavePlace (final PlaceObject plobj)
|
||||||
{
|
{
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
|
||||||
public void apply (PlaceControllerDelegate delegate) {
|
public void apply (PlaceControllerDelegate delegate) {
|
||||||
delegate.mayLeavePlace(plobj);
|
delegate.mayLeavePlace(plobj);
|
||||||
}
|
}
|
||||||
@@ -180,7 +199,7 @@ public abstract class PlaceController extends Controller
|
|||||||
public void didLeavePlace (final PlaceObject plobj)
|
public void didLeavePlace (final PlaceObject plobj)
|
||||||
{
|
{
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
|
||||||
public void apply (PlaceControllerDelegate delegate) {
|
public void apply (PlaceControllerDelegate delegate) {
|
||||||
delegate.didLeavePlace(plobj);
|
delegate.didLeavePlace(plobj);
|
||||||
}
|
}
|
||||||
@@ -206,7 +225,7 @@ public abstract class PlaceController extends Controller
|
|||||||
final boolean[] handled = new boolean[1];
|
final boolean[] handled = new boolean[1];
|
||||||
|
|
||||||
// let our delegates have a crack at the action
|
// let our delegates have a crack at the action
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceControllerDelegate.class) {
|
||||||
public void apply (PlaceControllerDelegate delegate) {
|
public void apply (PlaceControllerDelegate delegate) {
|
||||||
// we take advantage of short-circuiting here
|
// we take advantage of short-circuiting here
|
||||||
handled[0] = handled[0] || delegate.handleAction(action);
|
handled[0] = handled[0] || delegate.handleAction(action);
|
||||||
@@ -222,59 +241,27 @@ public abstract class PlaceController extends Controller
|
|||||||
*/
|
*/
|
||||||
protected void addDelegate (PlaceControllerDelegate delegate)
|
protected void addDelegate (PlaceControllerDelegate delegate)
|
||||||
{
|
{
|
||||||
ratifyDelegate(delegate);
|
|
||||||
if (_delegates == null) {
|
if (_delegates == null) {
|
||||||
_delegates = new ArrayList<PlaceControllerDelegate>();
|
_delegates = new ArrayList<PlaceControllerDelegate>();
|
||||||
}
|
}
|
||||||
_delegates.add(delegate);
|
_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.
|
* Applies the supplied operation to the registered delegates.
|
||||||
*/
|
*/
|
||||||
protected void applyToDelegates (DelegateOp op)
|
protected void applyToDelegates (DelegateOp op)
|
||||||
{
|
{
|
||||||
if (_delegates != null) {
|
if (_delegates != null) {
|
||||||
int dcount = _delegates.size();
|
for (int ii = 0, ll = _delegates.size(); ii < ll; ii++) {
|
||||||
for (int i = 0; i < dcount; i++) {
|
PlaceControllerDelegate delegate = _delegates.get(ii);
|
||||||
op.apply(_delegates.get(i));
|
if (op.shouldApply(delegate)) {
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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())) {
|
|
||||||
op.apply(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. */
|
/** A reference to the active client context. */
|
||||||
protected CrowdContext _ctx;
|
protected CrowdContext _ctx;
|
||||||
|
|
||||||
|
|||||||
@@ -99,9 +99,20 @@ public class PlaceManager
|
|||||||
/**
|
/**
|
||||||
* Used to call methods on this place manager's delegates.
|
* 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)
|
public void applyToOccupants (OccupantOp op)
|
||||||
{
|
{
|
||||||
if (_plobj != null) {
|
if (_plobj != null) {
|
||||||
Iterator iter = _plobj.occupantInfo.iterator();
|
for (OccupantInfo info : _plobj.occupantInfo) {
|
||||||
while (iter.hasNext()) {
|
op.apply(info);
|
||||||
op.apply((OccupantInfo)iter.next());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -183,7 +193,7 @@ public class PlaceManager
|
|||||||
_config = config;
|
_config = config;
|
||||||
|
|
||||||
// initialize our delegates
|
// initialize our delegates
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.init(PlaceManager.this, _omgr, _invmgr);
|
delegate.init(PlaceManager.this, _omgr, _invmgr);
|
||||||
}
|
}
|
||||||
@@ -198,7 +208,6 @@ public class PlaceManager
|
|||||||
*/
|
*/
|
||||||
public void addDelegate (PlaceManagerDelegate delegate)
|
public void addDelegate (PlaceManagerDelegate delegate)
|
||||||
{
|
{
|
||||||
ratifyDelegate(delegate);
|
|
||||||
if (_delegates == null) {
|
if (_delegates == null) {
|
||||||
_delegates = Lists.newArrayList();
|
_delegates = Lists.newArrayList();
|
||||||
}
|
}
|
||||||
@@ -215,9 +224,11 @@ public class PlaceManager
|
|||||||
public void applyToDelegates (DelegateOp op)
|
public void applyToDelegates (DelegateOp op)
|
||||||
{
|
{
|
||||||
if (_delegates != null) {
|
if (_delegates != null) {
|
||||||
int dcount = _delegates.size();
|
for (int ii = 0, ll = _delegates.size(); ii < ll; ii++) {
|
||||||
for (int i = 0; i < dcount; i++) {
|
PlaceManagerDelegate delegate = _delegates.get(ii);
|
||||||
op.apply(_delegates.get(i));
|
if (op.shouldApply(delegate)) {
|
||||||
|
op.apply(delegate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -472,7 +483,7 @@ public class PlaceManager
|
|||||||
protected void didInit ()
|
protected void didInit ()
|
||||||
{
|
{
|
||||||
// initialize our delegates
|
// initialize our delegates
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.didInit(_config);
|
delegate.didInit(_config);
|
||||||
}
|
}
|
||||||
@@ -513,7 +524,7 @@ public class PlaceManager
|
|||||||
protected void didStartup ()
|
protected void didStartup ()
|
||||||
{
|
{
|
||||||
// let our delegates know that we've started up
|
// let our delegates know that we've started up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.didStartup(_plobj);
|
delegate.didStartup(_plobj);
|
||||||
}
|
}
|
||||||
@@ -532,7 +543,7 @@ public class PlaceManager
|
|||||||
_plobj.removeListener(_bodyUpdater);
|
_plobj.removeListener(_bodyUpdater);
|
||||||
|
|
||||||
// let our delegates know that we've shut down
|
// let our delegates know that we've shut down
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.didShutdown();
|
delegate.didShutdown();
|
||||||
}
|
}
|
||||||
@@ -557,7 +568,7 @@ public class PlaceManager
|
|||||||
log.debug("Body entered", "where", where(), "oid", bodyOid);
|
log.debug("Body entered", "where", where(), "oid", bodyOid);
|
||||||
|
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.bodyEntered(bodyOid);
|
delegate.bodyEntered(bodyOid);
|
||||||
}
|
}
|
||||||
@@ -585,7 +596,7 @@ public class PlaceManager
|
|||||||
OccupantInfo leaver = _occInfo.remove(bodyOid);
|
OccupantInfo leaver = _occInfo.remove(bodyOid);
|
||||||
|
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.bodyLeft(bodyOid);
|
delegate.bodyLeft(bodyOid);
|
||||||
}
|
}
|
||||||
@@ -611,7 +622,7 @@ public class PlaceManager
|
|||||||
protected void bodyUpdated (final OccupantInfo info)
|
protected void bodyUpdated (final OccupantInfo info)
|
||||||
{
|
{
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.bodyUpdated(info);
|
delegate.bodyUpdated(info);
|
||||||
}
|
}
|
||||||
@@ -626,7 +637,7 @@ public class PlaceManager
|
|||||||
protected void placeBecameEmpty ()
|
protected void placeBecameEmpty ()
|
||||||
{
|
{
|
||||||
// let our delegates know what's up
|
// let our delegates know what's up
|
||||||
applyToDelegates(new DelegateOp() {
|
applyToDelegates(new DelegateOp(PlaceManagerDelegate.class) {
|
||||||
public void apply (PlaceManagerDelegate delegate) {
|
public void apply (PlaceManagerDelegate delegate) {
|
||||||
delegate.placeBecameEmpty();
|
delegate.placeBecameEmpty();
|
||||||
}
|
}
|
||||||
@@ -676,14 +687,6 @@ public class PlaceManager
|
|||||||
return 5 * 60 * 1000L;
|
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
|
* 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.
|
* sure to call super) and append your info to the buffer.
|
||||||
|
|||||||
Reference in New Issue
Block a user