/*
 * Copyright 2012-2015 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.trace;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * In-memory implementation of {@link TraceRepository}.
 *
 * @author Dave Syer
 */
public class InMemoryTraceRepository implements TraceRepository {

	private int capacity = 100;

	private boolean reverse = true;

	private final List<Trace> traces = new LinkedList<Trace>();

	/**
	 * Flag to say that the repository lists traces in reverse order.
	 * @param reverse flag value (default true)
	 */
	public void setReverse(boolean reverse) {
		this.reverse = reverse;
	}

	/**
	 * @param capacity the capacity to set
	 */
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	@Override
	public List<Trace> findAll() {
		synchronized (this.traces) {
			return Collections.unmodifiableList(this.traces);
		}
	}

	@Override
	public void add(Map<String, Object> map) {
		Trace trace = new Trace(new Date(), map);
		synchronized (this.traces) {
			while (this.traces.size() >= this.capacity) {
				this.traces.remove(this.reverse ? this.capacity - 1 : 0);
			}
			if (this.reverse) {
				this.traces.add(0, trace);
			}
			else {
				this.traces.add(trace);
			}
		}
	}

}

@@ -664,16 +664,44 @@
 trace;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -722,16 +722,16 @@
 ctions;%0A
-
 import j
@@ -904,16 +904,44 @@
 ve Syer%0A
+ * @author Olivier Bourgain%0A
  */%0Apubl
@@ -1290,24 +1290,56 @@
  reverse) %7B%0A
+%09%09synchronized (this.traces) %7B%0A%09
 %09%09this.rever
@@ -1352,16 +1352,20 @@
 everse;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09/**
@@ -1451,16 +1451,48 @@
 city) %7B%0A
+%09%09synchronized (this.traces) %7B%0A%09
 %09%09this.c
@@ -1511,16 +1511,20 @@
 pacity;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -1632,16 +1632,37 @@
 bleList(
+new ArrayList%3CTrace%3E(
 this.tra
@@ -1665,16 +1665,17 @@
 .traces)
+)
 ;%0A%09%09%7D%0A%09%7D

Make InMemoryTraceRepository thread-safe