package com.eactive.eai.adapter.socket2.common;

import com.eactive.eai.adapter.socket2.EAITimeoutException;

public class SimpleFuture {
    private Object result;
    private boolean ready = false;    
    
    public synchronized void init() {
    	ready = false;
    }
    
    public synchronized void set(Object result) {
        this.result = result;
        this.ready = true;
        notifyAll();
    }    
    
    public synchronized Object join() {
    	//return join(60000L);  // 60 seconds
    	
        while (!ready) {
            try {
                wait();
            } catch (InterruptedException e) {
            	break;
            }
        }
        return result;
    }
    
    public synchronized Object join(long timeout) throws EAITimeoutException {
        while (!ready) {
            try {
                wait(timeout);
            } catch (InterruptedException e) {
            	break;
            }
            if (!ready)
            	throw new EAITimeoutException("timeout error (" + timeout + "ms)");
        }
        return result;
    }
}
