/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * 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 com.github.tomakehurst.wiremock.verification;

import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestListener;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.size;

public class InMemoryRequestJournal implements RequestListener, RotatingRequestJournal {

	private Queue<LoggedRequest> requests = new ConcurrentLinkedQueue<LoggedRequest>();

	private Optional<Integer> maxEntries;

	private Optional<Integer> initialMaxEntries;

	/**
	 * @param initialMaxEntries Initial value for the size of the request journal. When the journal is reset the
	 *                          maximum number of entries is reset to this value.
	 */
	public InMemoryRequestJournal(Optional<Integer> initialMaxEntries) {
		this.initialMaxEntries = initialMaxEntries;
		setMaxEntries(initialMaxEntries);
	}

	@Override
	public int countRequestsMatching(RequestPattern requestPattern) {
		return size(filter(requests, matchedBy(requestPattern)));
	}

	@Override
	public List<LoggedRequest> getRequestsMatching(RequestPattern requestPattern) {
		return ImmutableList.copyOf(filter(requests, matchedBy(requestPattern)));
	}

	private Predicate<Request> matchedBy(final RequestPattern requestPattern) {
		return new Predicate<Request>() {
			public boolean apply(Request input) {
				return requestPattern.isMatchedBy(input);
			}
		};
	}

	@Override
	public void requestReceived(Request request, Response response) {
		requests.add(LoggedRequest.createFrom(request));
		removeOldEntries();
	}

	@Override
	public void requestReceived(Request request) {
		requestReceived(request, null);
	}

	@Override
	public void reset() {
		requests.clear();
		setMaxEntries(initialMaxEntries);
	}

	private void removeOldEntries() {
		// Prevent race conditions when maxEntries is updated by copying the value before reading it (race condition
		// between isPresent and get)
		Optional<Integer> maxEnt = maxEntries;
		if (maxEnt.isPresent()) {
			while (requests.size() > maxEnt.get()) {
				// Remove entries from the journal until we reach the desired number. In general this will only be
				// maximum one entry if we add one entry in requestReceived.
				requests.poll();
			}
		}
	}

	@Override
	public void setMaxEntries(Optional<Integer> maxEntries) {
		if (maxEntries.isPresent() && maxEntries.get() < 0) {
			throw new IllegalArgumentException("Maximum number of entries of journal must be greater than zero");
		}
		this.maxEntries = maxEntries;
		removeOldEntries();
	}

	@Override
	public Optional<Integer> getMaxEntries() {
		return this.maxEntries;
	}
}

@@ -1223,16 +1223,72 @@
 .size;%0A%0A
+/**%0A * Maintains a request journal. Is thread safe.%0A */%0A
 public c
@@ -1378,16 +1378,22 @@
 private 
+final 
 Queue%3CLo
@@ -1973,32 +1973,61 @@
 questPattern) %7B%0A
+%09%09synchronized (requests) %7B%0A%09
 %09%09return size(fi
@@ -2066,24 +2066,28 @@
 Pattern)));%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Overri
@@ -2162,32 +2162,61 @@
 questPattern) %7B%0A
+%09%09synchronized (requests) %7B%0A%09
 %09%09return Immutab
@@ -2275,16 +2275,20 @@
 ern)));%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09pri
@@ -2571,24 +2571,53 @@
 response) %7B%0A
+%09%09synchronized (requests) %7B%0A%09
 %09%09requests.a
@@ -2655,16 +2655,20 @@
 uest));%0A
+%09%09%7D%0A
 %09%09remove
@@ -2812,24 +2812,53 @@
 d reset() %7B%0A
+%09%09synchronized (requests) %7B%0A%09
 %09%09requests.c
@@ -2865,16 +2865,20 @@
 lear();%0A
+%09%09%7D%0A
 %09%09setMax
@@ -3156,16 +3156,46 @@
 nt()) %7B%0A
+%09%09%09synchronized (requests) %7B%0A%09
 %09%09%09while
@@ -3234,16 +3234,17 @@
 ) %7B%0A%09%09%09%09
+%09
 // Remov
@@ -3338,16 +3338,17 @@
  be%0A%09%09%09%09
+%09
 // maxim
@@ -3404,16 +3404,17 @@
 ed.%0A%09%09%09%09
+%09
 requests
@@ -3422,16 +3422,22 @@
 poll();%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Remove possibility for race condition to occur when adding/clearing and querying the request log at the same time