If you think the Android project Volley-Ball listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2011 The Android Open Source Project
*//fromwww.java2s.com
* 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 com.siu.android.volleyball.toolbox;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.siu.android.volleyball.BallResponse;
import com.siu.android.volleyball.response.ResponseListener;
import com.siu.android.volleyball.response.SingleResponseListener;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A Future that represents a Volley request.
*
* Used by providing as your response and error listeners. For example:
* <pre>
* RequestFuture<JSONObject> future = RequestFuture.newFuture();
* MyRequest request = new MyRequest(URL, future, future);
*
* // If you want to be able to cancel the request:
* future.setRequest(requestQueue.add(request));
*
* // Otherwise:
* requestQueue.add(request);
*
* try {
* JSONObject response = future.get();
* // do something with response
* } catch (InterruptedException e) {
* // handle the error
* } catch (ExecutionException e) {
* // handle the error
* }
* </pre>
*
* @param <T> The type of parsed response this future expects.
*/publicclass BallRequestFuture<T> extends SingleResponseListener<T> implements Future<T>, Response.ErrorListener {
private Request<?> mRequest;
privateboolean mResultReceived = false;
private T mResult;
private VolleyError mException;
publicstatic <E> BallRequestFuture<E> newFuture() {
returnnew BallRequestFuture<E>();
}
private BallRequestFuture() {}
publicvoid setRequest(Request<?> request) {
mRequest = request;
}
@Override
publicsynchronizedboolean cancel(boolean mayInterruptIfRunning) {
if (mRequest == null) {
return false;
}
if (!isDone()) {
mRequest.cancel();
return true;
} else {
return false;
}
}
@Override
public T get() throws InterruptedException, ExecutionException {
try {
return doGet(null);
} catch (TimeoutException e) {
thrownew AssertionError(e);
}
}
@Override
public T get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return doGet(TimeUnit.MILLISECONDS.convert(timeout, unit));
}
privatesynchronized T doGet(Long timeoutMs)
throws InterruptedException, ExecutionException, TimeoutException {
if (mException != null) {
thrownew ExecutionException(mException);
}
if (mResultReceived) {
return mResult;
}
if (timeoutMs == null) {
wait(0);
} elseif (timeoutMs > 0) {
wait(timeoutMs);
}
if (mException != null) {
thrownew ExecutionException(mException);
}
if (!mResultReceived) {
thrownew TimeoutException();
}
return mResult;
}
@Override
publicboolean isCancelled() {
if (mRequest == null) {
return false;
}
return mRequest.isCanceled();
}
@Override
publicsynchronizedboolean isDone() {
return mResultReceived || mException != null || isCancelled();
}
@Override
publicsynchronizedvoid onResponse(T response) {
mResultReceived = true;
mResult = response;
notifyAll();
}
@Override
publicsynchronizedvoid onErrorResponse(VolleyError error) {
mException = error;
notifyAll();
}
}