Type safety, formatting.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1874 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2006-07-12 23:22:52 +00:00
parent 73365f4721
commit fd7c4d98eb
3 changed files with 25 additions and 16 deletions
@@ -32,7 +32,7 @@ public class HttpPostUtil
int timeout) int timeout)
throws IOException, ServiceWaiter.TimeoutException throws IOException, ServiceWaiter.TimeoutException
{ {
final ServiceWaiter waiter = new ServiceWaiter( final ServiceWaiter<String> waiter = new ServiceWaiter<String>(
(timeout < 0) ? ServiceWaiter.NO_TIMEOUT : timeout); (timeout < 0) ? ServiceWaiter.NO_TIMEOUT : timeout);
Thread tt = new Thread() { Thread tt = new Thread() {
public void run () { public void run () {
@@ -71,10 +71,9 @@ public class HttpPostUtil
tt.start(); tt.start();
if (waiter.waitForResponse()) { if (waiter.waitForResponse()) {
return (String) waiter.getArgument(); return waiter.getArgument();
} else { } else {
throw (IOException) waiter.getArgument(); throw (IOException) waiter.getError();
} }
} }
} }
+1 -1
View File
@@ -307,7 +307,7 @@ public class HashIntMap<V> extends AbstractMap<Integer,V>
} }
// documentation inherited // documentation inherited
public Set<IntEntry<V>> intEntrySet() public Set<IntEntry<V>> intEntrySet ()
{ {
return new AbstractSet<IntEntry<V>>() { return new AbstractSet<IntEntry<V>>() {
public int size () { public int size () {
+21 -11
View File
@@ -28,8 +28,8 @@ import com.samskivert.Log;
* instruct the Java compiler to give public scope to an anonymous inner * instruct the Java compiler to give public scope to an anonymous inner
* class rather than default scope. Sigh. * class rather than default scope. Sigh.
*/ */
public class ServiceWaiter public class ServiceWaiter<T>
implements ResultListener implements ResultListener<T>
{ {
/** Timeout to specify when you don't want a timeout. Use at your own /** Timeout to specify when you don't want a timeout. Use at your own
* risk. */ * risk. */
@@ -83,7 +83,7 @@ public class ServiceWaiter
* Marks the request as successful and posts the supplied response * Marks the request as successful and posts the supplied response
* argument for perusal by the caller. * argument for perusal by the caller.
*/ */
public synchronized void postSuccess (Object arg) public synchronized void postSuccess (T arg)
{ {
_success = 1; _success = 1;
_argument = arg; _argument = arg;
@@ -91,13 +91,12 @@ public class ServiceWaiter
} }
/** /**
* Marks the request as failed and posts the supplied response * Marks the request as failed.
* argument for perusal by the caller.
*/ */
public synchronized void postFailure (Object arg) public synchronized void postFailure (Exception error)
{ {
_success = -1; _success = -1;
_argument = arg; _error = error;
notify(); notify();
} }
@@ -105,11 +104,19 @@ public class ServiceWaiter
* Returns the argument posted by the waiter when the response * Returns the argument posted by the waiter when the response
* arrived. * arrived.
*/ */
public Object getArgument () public T getArgument ()
{ {
return _argument; return _argument;
} }
/**
* Returns the exception posted in the event of failure.
*/
public Exception getError ()
{
return _error;
}
/** /**
* Blocks waiting for the response. * Blocks waiting for the response.
* *
@@ -148,7 +155,7 @@ public class ServiceWaiter
} }
// documentation inherited from interface ResultListener // documentation inherited from interface ResultListener
public void requestCompleted (Object result) public void requestCompleted (T result)
{ {
postSuccess(result); postSuccess(result);
} }
@@ -156,7 +163,7 @@ public class ServiceWaiter
// documentation inherited from interface ResultListener // documentation inherited from interface ResultListener
public void requestFailed (Exception cause) public void requestFailed (Exception cause)
{ {
postFailure((cause != null) ? cause.getMessage() : null); postFailure(cause);
} }
/** Whether or not the response succeeded; positive for success, /** Whether or not the response succeeded; positive for success,
@@ -165,7 +172,10 @@ public class ServiceWaiter
protected int _success = 0; protected int _success = 0;
/** The argument posted by the waiter upon receipt of the response. */ /** The argument posted by the waiter upon receipt of the response. */
protected Object _argument; protected T _argument;
/** The exception posted by the waiter upon failure. */
protected Exception _error;
/** How many seconds to wait before giving up the ghost. */ /** How many seconds to wait before giving up the ghost. */
protected int _timeout; protected int _timeout;