Minna daisuki type safety.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4075 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2006-04-28 18:29:46 +00:00
parent ecee4e292e
commit b2896dc7ff
3 changed files with 49 additions and 45 deletions
@@ -1,5 +1,5 @@
// //
// $Id: DObjectManager.java,v 1.13 2004/08/27 02:20:20 mdb Exp $ // $Id$
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -53,7 +53,8 @@ public interface DObjectManager
* created and available, or if there was a problem creating the * created and available, or if there was a problem creating the
* object. * object.
*/ */
public void createObject (Class dclass, Subscriber target); public <T extends DObject> void createObject (
Class<T> dclass, Subscriber<T> target);
/** /**
* Requests that the specified subscriber be subscribed to the object * Requests that the specified subscriber be subscribed to the object
@@ -68,7 +69,8 @@ public interface DObjectManager
* @see Subscriber#objectAvailable * @see Subscriber#objectAvailable
* @see Subscriber#requestFailed * @see Subscriber#requestFailed
*/ */
public void subscribeToObject (int oid, Subscriber target); public <T extends DObject> void subscribeToObject (
int oid, Subscriber<T> target);
/** /**
* Requests that the specified subscriber be unsubscribed from the * Requests that the specified subscriber be unsubscribed from the
@@ -78,7 +80,8 @@ public interface DObjectManager
* unsubscription is desired. * unsubscription is desired.
* @param target The subscriber to be unsubscribed. * @param target The subscriber to be unsubscribed.
*/ */
public void unsubscribeFromObject (int oid, Subscriber target); public <T extends DObject> void unsubscribeFromObject (
int oid, Subscriber<T> target);
/** /**
* Requests that the specified object be destroyed. Once destroyed an * Requests that the specified object be destroyed. Once destroyed an
@@ -1,5 +1,5 @@
// //
// $Id: Subscriber.java,v 1.8 2004/08/27 02:20:20 mdb Exp $ // $Id$
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -38,7 +38,7 @@ package com.threerings.presents.dobj;
* @see SetListener * @see SetListener
* @see OidListListener * @see OidListListener
*/ */
public interface Subscriber public interface Subscriber<T extends DObject>
{ {
/** /**
* Called when a subscription request has succeeded and the object is * Called when a subscription request has succeeded and the object is
@@ -49,7 +49,7 @@ public interface Subscriber
* *
* @see DObjectManager#subscribeToObject * @see DObjectManager#subscribeToObject
*/ */
public void objectAvailable (DObject object); public void objectAvailable (T object);
/** /**
* Called when a subscription request has failed. The nature of the * Called when a subscription request has failed. The nature of the
@@ -117,31 +117,34 @@ public class PresentsDObjectMgr
} }
// inherit documentation from the interface // inherit documentation from the interface
public void createObject (Class dclass, Subscriber target) public <T extends DObject> void createObject (
Class<T> dclass, Subscriber<T> target)
{ {
// queue up a create object event // queue up a create object event
postEvent(new CreateObjectEvent(dclass, target)); postEvent(new CreateObjectEvent<T>(dclass, target));
} }
// inherit documentation from the interface // inherit documentation from the interface
public void subscribeToObject (int oid, Subscriber target) public <T extends DObject> void subscribeToObject (
int oid, Subscriber<T> target)
{ {
if (oid <= 0) { if (oid <= 0) {
target.requestFailed( target.requestFailed(
oid, new ObjectAccessException("Invalid oid " + oid + ".")); oid, new ObjectAccessException("Invalid oid " + oid + "."));
} else { } else {
// queue up an access object event // queue up an access object event
postEvent(new AccessObjectEvent( postEvent(new AccessObjectEvent<T>(
oid, target, AccessObjectEvent.SUBSCRIBE)); oid, target, AccessObjectEvent.SUBSCRIBE));
} }
} }
// inherit documentation from the interface // inherit documentation from the interface
public void unsubscribeFromObject (int oid, Subscriber target) public <T extends DObject> void unsubscribeFromObject (
int oid, Subscriber<T> target)
{ {
// queue up an access object event // queue up an access object event
postEvent(new AccessObjectEvent(oid, target, postEvent(new AccessObjectEvent<T>(
AccessObjectEvent.UNSUBSCRIBE)); oid, target, AccessObjectEvent.UNSUBSCRIBE));
} }
// inherit documentation from the interface // inherit documentation from the interface
@@ -316,7 +319,7 @@ public class PresentsDObjectMgr
} else { } else {
cname = StringUtil.shortClassName(unit); cname = StringUtil.shortClassName(unit);
} }
UnitProfile uprof = (UnitProfile)_profiles.get(cname); UnitProfile uprof = _profiles.get(cname);
if (uprof == null) { if (uprof == null) {
_profiles.put(cname, uprof = new UnitProfile()); _profiles.put(cname, uprof = new UnitProfile());
} }
@@ -399,7 +402,7 @@ public class PresentsDObjectMgr
boolean notify = true; // assume always notify boolean notify = true; // assume always notify
try { try {
// do any internal management necessary based on this event // do any internal management necessary based on this event
Method helper = (Method)_helpers.get(event.getClass()); Method helper = _helpers.get(event.getClass());
if (helper != null) { if (helper != null) {
// invoke the helper method // invoke the helper method
Object rv = helper.invoke(this, new Object[] { event, target }); Object rv = helper.invoke(this, new Object[] { event, target });
@@ -466,11 +469,8 @@ public class PresentsDObjectMgr
*/ */
public void dumpUnitProfiles () public void dumpUnitProfiles ()
{ {
for (Iterator itr = _profiles.entrySet().iterator(); itr.hasNext(); ) { for (Map.Entry<String,UnitProfile> entry : _profiles.entrySet()) {
Map.Entry entry = (Map.Entry) itr.next(); Log.info("P: " + entry.getKey() + " => " + entry.getValue());
String cname = (String) entry.getKey();
UnitProfile uprof = (UnitProfile) entry.getValue();
Log.info("P: " + cname + " => " + uprof);
} }
} }
@@ -495,7 +495,7 @@ public class PresentsDObjectMgr
target.setManager(null); target.setManager(null);
// deal with any remaining oid lists that reference this object // deal with any remaining oid lists that reference this object
Reference[] refs = (Reference[])_refs.remove(oid); Reference[] refs = _refs.remove(oid);
if (refs != null) { if (refs != null) {
for (int i = 0; i < refs.length; i++) { for (int i = 0; i < refs.length; i++) {
// skip empty spots // skip empty spots
@@ -564,7 +564,7 @@ public class PresentsDObjectMgr
DObject reffer, String field, int reffedOid) DObject reffer, String field, int reffedOid)
{ {
// look up the reference vector for the referenced object // look up the reference vector for the referenced object
Reference[] refs = (Reference[])_refs.get(reffedOid); Reference[] refs = _refs.get(reffedOid);
Reference ref = null; Reference ref = null;
if (refs != null) { if (refs != null) {
@@ -617,7 +617,7 @@ public class PresentsDObjectMgr
// get the reference vector for the referenced object. we use bare // get the reference vector for the referenced object. we use bare
// arrays rather than something like an array list to conserve // arrays rather than something like an array list to conserve
// memory. there will be many objects and references // memory. there will be many objects and references
Reference[] refs = (Reference[])_refs.get(oid); Reference[] refs = _refs.get(oid);
if (refs == null) { if (refs == null) {
refs = new Reference[DEFREFVEC_SIZE]; refs = new Reference[DEFREFVEC_SIZE];
_refs.put(oid, refs); _refs.put(oid, refs);
@@ -669,7 +669,7 @@ public class PresentsDObjectMgr
// ", roid=" + toid + "]."); // ", roid=" + toid + "].");
// get the reference vector for the referenced object // get the reference vector for the referenced object
Reference[] refs = (Reference[])_refs.get(oid); Reference[] refs = _refs.get(oid);
if (refs == null) { if (refs == null) {
// this can happen normally when an object is destroyed. it // this can happen normally when an object is destroyed. it
// will remove itself from the reference system and then // will remove itself from the reference system and then
@@ -731,11 +731,9 @@ public class PresentsDObjectMgr
report.append("- Unit profiles: ").append(_profiles.size()); report.append("- Unit profiles: ").append(_profiles.size());
report.append("\n"); report.append("\n");
for (Iterator itr = _profiles.entrySet().iterator(); itr.hasNext(); ) { for (Map.Entry<String,UnitProfile> entry : _profiles.entrySet()) {
Map.Entry entry = (Map.Entry) itr.next(); report.append(" ").append(entry.getKey());
String cname = (String) entry.getKey(); report.append(" ").append(entry.getValue());
UnitProfile uprof = (UnitProfile) entry.getValue();
report.append(" ").append(cname).append(" ").append(uprof);
report.append("\n"); report.append("\n");
} }
} }
@@ -744,7 +742,8 @@ public class PresentsDObjectMgr
* Calls {@link Subscriber#objectAvailable} and catches and logs any * Calls {@link Subscriber#objectAvailable} and catches and logs any
* exception thrown by the subscriber during the call. * exception thrown by the subscriber during the call.
*/ */
protected static void informObjectAvailable (Subscriber sub, DObject obj) protected static <T extends DObject> void informObjectAvailable (
Subscriber<T> sub, T obj)
{ {
try { try {
sub.objectAvailable(obj); sub.objectAvailable(obj);
@@ -760,9 +759,9 @@ public class PresentsDObjectMgr
* Used to create a distributed object and register it with the * Used to create a distributed object and register it with the
* system. * system.
*/ */
protected class CreateObjectEvent extends DEvent protected class CreateObjectEvent<T extends DObject> extends DEvent
{ {
public CreateObjectEvent (Class clazz, Subscriber target) public CreateObjectEvent (Class<T> clazz, Subscriber<T> target)
{ {
super(0); // target the fake object super(0); // target the fake object
_class = clazz; _class = clazz;
@@ -778,11 +777,11 @@ public class PresentsDObjectMgr
throws ObjectAccessException throws ObjectAccessException
{ {
int oid = getNextOid(); int oid = getNextOid();
DObject obj = null; T obj = null;
try { try {
// create a new instance of this object // create a new instance of this object
obj = (DObject)_class.newInstance(); obj = _class.newInstance();
// initialize this object // initialize this object
obj.setOid(oid); obj.setOid(oid);
@@ -824,20 +823,20 @@ public class PresentsDObjectMgr
return false; return false;
} }
protected transient Class _class; protected transient Class<T> _class;
protected transient Subscriber _target; protected transient Subscriber<T> _target;
} }
/** /**
* Used to make an object available to a subscriber (with or without * Used to make an object available to a subscriber (with or without
* the associated subscription). * the associated subscription).
*/ */
protected class AccessObjectEvent extends DEvent protected class AccessObjectEvent<T extends DObject> extends DEvent
{ {
public static final int SUBSCRIBE = 0; public static final int SUBSCRIBE = 0;
public static final int UNSUBSCRIBE = 1; public static final int UNSUBSCRIBE = 1;
public AccessObjectEvent (int oid, Subscriber target, public AccessObjectEvent (int oid, Subscriber<T> target,
int action) int action)
{ {
super(0); // target the bogus object super(0); // target the bogus object
@@ -855,7 +854,7 @@ public class PresentsDObjectMgr
throws ObjectAccessException throws ObjectAccessException
{ {
// look up the target object // look up the target object
DObject obj = (DObject)_objects.get(_oid); T obj = (T)_objects.get(_oid);
// if we're unsubscribing, take care of that and get on out // if we're unsubscribing, take care of that and get on out
if (_action == UNSUBSCRIBE) { if (_action == UNSUBSCRIBE) {
@@ -890,7 +889,7 @@ public class PresentsDObjectMgr
} }
protected int _oid; protected int _oid;
protected Subscriber _target; protected Subscriber<T> _target;
protected int _action; protected int _action;
} }
@@ -986,7 +985,7 @@ public class PresentsDObjectMgr
protected Queue _evqueue = new Queue(); protected Queue _evqueue = new Queue();
/** The managed distributed objects table. */ /** The managed distributed objects table. */
protected HashIntMap _objects = new HashIntMap(); protected HashIntMap<DObject> _objects = new HashIntMap<DObject>();
/** Used to assign a unique oid to each distributed object. */ /** Used to assign a unique oid to each distributed object. */
protected int _nextOid = 0; protected int _nextOid = 0;
@@ -1000,7 +999,7 @@ public class PresentsDObjectMgr
protected Throttle _fatalThrottle = new Throttle(30, 60*1000L); protected Throttle _fatalThrottle = new Throttle(30, 60*1000L);
/** Used to track oid list references of distributed objects. */ /** Used to track oid list references of distributed objects. */
protected HashIntMap _refs = new HashIntMap(); protected HashIntMap<Reference[]> _refs = new HashIntMap<Reference[]>();
/** The default access controller to use when creating distributed /** The default access controller to use when creating distributed
* objects. */ * objects. */
@@ -1015,7 +1014,8 @@ public class PresentsDObjectMgr
protected Perf _timer = Perf.getPerf(); protected Perf _timer = Perf.getPerf();
/** Used to profile our events and runnable units. */ /** Used to profile our events and runnable units. */
protected HashMap _profiles = new HashMap(); protected HashMap<String,UnitProfile> _profiles =
new HashMap<String,UnitProfile>();
/** Used to track runtime statistics. */ /** Used to track runtime statistics. */
protected Stats _recent = new Stats(), _current = _recent; protected Stats _recent = new Stats(), _current = _recent;
@@ -1036,6 +1036,7 @@ public class PresentsDObjectMgr
* This table maps event classes to helper methods that perform some * This table maps event classes to helper methods that perform some
* additional processing for particular events. * additional processing for particular events.
*/ */
protected static HashMap _helpers = new HashMap(); protected static HashMap<Class,Method> _helpers =
new HashMap<Class,Method>();
static { registerEventHelpers(); } static { registerEventHelpers(); }
} }