Refactor ConcurrentHashMap creation code to a helper method

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132352263
This commit is contained in:
shikhman 2016-09-06 12:57:49 -07:00 committed by Ben McIlwain
parent 0538c1e210
commit dbb9ef80c5
3 changed files with 25 additions and 27 deletions

View file

@ -17,6 +17,7 @@ package google.registry.monitoring.metrics;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.collect.ImmutableList;
import java.util.concurrent.ConcurrentHashMap;
/** Static helper methods for the Metrics library. */
final class MetricsUtils {
@ -25,6 +26,16 @@ final class MetricsUtils {
private static final String LABEL_SIZE_ERROR =
"The count of labelValues must be equal to the underlying Metric's count of labels.";
/**
* The below constants replicate the default initial capacity, load factor, and concurrency level
* for {@link ConcurrentHashMap} as of Java SE 7. They are recorded here so that a {@link
* com.google.common.util.concurrent.Striped} object can be constructed with a concurrency level
* matching the default concurrency level of a {@link ConcurrentHashMap}.
*/
private static final int HASHMAP_INITIAL_CAPACITY = 16;
private static final float HASHMAP_LOAD_FACTOR = 0.75f;
static final int DEFAULT_CONCURRENCY_LEVEL = 16;
private MetricsUtils() {}
/**
@ -53,4 +64,8 @@ final class MetricsUtils {
!Double.isInfinite(value) && !Double.isNaN(value) && !NEGATIVE_ZERO.equals(value),
"value must be finite, not NaN, and not -0.0");
}
static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int concurrencyLevel) {
return new ConcurrentHashMap<>(HASHMAP_INITIAL_CAPACITY, HASHMAP_LOAD_FACTOR, concurrencyLevel);
}
}