/*
 * Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.actuate.metrics;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * {@link MetricRepository} implementation that stores metric in-memory.
 * 
 * @author Dave Syer
 */
public class InMemoryMetricRepository implements MetricRepository {

	private ConcurrentMap<String, Measurement> metrics = new ConcurrentHashMap<String, Measurement>();

	private ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();

	@Override
	public void increment(String metricName, int amount, Date timestamp) {
		Measurement current = this.metrics.get(metricName);
		if (current != null) {
			Object lock = this.locks.putIfAbsent(metricName, new Object());
			if (lock == null) {
				lock = this.locks.get(metricName);
			}
			synchronized (lock) {
				current = this.metrics.get(metricName);
				Metric metric = current.getMetric();
				this.metrics.replace(metricName, current, new Measurement(timestamp,
						metric.increment(amount)));
				return;
			}
		}
		this.metrics.putIfAbsent(metricName, new Measurement(timestamp, new Metric(
				metricName, amount)));
	}

	@Override
	public void set(String metricName, double value, Date timestamp) {
		Measurement current = this.metrics.get(metricName);
		if (current != null) {
			Metric metric = current.getMetric();
			this.metrics.replace(metricName, current,
					new Measurement(timestamp, metric.set(value)));
		}
		else {
			this.metrics.putIfAbsent(metricName, new Measurement(timestamp, new Metric(
					metricName, value)));
		}
	}

	@Override
	public void delete(String metricName) {
		this.metrics.remove(metricName);
	}

	@Override
	public Metric findOne(String metricName) {
		if (this.metrics.containsKey(metricName)) {
			return this.metrics.get(metricName).getMetric();
		}
		return new Metric(metricName, 0);
	}

	@Override
	public Collection<Metric> findAll() {
		ArrayList<Metric> result = new ArrayList<Metric>();
		for (Measurement measurement : this.metrics.values()) {
			result.add(measurement.getMetric());
		}
		return result;
	}

}

@@ -1289,88 +1289,8 @@
 ) %7B%0A
-%09%09Measurement current = this.metrics.get(metricName);%0A%09%09if (current != null) %7B%0A%09
 %09%09Ob
@@ -1353,17 +1353,16 @@
 t());%0A%09%09
-%09
 if (lock
@@ -1373,17 +1373,16 @@
 null) %7B%0A
-%09
 %09%09%09lock 
@@ -1413,20 +1413,18 @@
 ame);%0A%09%09
-%09
 %7D%0A
-%09
 %09%09synchr
@@ -1442,17 +1442,28 @@
 k) %7B%0A%09%09%09
-%09
+Measurement 
 current 
@@ -1494,16 +1494,42 @@
 cName);%0A
+%09%09%09if (current != null) %7B%0A
 %09%09%09%09Metr
@@ -1687,18 +1687,26 @@
 %0A%09%09%09%7D%0A%09%09
-%7D%0A
+%09else %7B%0A%09%09
 %09%09this.m
@@ -1754,33 +1754,39 @@
 ement(timestamp,
- 
+%0A%09%09%09%09%09%09
 new Metric(%0A%09%09%09%09
@@ -1780,21 +1780,16 @@
  Metric(
-%0A%09%09%09%09
 metricNa
@@ -1803,16 +1803,25 @@
 unt)));%0A
+%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

Remove a small race condition