com.almende.util.callback.SyncCallback Maven / Gradle / Ivy
/*
* Copyright: Almende B.V. (2014), Rotterdam, The Netherlands
* License: The Apache Software License, Version 2.0
*/
package com.almende.util.callback;
import com.almende.util.TypeUtil;
/**
* The Class SyncCallback.
*
* @param
* the generic type
*/
public class SyncCallback extends AsyncCallback {
private T response = null;
private Exception exception = null;
private boolean done = false;
/**
* Instantiates a new sync callback.
*
* @param type
* the type
*/
public SyncCallback(TypeUtil type) {
super(type);
}
/**
* Instantiates a new sync callback.
*/
public SyncCallback() {
super();
}
/*
* (non-Javadoc)
* @see
* com.almende.eve.agent.callback.AsyncCallback#onSuccess(java.lang.Object)
*/
@Override
public void onSuccess(final T response) {
this.response = response;
done = true;
synchronized (this) {
notifyAll();
}
}
/*
* (non-Javadoc)
* @see
* com.almende.eve.agent.callback.AsyncCallback#onFailure(java.lang.Exception
* )
*/
@Override
public void onFailure(final Exception exception) {
this.exception = exception;
done = true;
synchronized (this) {
notifyAll();
}
}
/**
* Get will wait for the request to finish and then return the
* response. If an exception is returned, the exception will be
* thrown.
*
* @return response
* @throws Exception
* the exception
*/
public T get() throws Exception {
while (!done) {
synchronized (this) {
wait();
}
}
if (exception != null) {
throw exception;
}
return type.inject(response);
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy