From 007bdc91c8b6ec45770e71bce75e0f97e1cdeb9b Mon Sep 17 00:00:00 2001 From: Tom Conkling Date: Tue, 15 Jul 2008 20:26:26 +0000 Subject: [PATCH] created IntSetStat git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@657 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../com/threerings/stats/data/IntSetStat.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/java/com/threerings/stats/data/IntSetStat.java diff --git a/src/java/com/threerings/stats/data/IntSetStat.java b/src/java/com/threerings/stats/data/IntSetStat.java new file mode 100644 index 00000000..65b1c675 --- /dev/null +++ b/src/java/com/threerings/stats/data/IntSetStat.java @@ -0,0 +1,88 @@ +// +// $Id$ + +package com.threerings.stats.data; + +import java.io.IOException; +import java.util.HashSet; + +import com.samskivert.util.StringUtil; +import com.threerings.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; + +public class IntSetStat extends Stat +{ + /** + * Constructs a new IntSetStat that will store an unbounded number of ints. + */ + public IntSetStat () + { + _maxSize = -1; + } + + /** + * Constructs a new IntSetStat that will store up to maxSize ints. + */ + public IntSetStat (int maxSize) + { + _maxSize = maxSize; + } + + /** + * Returns the number of values stored in the set. + */ + public int size () + { + return _intSet.size(); + } + + /** + * Returns true if the specified int is contained in this set. + */ + public boolean contains (int key) + { + return _intSet.contains(key); + } + + /** + * Adds the specified int to this set. + * + * @return true if the int was newly added, false if it was already contained in the set. + */ + public boolean add (int key) + { + return (_maxSize < 0 || _intSet.size() < _maxSize ? _intSet.add(key) : false); + } + + @Override + public void persistTo (ObjectOutputStream out, AuxDataSource aux) + throws IOException + { + out.writeByte(_intSet.size()); + for (int key : _intSet) { + out.writeInt(key); + } + } + + @Override + public void unpersistFrom (ObjectInputStream in, AuxDataSource aux) + throws IOException, ClassNotFoundException + { + int numValues = in.readByte(); + _intSet = new HashSet(numValues); + for (int ii = 0; ii < numValues; ii++) { + _intSet.add(in.readInt()); + } + + } + + @Override + public String valueToString () + { + return StringUtil.toString(_intSet); + } + + protected int _maxSize; + protected HashSet _intSet = new HashSet(); + +}