added StatSet.updateStat(), which uses a StatModifier to do its business

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@681 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Tom Conkling
2008-07-25 20:55:14 +00:00
parent 41be22fdd6
commit bc38d2dde9
2 changed files with 35 additions and 3 deletions
@@ -53,6 +53,6 @@ public class MaxValueIntStat extends IntStat
_maxValue = in.readInt();
}
/** The largest value ever accumulated to this stat. */
/** The largest value that this stat has been assigned. */
protected int _maxValue;
}
@@ -5,6 +5,7 @@ package com.threerings.stats.data;
import java.util.Iterator;
import com.threerings.media.Log;
import com.threerings.presents.dobj.DSet;
/**
@@ -40,6 +41,24 @@ public final class StatSet extends DSet<Stat>
_container = container;
}
/**
* Updates a stat in this set, using the supplied StatModifier.
*/
@SuppressWarnings("unchecked")
public <T extends Stat> void updateStat (StatModifier<T> modifier)
{
Stat stat = getStat(modifier.getType());
if (stat != null) {
modifier.modify((T)stat);
updateStat(stat);
} else {
stat = modifier.getType().newStat();
modifier.modify((T)stat);
addStat(stat);
}
}
/**
* Sets an integer statistic in this set.
*
@@ -227,6 +246,8 @@ public final class StatSet extends DSet<Stat>
} else {
add(stat);
}
Log.log.info("StatSet.addStat()", "Stat", stat);
}
protected void updateStat (Stat stat)
@@ -234,6 +255,17 @@ public final class StatSet extends DSet<Stat>
if (_container != null) {
_container.updateStats(stat);
}
Log.log.info("StatSet.updateStat()", "Stat", stat);
}
/**
* Returns the Stat of the given type, if it exists in the set, and null otherwise.
*/
protected Stat getStat (Stat.Type type)
{
Stat stat = get(type.name());
return (stat != null ? stat : null);
}
protected transient Container _container;