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

com.jolira.st4j.impl.MetricStoreImpl Maven / Gradle / Ivy

/**
 * Copyright (c) 2011 jolira. All rights reserved. This program and the accompanying materials are made available under
 * the terms of the GNU Public License 2.0 which is available at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

package com.jolira.st4j.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;
import javax.inject.Singleton;

import com.jolira.st4j.Metric;
import com.jolira.st4j.MetricStore;

/**
 * A simple implementation of the metric store.
 * 
 * @author jfk
 * @date Aug 27, 2011 6:41:52 PM
 * @since 1.0
 * 
 */
@Singleton
public class MetricStoreImpl implements MetricStore {
    private static final String DEFAULT = "##default";
    private final static ThreadLocal> localMetrics = new ThreadLocal>() {
        @Override
        protected Map initialValue() {
            return new HashMap();
        }
    };

    @Override
    public Map getAndResetThreadLocalMetrics() {
        final Map result = localMetrics.get();

        localMetrics.remove();

        return result;
    }

    @Override
    public String getMetricName(@Nullable final String mname, final Class type) {
        if (mname != null && !DEFAULT.equals(mname)) {
            return mname;
        }

        final Metric metric = type.getAnnotation(Metric.class);

        if (metric != null) {
            final String value = metric.value();

            if (value != null && !DEFAULT.equals(value)) {
                return value;
            }
        }

        final String name = type.getName();

        return name.toLowerCase();
    }

    @Override
    public  T getThreadLocalMeasurement(final String mname, final Class type) {
        final String metricName = getMetricName(mname, type);
        final Map metricByName = localMetrics.get();
        final Object obj = metricByName.get(metricName);

        return type.cast(obj);
    }

    @Override
    public void postThreadLocalMeasurement(final String mname, final Object metric, final boolean unique) {
        final Class type = metric.getClass();
        final String metricName = getMetricName(mname, type);
        final Map metricByName = localMetrics.get();
        final boolean existis = metricByName.containsKey(metricName);

        if (unique || !existis) {
            metricByName.put(metricName, metric);
            return;
        }

        final Object existing = metricByName.get(metricName);

        if (existing instanceof Collection) {
            @SuppressWarnings("unchecked")
            final Collection collection = (Collection) existing;

            collection.add(metric);
            return;
        }

        final ArrayList collection = new ArrayList();

        collection.add(existing);
        collection.add(metric);

        metricByName.put(metricName, collection);
    }
}