Updated for guava 10.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6709 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2011-09-28 22:47:06 +00:00
parent 779ffff3b4
commit 0c42de73b3
@@ -23,8 +23,12 @@ package com.threerings.util;
import java.util.Map; import java.util.Map;
import com.google.common.collect.MapMaker; import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
import static com.threerings.NaryaLog.log; import static com.threerings.NaryaLog.log;
@@ -122,8 +126,9 @@ public class MethodProfiler
*/ */
public Map<String, Result> getResults () public Map<String, Result> getResults ()
{ {
Map<String, Result> results = Maps.newHashMapWithExpectedSize(_profiles.size()); Map<String, Result> results = Maps.newHashMapWithExpectedSize(
for (Map.Entry<String, RunningStats> entry : _profiles.entrySet()) { Ints.saturatedCast(_profiles.size()));
for (Map.Entry<String, RunningStats> entry : _profiles.asMap().entrySet()) {
synchronized (entry.getValue()) { synchronized (entry.getValue()) {
results.put(entry.getKey(), toResult(entry.getValue())); results.put(entry.getKey(), toResult(entry.getValue()));
} }
@@ -196,7 +201,7 @@ public class MethodProfiler
*/ */
public void reset () public void reset ()
{ {
_profiles.clear(); _profiles.invalidateAll();
} }
/** /**
@@ -204,7 +209,7 @@ public class MethodProfiler
*/ */
protected void recordTime (String method, double elapsedMs) protected void recordTime (String method, double elapsedMs)
{ {
RunningStats stats = _profiles.get(method); RunningStats stats = _profiles.getUnchecked(method);
synchronized (stats) { synchronized (stats) {
stats.addSample(elapsedMs); stats.addSample(elapsedMs);
} }
@@ -331,6 +336,10 @@ public class MethodProfiler
}; };
/** Stats by method name. */ /** Stats by method name. */
protected final Map<String, RunningStats> _profiles = protected final Cache<String, RunningStats> _profiles = CacheBuilder.newBuilder()
new MapMaker().makeComputingMap(DefaultMap.newInstanceCreator(RunningStats.class)); .build(new CacheLoader<String, RunningStats>() {
public RunningStats load (String key) {
return new RunningStats();
}
});
} }