Created
June 22, 2016 17:11
-
-
Save lbalmaceda/f6112fe94c03c7dbbb28df271e583a3e to your computer and use it in GitHub Desktop.
Generics with Factory as Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lbalmaceda.genericsproject; | |
import com.lbalmaceda.genericsproject.exceptions.BaseException; | |
import java.io.Reader; | |
/** | |
* Created by lbalmaceda on 6/22/16. | |
*/ | |
public interface ErrorFactory<U extends BaseException> { | |
U from(Exception exception); | |
U from(String message, Exception exception); | |
U from(Reader jsonReader); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lbalmaceda.genericsproject.exceptions; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
public class AuthException extends BaseException { | |
public AuthException(String message) { | |
super(message); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lbalmaceda.genericsproject.exceptions; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
public class BaseException extends Exception { | |
public BaseException(String message) { | |
super(message); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lbalmaceda.genericsproject.exceptions; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
public class MgmtException extends BaseException { | |
public MgmtException(String message) { | |
super(message); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
import java.io.Reader; | |
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<>(authErrorFactory, simpleCallback); | |
authTRequest.start(); | |
VoidRequest<MgmtException> voidRequest = new VoidRequest<>(mgmtErrorFactory, 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"); | |
} | |
}; | |
private ErrorFactory<AuthException> authErrorFactory = new ErrorFactory<AuthException>() { | |
@Override | |
public AuthException from(Exception exception) { | |
return null; | |
} | |
@Override | |
public AuthException from(String message, Exception exception) { | |
return null; | |
} | |
@Override | |
public AuthException from(Reader jsonReader) { | |
return null; | |
} | |
}; | |
private ErrorFactory<MgmtException> mgmtErrorFactory = new ErrorFactory<MgmtException>() { | |
@Override | |
public MgmtException from(Exception exception) { | |
return null; | |
} | |
@Override | |
public MgmtException from(String message, Exception exception) { | |
return null; | |
} | |
@Override | |
public MgmtException from(Reader jsonReader) { | |
return null; | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> errorGeneration; | |
private final CustomCallback<T, U> callback; | |
public BaseRequest(ErrorFactory<U> errorGeneration, CustomCallback<T, U> callback) { | |
this.errorGeneration = errorGeneration; | |
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 = errorGeneration.from(ex); | |
callback.onFailure(err); | |
} | |
} | |
}; | |
protected abstract T generateSuccess(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ErrorFactory<U> errorFactory, CustomCallback<T, U> callback) { | |
super(errorFactory, callback); | |
} | |
@Override | |
protected T generateSuccess() { | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ErrorFactory<U> errorFactory, CustomCallback<Void, U> callback) { | |
super(errorFactory, callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment