package org.terracotta.statistics;
import org.terracotta.context.WeakIdentityHashMap;
import org.terracotta.context.annotations.ContextAttribute;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
@ContextAttribute(value = "this")
class PassThroughStatistic<T extends Serializable> implements ValueStatistic<T> {
private static final WeakIdentityHashMap<Object, Collection<PassThroughStatistic<? extends Serializable>>> BINDING = new WeakIdentityHashMap<>();
private static void bindStatistic(PassThroughStatistic<? extends Serializable> stat, Object to) {
Collection<PassThroughStatistic<? extends Serializable>> collection = BINDING.get(to);
if (collection == null) {
collection = new CopyOnWriteArrayList<>();
Collection<PassThroughStatistic<? extends Serializable>> racer = BINDING.putIfAbsent(to, collection);
if (racer != null) {
collection = racer;
}
}
collection.add(stat);
}
public static void removeStatistics(Object to) {
BINDING.remove(to);
}
static boolean hasStatisticsFor(Object to) {
return BINDING.get(to) != null;
}
@ContextAttribute("name") public final String name;
@ContextAttribute("tags") public final Set<String> tags;
@ContextAttribute("properties") public final Map<String, Object> properties;
@ContextAttribute("type") public final StatisticType type;
private final ValueStatistic<T> source;
public PassThroughStatistic(Object context, String name, Set<String> tags, Map<String, ? extends Object> properties, ValueStatistic<T> source) {
this.name = name;
this.tags = Collections.unmodifiableSet(new HashSet<>(tags));
this.properties = Collections.unmodifiableMap(new HashMap<String, Object>(properties));
this.source = source;
this.type = source.type();
bindStatistic(this, context);
}
@Override
public T value() {
return source.value();
}
@Override
public StatisticType type() {
return type;
}
}