Created
June 21, 2016 19:41
-
-
Save lbalmaceda/1e0a3ce58ad3bc3344fc5607b3eaa5eb to your computer and use it in GitHub Desktop.
Generics
This file contains 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.CustomException; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
public interface CustomCallback<T, U extends CustomException> { | |
void onSuccess(T response); | |
void onFailure(U error); | |
} |
This file contains 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 BarException extends CustomException{ | |
public BarException(String message) { | |
super(message); | |
} | |
} |
This file contains 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 CustomException extends Exception { | |
public CustomException(String message) { | |
super(message); | |
} | |
} |
This file contains 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 FooException extends CustomException{ | |
public FooException(String message) { | |
super(message); | |
} | |
} |
This file contains 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.BarException; | |
import com.lbalmaceda.genericsproject.exceptions.FooException; | |
import com.lbalmaceda.genericsproject.requests.BarRequest; | |
import com.lbalmaceda.genericsproject.requests.FooRequest; | |
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()); | |
FooRequest<String> simpleRequest = new FooRequest<>(simpleCallback); | |
simpleRequest.start(); | |
BarRequest voidRequest = new BarRequest(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, FooException> simpleCallback = new CustomCallback<String, FooException>() { | |
@Override | |
public void onSuccess(String response) { | |
Log.e(TAG, "OnSuccess for SimpleCallback"); | |
showToast("SimpleRequest fine!"); | |
} | |
@Override | |
public void onFailure(FooException error) { | |
Log.e(TAG, "OnFailure for SimpleCallback"); | |
} | |
}; | |
private CustomCallback<Void, BarException> voidCallback = new CustomCallback<Void, BarException>() { | |
@Override | |
public void onSuccess(Void response) { | |
Log.e(TAG, "OnSuccess for VoidCallback"); | |
showToast("VoidRequest fine!"); | |
} | |
@Override | |
public void onFailure(BarException error) { | |
Log.e(TAG, "OnFailure for VoidCallback"); | |
} | |
}; | |
} |
This file contains 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.exceptions.BarException; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
public class BarRequest extends BaseRequest<Void, BarException> { | |
public BarRequest(CustomCallback<Void, BarException> callback) { | |
super(callback); | |
} | |
@Override | |
protected Void generateSuccess() { | |
return null; | |
} | |
@Override | |
protected BarException generateException() { | |
return new BarException("BarBar"); | |
} | |
} |
This file contains 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.exceptions.CustomException; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
abstract class BaseRequest<T, U extends CustomException> { | |
private static final String TAG = BaseRequest.class.getSimpleName(); | |
private final CustomCallback<T, U> callback; | |
public BaseRequest(CustomCallback<T, U> callback) { | |
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); | |
callback.onSuccess(generateSuccess()); | |
Log.e(TAG, "Thread ended fine"); | |
} catch (InterruptedException ignored) { | |
Log.e(TAG, "Thread ended with error"); | |
callback.onFailure(generateException()); | |
} | |
} | |
}; | |
protected abstract T generateSuccess(); | |
protected abstract U generateException(); | |
} |
This file contains 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.exceptions.FooException; | |
/** | |
* Created by lbalmaceda on 6/21/16. | |
*/ | |
public class FooRequest<T> extends BaseRequest<T, FooException>{ | |
public FooRequest(CustomCallback<T, FooException> callback) { | |
super(callback); | |
} | |
@Override | |
protected T generateSuccess() { | |
return null; | |
} | |
@Override | |
protected FooException generateException() { | |
return new FooException("BarBar"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment