<commit_before>/*
 * Copyright (C) 2011 <NAME>
 *
 * 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;

/**
 * Maintains a request journal. Is thread safe.
 */
public class InMemoryRequestJournal implements RequestListener, RotatingRequestJournal {

	private final 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) {
		synchronized (requests) {
			return size(filter(requests, matchedBy(requestPattern)));
		}
	}

	@Override
	public List<LoggedRequest> getRequestsMatching(RequestPattern requestPattern) {
		synchronized (requests) {
			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) {
		synchronized (requests) {
			requests.add(LoggedRequest.createFrom(request));
		}
		removeOldEntries();
	}

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

	@Override
	public void reset() {
		synchronized (requests) {
			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()) {
			synchronized (requests) {
				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;<commit_msg>Oops, forgot that the request log is already a ConcurrentLinkedQueue<commit_after>/*
 * Copyright (C) 2011 <NAME>
 *
 * 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;

/**
 * Maintains a request journal. Is thread safe.
 */
public class InMemoryRequestJournal implements RequestListener, RotatingRequestJournal {

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

	private Optional<Integer> maxEntries;

	private final 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;
{'fraction_non_alphanumeric': 0.08945560253699789, 'fraction_numerical': 0.0023784355179704017, 'mean_word_length': 4.665419161676646, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5632639', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2034, 'n_words': 639}