
package org.osgi.test.support.compatibility;

/**
 * A classic Semaphore class.
 */
public class Semaphore {

	int count = 0;

	/** 
	 * Creates a new Semaphore with 0 as a starti value.
	 */
	public Semaphore() {
		this(0);
	}

	/** 
	 * Creates a new Semaphore with a given start value.
	 * @param startValue sets the value to start with.
	 * @throws IllegalArgumentException if the startValue is negative
	 */
	public Semaphore(int startValue) throws IllegalArgumentException {
		if(startValue < 0) {
			throw new IllegalArgumentException("startValue can't be lower than 0");
		}
		count = startValue;
	}
	
	/**
	 * Waits until a signal is (or has been) given.
	 */
	public synchronized void waitForSignal() {
		while(count == 0) {
			try {
				super.wait();
			}
			catch(InterruptedException e) {
			    throw new RuntimeException("Thread " 
			        + Thread.currentThread().getName() + " was interrupted");
			}
		}
		count--;
	}
	
	/**
	 * Waits until a signal is (or has been) given.
	 */
	public synchronized void waitForSignal(long timeout) throws InterruptedException {
		while (count == 0) {
			super.wait(timeout);
		}
		count--;
	}
	
	/**
	 * Give the signal.
	 */
	public synchronized void signal() {
        count++;
        notify();
    }
}


@@ -121,25 +121,24 @@
 t = 0;%0A%0A%09/**
- 
 %0A%09 * Creates
@@ -171,17 +171,16 @@
  a start
-i
  value.%0A
@@ -665,37 +665,24 @@
  */%0A%09public 
-synchronized 
 void waitFor
@@ -694,73 +694,15 @@
 l() 
-%7B%0A%09%09while(count == 0) %7B%0A%09%09%09try %7B%0A%09%09%09%09super.wait();%0A%09%09%09%7D%0A%09%09%09catch(
+throws 
 Inte
@@ -721,146 +721,29 @@
 tion
- e)
  %7B%0A%09%09
-%09    throw new RuntimeException(%22Thread %22 %0A%09%09%09        + Thread.currentThread().getName() + %22 was interrupted%22);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09count--
+waitForSignal(0)
 ;%0A%09%7D
@@ -915,17 +915,11 @@
 ) %7B%0A
+
 %09%09%09
-super.
 wait

implement waitForSignal() in terms of waitForSignal(long)