<commit_before>/* The MIT License (MIT)
 * Copyright (c) 2014 <NAME>
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.io;

/**
 * Default resource pool.
 */

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public abstract class ResourcePool<T> {

	protected final Queue<T> resources = new ConcurrentLinkedQueue<T>();
	private final Semaphore semaphore;

	public ResourcePool(int maxResources) {
		semaphore = new Semaphore(maxResources, true);
	}

	/**
	 * Try to acquire a resource from the pool. Either a resource is returned or the request times out and throws a exception.
	 * 
	 * @param maxWait
	 *            The maximum time to wait for a resource, in milliseconds.
	 * @return the requested resource.
	 * @throws InterruptedException
	 *             if interrupted while waiting for a resource.
	 * @throws ResourceCreationException
	 *             if the resource could not be created.
	 */
	public T getResource(long maxWait) throws InterruptedException, ResourceCreationException {

		// try to get a resource. Do not wait longer than the timeout
		semaphore.tryAcquire(maxWait, TimeUnit.MILLISECONDS);

		// Acquire a resource
		T resource = resources.poll();
		if (resource != null)
			return resource;

		// create a resource if none are available
		try {
			return createResource();
		} catch (Exception e) {
			// in case of failure the Semaphore permit must be returned, or they will run out
			semaphore.release();
			throw new ResourceCreationException(e);
		}
	}

	abstract protected T createResource();

	// to allow checks and manipulation before return
	public T processBeforReturn(T resource) {
		return resource;
	}

	public void returnResource(T resource) {
		T processedResource = processBeforReturn(resource);
		resources.add(processedResource);
		semaphore.release();
	}
}
<commit_msg>CreateResource method must be able to throw Exceptions<commit_after>/* The MIT License (MIT)
 * Copyright (c) 2014 <NAME>
 * http://opensource.org/licenses/MIT
 */

package com.github.dozedoff.commonj.io;

/**
 * Default resource pool.
 */

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public abstract class ResourcePool<T> {

	protected final Queue<T> resources = new ConcurrentLinkedQueue<T>();
	private final Semaphore semaphore;

	public ResourcePool(int maxResources) {
		semaphore = new Semaphore(maxResources, true);
	}

	/**
	 * Try to acquire a resource from the pool. Either a resource is returned or the request times out and throws a exception.
	 * 
	 * @param maxWait
	 *            The maximum time to wait for a resource, in milliseconds.
	 * @return the requested resource.
	 * @throws InterruptedException
	 *             if interrupted while waiting for a resource.
	 * @throws ResourceCreationException
	 *             if the resource could not be created.
	 */
	public T getResource(long maxWait) throws InterruptedException, ResourceCreationException {

		// try to get a resource. Do not wait longer than the timeout
		semaphore.tryAcquire(maxWait, TimeUnit.MILLISECONDS);

		// Acquire a resource
		T resource = resources.poll();
		if (resource != null)
			return resource;

		// create a resource if none are available
		try {
			return createResource();
		} catch (Exception e) {
			// in case of failure the Semaphore permit must be returned, or they will run out
			semaphore.release();
			throw new ResourceCreationException(e);
		}
	}

	abstract protected T createResource() throws Exception;

	// to allow checks and manipulation before return
	public T processBeforReturn(T resource) {
		return resource;
	}

	public void returnResource(T resource) {
		T processedResource = processBeforReturn(resource);
		resources.add(processedResource);
		semaphore.release();
	}
}

{'fraction_non_alphanumeric': 0.08489135927235977, 'fraction_numerical': 0.00202122283981809, 'mean_word_length': 3.7584134615384617, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4938950', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1080, 'n_words': 389}