2020import com .proxerme .library .entity .LoginData ;
2121import com .proxerme .library .entity .LoginUser ;
2222import com .proxerme .library .entity .News ;
23- import com .proxerme .library .event .ConferencesEvent ;
2423import com .proxerme .library .event .IEvent ;
25- import com .proxerme .library .event .LoginEvent ;
26- import com .proxerme .library .event .LogoutEvent ;
27- import com .proxerme .library .event .NewsEvent ;
24+ import com .proxerme .library .event .error .ConferencesErrorEvent ;
25+ import com .proxerme .library .event .error .ErrorEvent ;
26+ import com .proxerme .library .event .error .LoginErrorEvent ;
27+ import com .proxerme .library .event .error .LogoutErrorEvent ;
28+ import com .proxerme .library .event .error .NewsErrorEvent ;
29+ import com .proxerme .library .event .success .ConferencesEvent ;
30+ import com .proxerme .library .event .success .LoginEvent ;
31+ import com .proxerme .library .event .success .LogoutEvent ;
32+ import com .proxerme .library .event .success .NewsEvent ;
2833
2934import org .json .JSONException ;
3035import org .json .JSONObject ;
@@ -174,7 +179,7 @@ public static void cleanup() {
174179 *
175180 * @param <T> The type of result, the inheriting request will return.
176181 */
177- public static abstract class ProxerRequest <T , E extends IEvent > {
182+ public static abstract class ProxerRequest <T , E extends IEvent , EE extends ErrorEvent > {
178183
179184 /**
180185 * Builds the request, to be used in the
@@ -214,16 +219,16 @@ public void run() {
214219 JSONObject json = response .asJsonObject ();
215220
216221 if (json == null ) {
217- EventBus .getDefault ().post (new ProxerException (UNKNOWN ));
222+ EventBus .getDefault ().post (createErrorEvent ( new ProxerException (UNKNOWN ) ));
218223 } else {
219224 final E result = createEvent (parse (json ));
220225
221226 EventBus .getDefault ().post (result );
222227 }
223228 } catch (final JSONException e ) {
224- EventBus .getDefault ().post (ErrorHandler .handleException (e ));
229+ EventBus .getDefault ().post (createErrorEvent ( ErrorHandler .handleException (e ) ));
225230 } catch (final BridgeException e ) {
226- EventBus .getDefault ().post (ErrorHandler .handleException (e ));
231+ EventBus .getDefault ().post (createErrorEvent ( ErrorHandler .handleException (e ) ));
227232 }
228233
229234 parseThreads .remove (getThread ());
@@ -234,7 +239,7 @@ public void run() {
234239 parseThread .start ();
235240 } else {
236241 if (exception .reason () != BridgeException .REASON_REQUEST_CANCELLED ) {
237- EventBus .getDefault ().post (ErrorHandler .handleException (exception ));
242+ EventBus .getDefault ().post (createErrorEvent ( ErrorHandler .handleException (exception ) ));
238243 }
239244 }
240245 }
@@ -276,12 +281,14 @@ public final T executeSynchronized() throws ProxerException {
276281 protected abstract T parse (@ NonNull JSONObject response ) throws JSONException ;
277282
278283 protected abstract E createEvent (@ NonNull T result );
284+
285+ protected abstract EE createErrorEvent (@ NonNull ProxerException exception );
279286 }
280287
281288 /**
282289 * A request, returning a List of {@link News}.
283290 */
284- public static class NewsRequest extends ProxerRequest <List <News >, NewsEvent > {
291+ public static class NewsRequest extends ProxerRequest <List <News >, NewsEvent , NewsErrorEvent > {
285292
286293 private int page ;
287294
@@ -310,12 +317,17 @@ protected List<News> parse(@NonNull JSONObject response) throws JSONException {
310317 protected NewsEvent createEvent (@ NonNull List <News > result ) {
311318 return new NewsEvent (result );
312319 }
320+
321+ @ Override
322+ protected NewsErrorEvent createErrorEvent (@ NonNull ProxerException exception ) {
323+ return new NewsErrorEvent (exception );
324+ }
313325 }
314326
315327 /**
316328 * A request for the login, returning a {@link LoginUser} on success.
317329 */
318- public static class LoginRequest extends ProxerRequest <LoginUser , LoginEvent > {
330+ public static class LoginRequest extends ProxerRequest <LoginUser , LoginEvent , LoginErrorEvent > {
319331
320332 private LoginUser user ;
321333
@@ -351,12 +363,17 @@ protected LoginUser parse(@NonNull JSONObject response) throws JSONException {
351363 protected LoginEvent createEvent (@ NonNull LoginUser result ) {
352364 return new LoginEvent (result );
353365 }
366+
367+ @ Override
368+ protected LoginErrorEvent createErrorEvent (@ NonNull ProxerException exception ) {
369+ return new LoginErrorEvent (exception );
370+ }
354371 }
355372
356373 /**
357374 * A request for the logout.
358375 */
359- public static class LogoutRequest extends ProxerRequest <Void , LogoutEvent > {
376+ public static class LogoutRequest extends ProxerRequest <Void , LogoutEvent , LogoutErrorEvent > {
360377
361378 @ NonNull
362379 @ Override
@@ -379,12 +396,18 @@ protected Void parse(@NonNull JSONObject response) throws JSONException {
379396 protected LogoutEvent createEvent (@ NonNull Void result ) {
380397 return new LogoutEvent ();
381398 }
399+
400+ @ Override
401+ protected LogoutErrorEvent createErrorEvent (@ NonNull ProxerException exception ) {
402+ return new LogoutErrorEvent (exception );
403+ }
382404 }
383405
384406 /**
385407 * A request for retrieval of the {@link Conference}s of a user.
386408 */
387- public static class ConferencesRequest extends ProxerRequest <List <Conference >, ConferencesEvent > {
409+ public static class ConferencesRequest extends ProxerRequest <List <Conference >,
410+ ConferencesEvent , ConferencesErrorEvent > {
388411
389412 private int page ;
390413
@@ -412,6 +435,11 @@ protected List<Conference> parse(@NonNull JSONObject response) throws JSONExcept
412435 protected ConferencesEvent createEvent (@ NonNull List <Conference > result ) {
413436 return new ConferencesEvent (result );
414437 }
438+
439+ @ Override
440+ protected ConferencesErrorEvent createErrorEvent (@ NonNull ProxerException exception ) {
441+ return new ConferencesErrorEvent (exception );
442+ }
415443 }
416444
417445 private static class ParseThread extends Thread {
0 commit comments