All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ringcentral.platform.metrics.AbstractMetricContext Maven / Gradle / Ivy

package com.ringcentral.platform.metrics;

import java.util.Map;
import java.util.concurrent.*;

import static com.ringcentral.platform.metrics.UnmodifiableMetricContext.emptyUnmodifiableMetricContext;
import static org.apache.commons.lang3.ClassUtils.getAllInterfaces;

public abstract class AbstractMetricContext implements MetricContext {

    private final Map props;
    private static final ConcurrentMap, Class> typeKeys = new ConcurrentHashMap<>();

    protected AbstractMetricContext(Map props) {
        this.props = props;
    }

    protected void put(Object key, Object value) {
        props.put(key, value);
    }

    protected void with(Object value) {
        if (value instanceof MetricContextTypeKeySubtype) {
            Class type = value.getClass();

            Class typeKey = typeKeys.computeIfAbsent(type, t -> {
                for (Class iface : getAllInterfaces(type)) {
                    if (iface.isAnnotationPresent(MetricContextTypeKey.class)) {
                        return iface;
                    }
                }

                return t;
            });

            put(typeKey, value);
        } else {
            put(value.getClass(), value);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public  V get(Object key) {
        return isEmpty() ? null : (V)props.get(key);
    }

    protected Map properties() {
        return props;
    }

    @Override
    public boolean isEmpty() {
        return props.isEmpty();
    }

    public UnmodifiableMetricContext unmodifiable() {
        return isEmpty() ? emptyUnmodifiableMetricContext() : new UnmodifiableMetricContext(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy