- Created SetStat<T> generic type

- IntSetStat and StringSetStat extend SetStat
- consolidated SetStat-related functions in StatSet, and added StatSet.getSetStatSize()
- she sells SetStat StatSets by the seashore

git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@683 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Tom Conkling
2008-07-25 22:48:13 +00:00
parent 2a18ff5f02
commit 6f8a1e0db0
4 changed files with 157 additions and 39 deletions
@@ -1,5 +1,23 @@
//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.stats.data;
@@ -13,7 +31,7 @@ import com.threerings.util.StreamableArrayIntSet;
/**
* Used to track a statistic comprised of a bounded set of integers.
*/
public class IntSetStat extends Stat
public class IntSetStat extends SetStat<Integer>
{
/**
* Constructs a new IntSetStat that will store up to 255 ints.
@@ -36,6 +54,7 @@ public class IntSetStat extends Stat
/**
* Returns the number of values stored in the set.
*/
@Override // from SetStat
public int size ()
{
return _intSet.size();
@@ -44,7 +63,8 @@ public class IntSetStat extends Stat
/**
* Returns true if the specified int is contained in this set.
*/
public boolean contains (int key)
@Override // from SetStat
public boolean contains (Integer key)
{
return _intSet.contains(key);
}
@@ -55,7 +75,8 @@ public class IntSetStat extends Stat
* @return true if the int was newly added, false if it was already contained in the set, or
* if the set is full.
*/
public boolean add (int key)
@Override // from SetStat
public boolean add (Integer key)
{
boolean modified = (_intSet.size() < _maxSize) && _intSet.add(key);
setModified(_modified || modified);
@@ -0,0 +1,44 @@
//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.stats.data;
public abstract class SetStat<T> extends Stat
{
/**
* Returns the number of elements in this set.
*/
public abstract int size ();
/**
* Adds the specified object to this set.
*
* @return true if the object was newly added, false if it was already
* contained in the set.
*/
public abstract boolean add (T key);
/**
* Returns true if the specified object is contained in this set.
*/
public abstract boolean contains (T key);
}
+59 -35
View File
@@ -1,5 +1,23 @@
//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.stats.data;
@@ -67,7 +85,7 @@ public final class StatSet extends DSet<Stat>
*/
public void setStat (Stat.Type type, int value)
{
IntStat stat = (IntStat)get(type.name());
IntStat stat = (IntStat)getStat(type);
if (stat == null) {
stat = (IntStat)type.newStat();
stat.setValue(value);
@@ -99,7 +117,7 @@ public final class StatSet extends DSet<Stat>
*/
public void incrementStat (Stat.Type type, int delta)
{
IntStat stat = (IntStat)get(type.name());
IntStat stat = (IntStat)getStat(type);
if (stat == null) {
stat = (IntStat)type.newStat();
stat.increment(delta);
@@ -117,7 +135,7 @@ public final class StatSet extends DSet<Stat>
*/
public void appendStat (Stat.Type type, int value)
{
IntArrayStat stat = (IntArrayStat)get(type.name());
IntArrayStat stat = (IntArrayStat)getStat(type);
if (stat == null) {
stat = (IntArrayStat)type.newStat();
stat.appendValue(value);
@@ -129,34 +147,17 @@ public final class StatSet extends DSet<Stat>
}
/**
* Adds a string value to a {@link StringSetStat}.
* Adds a value to a {@link SetStat}.
*
* @exception ClassCastException thrown if the registered type of the specified stat is not an
* {@link StringSetStat}.
* @exception ClassCastException thrown if the registered type of the specified stat is not
* an {@link SetStat} parameterized on the given type.
*/
public void addToSetStat (Stat.Type type, String value)
@SuppressWarnings("unchecked")
public <T> void addToSetStat (Stat.Type type, T value)
{
StringSetStat stat = (StringSetStat)get(type.name());
SetStat<T> stat = (SetStat<T>)getStat(type);
if (stat == null) {
stat = (StringSetStat)type.newStat();
stat.add(value);
addStat(stat);
} else if (stat.add(value)) {
updateStat(stat);
}
}
/**
* Adds an int value to a {@link IntSetStat}.
*
* @exception ClassCastException thrown if the registered type of the specified stat is not an
* {@link IntSetStat}.
*/
public void addToSetStat (Stat.Type type, int value)
{
IntSetStat stat = (IntSetStat)get(type.name());
if (stat == null) {
stat = (IntSetStat)type.newStat();
stat = (SetStat<T>)type.newStat();
stat.add(value);
addStat(stat);
} else if (stat.add(value)) {
@@ -172,7 +173,7 @@ public final class StatSet extends DSet<Stat>
*/
public void incrementMapStat (Stat.Type type, String value, int amount)
{
StringMapStat stat = (StringMapStat)get(type.name());
StringMapStat stat = (StringMapStat)getStat(type);
if (stat == null) {
stat = (StringMapStat)type.newStat();
stat.increment(value, amount);
@@ -184,39 +185,62 @@ public final class StatSet extends DSet<Stat>
/**
* Returns the current value of the specified integer statistic.
*
* @exception ClassCastException thrown if the registered type of the specified stat is not an
* {@link IntStat}.
*/
public int getIntStat (Stat.Type type)
{
IntStat stat = (IntStat)get(type.name());
IntStat stat = (IntStat)getStat(type);
return (stat == null) ? 0 : stat.getValue();
}
/**
* Returns the maximum value by which the specified integer statistic has ever been
* incremented.
*
* @exception ClassCastException thrown if the registered type of the specified stat is not an
* {@link MaxIntStat}.
*/
public int getMaxIntStat (Stat.Type type)
{
MaxIntStat stat = (MaxIntStat)get(type.name());
MaxIntStat stat = (MaxIntStat)getStat(type);
return (stat == null) ? 0 : stat.getMaxValue();
}
/**
* Returns the current value of the specified integer array statistic.
*
* @exception ClassCastException thrown if the registered type of the specified stat is not an
* {@link IntArrayStat}.
*/
public int[] getIntArrayStat (Stat.Type type)
{
IntArrayStat stat = (IntArrayStat)get(type.name());
IntArrayStat stat = (IntArrayStat)getStat(type);
return (stat == null) ? new int[0] : stat.getValue();
}
/**
* Returns true if the specified {@link StringSetStat} contains the specified value, false
* Returns the current size of the specified SetStat statistic.
*
* @exception ClassCastException thrown if the registered type of the specified stat is not an
* {@link SetStat}.
*/
@SuppressWarnings("unchecked")
public int getSetStatSize (Stat.Type type)
{
SetStat stat = (SetStat)getStat(type);
return (stat == null ? 0 : stat.size());
}
/**
* Returns true if the specified {@link SetStat} contains the specified value, false
* otherwise.
*/
public boolean containsValue (Stat.Type type, String value)
@SuppressWarnings("unchecked")
public <T> boolean containsValue (Stat.Type type, T value)
{
StringSetStat stat = (StringSetStat)get(type.name());
SetStat<T> stat = (SetStat<T>)getStat(type);
return (stat == null) ? false : stat.contains(value);
}
@@ -226,7 +250,7 @@ public final class StatSet extends DSet<Stat>
*/
public int getMapValue (Stat.Type type, String value)
{
StringMapStat stat = (StringMapStat)get(type.name());
StringMapStat stat = (StringMapStat)getStat(type);
return (stat == null) ? 0 : stat.get(value);
}
@@ -1,5 +1,23 @@
//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.stats.data;
@@ -9,11 +27,21 @@ import com.samskivert.util.StringUtil;
/**
* Used to track a statistic comprised of a set of strings.
*/
public abstract class StringSetStat extends Stat
public abstract class StringSetStat extends SetStat<String>
{
/**
* Returns the number of Strings in this set.
*/
@Override // from SetStat
public int size ()
{
return _values.length;
}
/**
* Returns true if the specified string is contained in this set.
*/
@Override // from SetStat
public boolean contains (String key)
{
if (key == null) {
@@ -28,6 +56,7 @@ public abstract class StringSetStat extends Stat
* @return true if the string was newly added, false if it was already
* contained in the set.
*/
@Override // from SetStat
public boolean add (String key)
{
if (key == null) {