Skip to content

Instantly share code, notes, and snippets.

@lbalmaceda
Created June 22, 2016 16:11
Show Gist options
  • Save lbalmaceda/d04ee0efc97aadf841fdd5bfc3f39284 to your computer and use it in GitHub Desktop.
Save lbalmaceda/d04ee0efc97aadf841fdd5bfc3f39284 to your computer and use it in GitHub Desktop.
Generics with Exception error builder
package com.lbalmaceda.genericsproject;
import com.lbalmaceda.genericsproject.exceptions.BaseException;
/**
* Created by lbalmaceda on 6/21/16.
*/
public interface CustomCallback<T, U extends BaseException> {
void onSuccess(T response);
void onFailure(U error);
}
package com.lbalmaceda.genericsproject;
import com.lbalmaceda.genericsproject.exceptions.BaseException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Created by lbalmaceda on 6/22/16.
*/
public class ErrorFactory<U extends BaseException> {
private final Class<U> uClass;
public ErrorFactory(Class<U> uClass) {
this.uClass = uClass;
}
public U from(Exception exception) {
U u = null;
try {
final Constructor<U> constructor = uClass.getDeclaredConstructor(Throwable.class);
u = constructor.newInstance(exception);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return u;
}
public U from(String message, Exception exception) {
U u = null;
try {
final Constructor<U> constructor = uClass.getDeclaredConstructor(String.class, Throwable.class);
u = constructor.newInstance(message, exception);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return u;
}
}
package com.lbalmaceda.genericsproject.exceptions;
/**
* Created by lbalmaceda on 6/21/16.
*/
public class AuthException extends BaseException {
public AuthException(String message) {
super(message);
}
}
package com.lbalmaceda.genericsproject.exceptions;
/**
* Created by lbalmaceda on 6/21/16.
*/
public class BaseException extends Exception {
public BaseException(String message) {
super(message);
}
}
package com.lbalmaceda.genericsproject.exceptions;
/**
* Created by lbalmaceda on 6/21/16.
*/
public class MgmtException extends BaseException {
public MgmtException(String message) {
super(message);
}
}
package com.lbalmaceda.genericsproject;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.lbalmaceda.genericsproject.exceptions.AuthException;
import com.lbalmaceda.genericsproject.exceptions.MgmtException;
import com.lbalmaceda.genericsproject.requests.TRequest;
import com.lbalmaceda.genericsproject.requests.VoidRequest;
public class MainActivity extends AppCompatActivity {
private static String TAG = MainActivity.class.getSimpleName();
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler(MainActivity.this.getMainLooper());
TRequest<String, AuthException> authTRequest = new TRequest<>(AuthException.class, simpleCallback);
authTRequest.start();
VoidRequest<MgmtException> voidRequest = new VoidRequest<>(MgmtException.class, voidCallback);
voidRequest.start();
}
private void showToast(final String message) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
private CustomCallback<String, AuthException> simpleCallback = new CustomCallback<String, AuthException>() {
@Override
public void onSuccess(String response) {
Log.e(TAG, "OnSuccess for SimpleCallback");
showToast("SimpleRequest fine!");
}
@Override
public void onFailure(AuthException error) {
Log.e(TAG, "OnFailure for SimpleCallback");
}
};
private CustomCallback<Void, MgmtException> voidCallback = new CustomCallback<Void, MgmtException>() {
@Override
public void onSuccess(Void response) {
Log.e(TAG, "OnSuccess for VoidCallback");
showToast("VoidRequest fine!");
}
@Override
public void onFailure(MgmtException error) {
Log.e(TAG, "OnFailure for VoidCallback");
}
};
}
package com.lbalmaceda.genericsproject.requests;
import android.os.Handler;
import android.util.Log;
import com.lbalmaceda.genericsproject.CustomCallback;
import com.lbalmaceda.genericsproject.ErrorFactory;
import com.lbalmaceda.genericsproject.exceptions.BaseException;
import java.util.Random;
/**
* Created by lbalmaceda on 6/21/16.
*/
abstract class BaseRequest<T, U extends BaseException> {
private static final String TAG = BaseRequest.class.getSimpleName();
private final ErrorFactory<U> errorFactory;
private final CustomCallback<T, U> callback;
public BaseRequest(ErrorFactory<U> errorFactory, CustomCallback<T, U> callback) {
this.errorFactory = errorFactory;
this.callback = callback;
}
public void start() {
Handler handler = new Handler();
handler.post(runnable);
}
private Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e(TAG, "Thread starting");
try {
Thread.sleep(3000);
Random r = new Random();
if (r.nextBoolean()) {
throw new InterruptedException("Fake");
}
callback.onSuccess(generateSuccess());
Log.e(TAG, "Thread ended fine");
} catch (InterruptedException ex) {
Log.e(TAG, "Thread ended with error");
final U err = errorFactory.from(ex);
callback.onFailure(err);
}
}
};
protected abstract T generateSuccess();
}
package com.lbalmaceda.genericsproject.requests;
import com.lbalmaceda.genericsproject.CustomCallback;
import com.lbalmaceda.genericsproject.ErrorFactory;
import com.lbalmaceda.genericsproject.exceptions.BaseException;
/**
* Created by lbalmaceda on 6/21/16.
*/
public class TRequest<T, U extends BaseException> extends BaseRequest<T, U> {
public TRequest(Class<U> uClass, CustomCallback<T, U> callback) {
super(new ErrorFactory<>(uClass), callback);
}
@Override
protected T generateSuccess() {
return null;
}
}
package com.lbalmaceda.genericsproject.requests;
import com.lbalmaceda.genericsproject.CustomCallback;
import com.lbalmaceda.genericsproject.ErrorFactory;
import com.lbalmaceda.genericsproject.exceptions.BaseException;
/**
* Created by lbalmaceda on 6/21/16.
*/
public class VoidRequest<U extends BaseException> extends TRequest<Void, U>{
public VoidRequest(Class<U> uClass, CustomCallback<Void, U> callback) {
super(uClass, callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment