/*
 * Copyright 2006 Takahiro Nakamura.
 *
 * 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 woolpack.acquirable;

import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;

import woolpack.fn.Fn;

/**
 * 実行権制御のユーティリティです。
 * 型推論で表記を簡略するためのスタティックメソッドを含みます。
 * 
 * @author nakamura
 * 
 */
public final class AcquirableUtils {

	/**
	 * 無制限に実行権を与える{@link Acquirable}です。
	 */
	public static final Acquirable<RuntimeException> ANY = new Acquirable<RuntimeException>() {
		public boolean acquire() {
			return true;
		}
		public void release() {
			// nothing to do.
		}
	};

	/**
	 * 常に実行権を与えない{@link Acquirable}です。
	 */
	public static final Acquirable<RuntimeException> NONE = new Acquirable<RuntimeException>() {
		public boolean acquire() {
			return false;
		}
		public void release() {
			// nothing to do.
		}
	};

	/**
	 * 常に実行権を与えず、実行したスレッドに対し{@link Thread#interrupt()}を実行する{@link Acquirable}です。
	 */
	public static final Acquirable<RuntimeException> INTERRUPT = new Acquirable<RuntimeException>() {
		public boolean acquire() {
			Thread.currentThread().interrupt();
			return false;
		}
		public void release() {
			// nothing to do.
		}
	};

	/**
	 * 常に実行権を与えず、
	 * 実行したスレッドに対し{@link Thread#interrupt()}を実行し、
	 * {@link InterruptedException}を原因として持つ
	 * {@link IllegalStateException}を投げる{@link Acquirable}です。
	 */
	public static final Acquirable<InterruptedException> INTERRUPTED_EXCEPTION = new Acquirable<InterruptedException>() {
		public boolean acquire() throws InterruptedException {
			Thread.currentThread().interrupt();
			throw new InterruptedException();
		}

		public void release() {
			// nothing to do.
		}
	};

	private AcquirableUtils() {
	}
	
	public static <C, R, E extends Exception> AcquireFn<C, R, E> acquire(
			final Fn<? super C, ? extends Acquirable<? extends E>, ? extends E> acquireGetter,
			final Fn<? super C, ? extends R, ? extends E> trueFn,
			final Fn<? super C, ? extends R, ? extends E> falseFn) {
		return new AcquireFn<C, R, E>(acquireGetter, trueFn, falseFn);
	}
	
	public static DoLock<RuntimeException> doLock(final Lock lock) {
		return new DoLock<RuntimeException>(lock);
	}
	
	public static DoSemaphore doSemaphore(final Semaphore semaphore) {
		return new DoSemaphore(semaphore);
	}
	
	public static TryLock<RuntimeException> tryLock(final Lock lock) {
		return new TryLock<RuntimeException>(lock);
	}
	
	public static TrySemaphore<RuntimeException> trySemaphore(final Semaphore semaphore) {
		return new TrySemaphore<RuntimeException>(semaphore);
	}
	
	public static <E extends Exception> AcquirableChain<E> chain(final List<? extends Acquirable<? extends E>> list) {
		return new AcquirableChain<E>(list);
	}
}
