Class AsyncHelper
AsyncDataLink.
This class cannot be inherited or instantiated.
Thread safety
Unless otherwise noted, methods of this class are safe to use by multiple threads concurrently.Synchronization transparency
Unless otherwise noted, methods of this class are not synchronization transparent.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic DataTransferExceptiongetTransferException(Throwable... exceptions) Creates a singleDataTransferExceptionfrom the specified exceptions.static <DataType> AsyncDataListener<DataType>makeSafeListener(AsyncDataListener<? super DataType> outputListener) Creates anAsyncDataListenerwhich forwards data to a specified listener in a thread-safe manner.static <DataType> AsyncDataListener<OrderedData<DataType>>makeSafeOrderedListener(AsyncDataListener<? super DataType> outputListener) Creates anAsyncDataListenerwhich forwards data (with an additional index) to a specified listener in a thread-safe manner.
-
Method Details
-
getTransferException
Creates a singleDataTransferExceptionfrom the specified exceptions.The exception will be created as follows:
-
If there are no exceptions specified
nullis returned. -
If there is a single exception is specified: The specified exception is
returned if it is a
DataTransferException, if it is not then a newDataTransferExceptionwith the specified exception as itscause. -
If there are more than one exception specified: A new
DataTransferExceptionis created and the specified exceptions will be added to it assuppressedexceptions.
- Parameters:
exceptions- the array of exceptions to be represented by the returnedDataTransferExceptionexception. This array cannot benulland cannot containnullelements.- Returns:
- the
DataTransferExceptionrepresenting the specified exceptions ornullif there was no exception specified (empty array was passed) - Throws:
NullPointerException- thrown if the passed array of exceptions isnullor containsnullelements
-
If there are no exceptions specified
-
makeSafeOrderedListener
public static <DataType> AsyncDataListener<OrderedData<DataType>> makeSafeOrderedListener(AsyncDataListener<? super DataType> outputListener) Creates anAsyncDataListenerwhich forwards data (with an additional index) to a specified listener in a thread-safe manner. That is, theonDataArriveandonDoneReceivemethod of the returnedAsyncDataListenercan be called concurrently from multiple threads and these methods of the specified listener will still not be called concurrently. Also data will be forwarded to the specified listener only if there were no data forwarded with a greater or equalindexand theonDoneReceivemethod has not yet been called.As an example see the following single threaded code:
AsyncDataListener<OrderedData<String>> listener; listener = makeSafeOrderedListener(...); listener.onDataArrive(new OrderedData<>(1, "data1")); listener.onDataArrive(new OrderedData<>(3, "data3")); listener.onDataArrive(new OrderedData<>(2, "data2")); listener.onDoneReceive(AsyncReport.SUCCESS); listener.onDataArrive(new OrderedData<>(4, "data4")); listener.onDoneReceive(AsyncReport.CANCELED);
The above code will forward"data1"and"data3"to the wrapped listener (which should be written in the place of "...") only and in this order. Also only theAsyncReport.SUCCESSwill be reported in theonDoneReceivemethod. That is, the"data3"line and the two lines below the firstonDoneReceivecall will be effectively ignored.- Type Parameters:
DataType- the type of the data to be forwarded to the specified listener- Parameters:
outputListener- theAsyncDataListenerto which the data reported to the returned listener will be eventually forwarded. This argument cannot benull.- Returns:
- the
AsyncDataListenerwhich forwards data (with an additional index) to a specified listener in a thread-safe manner. This method never returnsnull. - See Also:
-
makeSafeListener
public static <DataType> AsyncDataListener<DataType> makeSafeListener(AsyncDataListener<? super DataType> outputListener) Creates anAsyncDataListenerwhich forwards data to a specified listener in a thread-safe manner.This method is similar to the
makeSafeOrderedListenermethod, the only difference is that the listener returned by this method implicitly assumes the order of the datas from the order of the method calls.More precisely, the
onDataArriveandonDoneReceivemethod of the returnedAsyncDataListenercan be called concurrently from multiple threads and these methods of the specified listener will still not be called concurrently. Also data will be forwarded to the specified listener only if theonDoneReceivemethod has not yet been called.As an example see the following single threaded code:
AsyncDataListener<String> listener; listener = makeSafeOrderedListener(...); listener.onDataArrive("data1"); listener.onDataArrive("data2"); listener.onDoneReceive(AsyncReport.SUCCESS); listener.onDataArrive("data3"); listener.onDoneReceive(AsyncReport.CANCELED);The above code will forward"data1"and"data2"to the wrapped listener (which should be written in the place of "...") only and in this order. Also only theAsyncReport.SUCCESSwill be reported in theonDoneReceivemethod. That is, the two lines below the firstonDoneReceivecall will be effectively ignored.Note that although invoking two
onDataArrivemethods concurrently is safe regarding consistency, in practice this must be avoided. This must be avoided because there is no telling in what order will these datas be forwarded and the contract ofAsyncDataListenerrequires that a data forwarded be at least as accurate as the previous ones. This implies that if theonDataArrivemethod is called concurrently the datas forwarded to them must be equivalent. In this case it was a waste of effort to forward all the data instead of just one of them.- Type Parameters:
DataType- the type of the data to be forwarded to the specified listener- Parameters:
outputListener- theAsyncDataListenerto which the data reported to the returned listener will be eventually forwarded. This argument cannot benull.- Returns:
- the
AsyncDataListenerwhich forwards data to a specified listener in a thread-safe manner. This method never returnsnull. This method may return the same listener passed in the argument if the specified listener already has the properties defined for the return value. - See Also:
-