I decided to go hog wild and clean up all the type use in Presents which

required some serious bending and folding of the generic type system, but for
the most part we managed to avoid any mutilating. The gendobj task now
generates properly typed "addToXXX" and "updateXXX" DSet methods based on the
parameterized type of the DSet. This might cause unrecompiled code to break,
but I don't think there are many cases in the base toolkit where people call
DSet adders or updaters. We'll see and I'll add backwards compatibility
versions for cases where we need them to support GG games (everything else we
can just recompile).


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4245 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2006-07-05 00:55:05 +00:00
parent 94b79826d4
commit 8afd0316ec
27 changed files with 269 additions and 212 deletions
@@ -56,7 +56,7 @@ public class CompoundEvent extends DEvent
_omgr = omgr;
_target = target;
_events = new StreamableArrayList();
_events = new StreamableArrayList<DEvent>();
}
/**
@@ -73,7 +73,7 @@ public class CompoundEvent extends DEvent
* Returns the list of events contained within this compound event.
* Don't mess with it.
*/
public List getEvents ()
public List<DEvent> getEvents ()
{
return _events;
}
@@ -95,7 +95,7 @@ public class CompoundEvent extends DEvent
case 0: // nothing doing
break;
case 1: // no point in being compound
_omgr.postEvent((DEvent)_events.get(0));
_omgr.postEvent(_events.get(0));
break;
default: // now we're talking
_omgr.postEvent(this);
@@ -124,7 +124,7 @@ public class CompoundEvent extends DEvent
int ecount = _events.size();
for (int i = 0; i < ecount; i++) {
((DEvent)_events.get(i)).setSourceOid(sourceOid);
_events.get(i).setSourceOid(sourceOid);
}
}
@@ -166,5 +166,5 @@ public class CompoundEvent extends DEvent
protected transient DObject _target;
/** A list of the events associated with this compound event. */
protected StreamableArrayList _events;
protected StreamableArrayList<DEvent> _events;
}
@@ -127,7 +127,7 @@ public class DObject
{
public DObject ()
{
_fields = (Field[])_ftable.get(getClass());
_fields = _ftable.get(getClass());
if (_fields == null) {
_fields = getClass().getFields();
Arrays.sort(_fields, FIELD_COMP);
@@ -280,10 +280,12 @@ public class DObject
/**
* Get the DSet with the specified name.
*/
public final DSet getSet (String setName)
public final <T extends DSet.Entry> DSet<T> getSet (String setName)
{
try {
return (DSet) getField(setName).get(this);
@SuppressWarnings("unchecked") DSet<T> casted =
(DSet<T>)getField(setName).get(this);
return casted;
} catch (Exception e) {
throw new IllegalArgumentException("No such set: " + setName);
}
@@ -292,7 +294,7 @@ public class DObject
/**
* Request to have the specified item added to the specified DSet.
*/
public void addToSet (String setName, DSet.Entry entry)
public <T extends DSet.Entry> void addToSet (String setName, T entry)
{
requestEntryAdd(setName, getSet(setName), entry);
}
@@ -844,7 +846,8 @@ public class DObject
/**
* Calls by derived instances when a set adder method was called.
*/
protected void requestEntryAdd (String name, DSet set, DSet.Entry entry)
protected <T extends DSet.Entry> void requestEntryAdd (
String name, DSet<T> set, T entry)
{
// if we're on the authoritative server, we update the set immediately
boolean alreadyApplied = false;
@@ -855,16 +858,17 @@ public class DObject
alreadyApplied = true;
}
// dispatch an entry added event
postEvent(new EntryAddedEvent(_oid, name, entry, alreadyApplied));
postEvent(new EntryAddedEvent<T>(_oid, name, entry, alreadyApplied));
}
/**
* Calls by derived instances when a set remover method was called.
*/
protected void requestEntryRemove (String name, DSet set, Comparable key)
protected <T extends DSet.Entry> void requestEntryRemove (
String name, DSet<T> set, Comparable key)
{
// if we're on the authoritative server, we update the set immediately
DSet.Entry oldEntry = null;
T oldEntry = null;
if (_omgr != null && _omgr.isManager(this)) {
oldEntry = set.removeKey(key);
if (oldEntry == null) {
@@ -872,16 +876,17 @@ public class DObject
}
}
// dispatch an entry removed event
postEvent(new EntryRemovedEvent(_oid, name, key, oldEntry));
postEvent(new EntryRemovedEvent<T>(_oid, name, key, oldEntry));
}
/**
* Calls by derived instances when a set updater method was called.
*/
protected void requestEntryUpdate (String name, DSet set, DSet.Entry entry)
protected <T extends DSet.Entry> void requestEntryUpdate (
String name, DSet<T> set, T entry)
{
// if we're on the authoritative server, we update the set immediately
DSet.Entry oldEntry = null;
T oldEntry = null;
if (_omgr != null && _omgr.isManager(this)) {
oldEntry = set.update(entry);
if (oldEntry == null) {
@@ -889,7 +894,7 @@ public class DObject
}
}
// dispatch an entry updated event
postEvent(new EntryUpdatedEvent(_oid, name, entry, oldEntry));
postEvent(new EntryUpdatedEvent<T>(_oid, name, entry, oldEntry));
}
/**
@@ -955,12 +960,14 @@ public class DObject
/** Maintains a mapping of sorted field arrays for each distributed
* object class. */
protected static HashMap _ftable = new HashMap();
protected static HashMap<Class,Field[]> _ftable =
new HashMap<Class,Field[]>();
/** Used to sort and search {@link #_fields}. */
protected static final Comparator FIELD_COMP = new Comparator() {
public int compare (Object o1, Object o2) {
return ((Field)o1).getName().compareTo(((Field)o2).getName());
protected static final Comparator<Field> FIELD_COMP =
new Comparator<Field>() {
public int compare (Field f1, Field f2) {
return f1.getName().compareTo(f2.getName());
}
};
}
@@ -239,7 +239,8 @@ public class DSet<E extends DSet.Entry>
*/
public Object[] toArray (Object[] array)
{
return toArray((E[])array);
@SuppressWarnings("unchecked") E[] casted = (E[])array;
return toArray(casted);
}
/**
@@ -32,7 +32,7 @@ import com.threerings.presents.Log;
*
* @see DObjectManager#postEvent
*/
public class EntryAddedEvent extends NamedEvent
public class EntryAddedEvent<T extends DSet.Entry> extends NamedEvent
{
/**
* Constructs a new entry added event on the specified target object
@@ -44,7 +44,7 @@ public class EntryAddedEvent extends NamedEvent
* entry.
* @param entry the entry to add to the set attribute.
*/
public EntryAddedEvent (int targetOid, String name, DSet.Entry entry)
public EntryAddedEvent (int targetOid, String name, T entry)
{
this(targetOid, name, entry, false);
}
@@ -53,7 +53,7 @@ public class EntryAddedEvent extends NamedEvent
* Used when the distributed object already added the entry before
* generating the event.
*/
public EntryAddedEvent (int targetOid, String name, DSet.Entry entry,
public EntryAddedEvent (int targetOid, String name, T entry,
boolean alreadyApplied)
{
super(targetOid, name);
@@ -72,7 +72,7 @@ public class EntryAddedEvent extends NamedEvent
/**
* Returns the entry that has been added.
*/
public DSet.Entry getEntry ()
public T getEntry ()
{
return _entry;
}
@@ -84,7 +84,7 @@ public class EntryAddedEvent extends NamedEvent
throws ObjectAccessException
{
if (!_alreadyApplied) {
boolean added = ((DSet)target.getAttribute(_name)).add(_entry);
boolean added = target.getSet(_name).add(_entry);
if (!added) {
Log.warning("Duplicate entry found [event=" + this + "].");
}
@@ -109,7 +109,7 @@ public class EntryAddedEvent extends NamedEvent
StringUtil.toString(buf, _entry);
}
protected DSet.Entry _entry;
protected T _entry;
/** Used when this event is generated on the authoritative server
* where object changes are made immediately. This lets us know not to
@@ -31,7 +31,7 @@ import com.threerings.presents.Log;
*
* @see DObjectManager#postEvent
*/
public class EntryRemovedEvent extends NamedEvent
public class EntryRemovedEvent<T extends DSet.Entry> extends NamedEvent
{
/**
* Constructs a new entry removed event on the specified target object
@@ -45,7 +45,7 @@ public class EntryRemovedEvent extends NamedEvent
* @param oldEntry the previous value of the entry.
*/
public EntryRemovedEvent (int targetOid, String name, Comparable key,
DSet.Entry oldEntry)
T oldEntry)
{
super(targetOid, name);
_key = key;
@@ -65,13 +65,13 @@ public class EntryRemovedEvent extends NamedEvent
*/
public Comparable getKey ()
{
return (Comparable)_key;
return _key;
}
/**
* Returns the entry that was in the set prior to being updated.
*/
public DSet.Entry getOldEntry ()
public T getOldEntry ()
{
return _oldEntry;
}
@@ -83,7 +83,7 @@ public class EntryRemovedEvent extends NamedEvent
throws ObjectAccessException
{
if (_oldEntry == UNSET_OLD_ENTRY) {
DSet set = (DSet)target.getAttribute(_name);
DSet<T> set = target.getSet(_name);
// remove, fetch the previous value for interested callers
_oldEntry = set.removeKey(_key);
if (_oldEntry == null) {
@@ -113,5 +113,6 @@ public class EntryRemovedEvent extends NamedEvent
}
protected Comparable _key;
protected transient DSet.Entry _oldEntry = UNSET_OLD_ENTRY;
@SuppressWarnings("unchecked")
protected transient T _oldEntry = (T)UNSET_OLD_ENTRY;
}
@@ -32,7 +32,7 @@ import com.threerings.presents.Log;
*
* @see DObjectManager#postEvent
*/
public class EntryUpdatedEvent extends NamedEvent
public class EntryUpdatedEvent<T extends DSet.Entry> extends NamedEvent
{
/**
* Constructs a new entry updated event on the specified target object
@@ -45,8 +45,7 @@ public class EntryUpdatedEvent extends NamedEvent
* @param entry the entry to update.
* @param oldEntry the previous value of the entry.
*/
public EntryUpdatedEvent (int targetOid, String name, DSet.Entry entry,
DSet.Entry oldEntry)
public EntryUpdatedEvent (int targetOid, String name, T entry, T oldEntry)
{
super(targetOid, name);
_entry = entry;
@@ -64,7 +63,7 @@ public class EntryUpdatedEvent extends NamedEvent
/**
* Returns the entry that has been updated.
*/
public DSet.Entry getEntry ()
public T getEntry ()
{
return _entry;
}
@@ -72,7 +71,7 @@ public class EntryUpdatedEvent extends NamedEvent
/**
* Returns the entry that was in the set prior to being updated.
*/
public DSet.Entry getOldEntry ()
public T getOldEntry ()
{
return _oldEntry;
}
@@ -85,7 +84,7 @@ public class EntryUpdatedEvent extends NamedEvent
{
// only apply the change if we haven't already
if (_oldEntry == UNSET_OLD_ENTRY) {
DSet set = (DSet)target.getAttribute(_name);
DSet<T> set = target.getSet(_name);
// fetch the previous value for interested callers
_oldEntry = set.update(_entry);
if (_oldEntry == null) {
@@ -115,6 +114,7 @@ public class EntryUpdatedEvent extends NamedEvent
StringUtil.toString(buf, _entry);
}
protected DSet.Entry _entry;
protected transient DSet.Entry _oldEntry = UNSET_OLD_ENTRY;
protected T _entry;
@SuppressWarnings("unchecked")
protected transient T _oldEntry = (T)UNSET_OLD_ENTRY;
}