Last active
November 14, 2015 04:03
-
-
Save billmote/b486341bf87e7df1fca7 to your computer and use it in GitHub Desktop.
Enum variant to handle switching of environments. I had previously implemented PushConfig.java as an abstract class and had QaPushConfig.java and ProdPushConfig.java extend them, but that resulted in ugly switch-case logic everywhere I wanted to use them. Patrick refactored that implementation into what you see below.
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
| public class MyApplication extends Application { | |
| public static final PushConfig.Environment ENVIRONMENT = PushConfig.Environment.PROD; // can also be .QA | |
| private static final String TAG = "MyApplication"; | |
| @Override | |
| public void onCreate() { | |
| super.onCreate(); | |
| final PushConfig pushConfig = ENVIRONMENT.getPushConfig(); | |
| String appId = pushConfig.etAppId; | |
| String url = pushConfig.restEndPoint; | |
| String gcmSenderId = pushConfig.gcmSenderId; | |
| // Do something with those values ... | |
| } | |
| } |
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
| /** | |
| * Just a helper class to validate data. Can be largely ignored. | |
| * | |
| * Created by billmote on 4/26/15. | |
| */ | |
| public final class Preconditions { | |
| private Preconditions() { | |
| throw new AssertionError("No instances."); | |
| } | |
| public static void checkArgument(boolean expression, Object errorMessage) { | |
| if (!expression) { | |
| throw new IllegalArgumentException(String.valueOf(errorMessage)); | |
| } | |
| } | |
| public static void checkState(boolean expression, Object errorMessage) { | |
| if (!expression) { | |
| throw new IllegalStateException(String.valueOf(errorMessage)); | |
| } | |
| } | |
| public static <T> T checkNotNull(T reference, String name) { | |
| if (reference == null) { | |
| throw new NullPointerException(name + " must not be null"); | |
| } | |
| return reference; | |
| } | |
| public static <T extends CharSequence> T checkNotBlank(T value, String name) { | |
| if (TextUtils.isEmpty(value)) { | |
| throw new IllegalArgumentException(name + " must not be blank"); | |
| } | |
| return value; | |
| } | |
| } |
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
| /** | |
| * Created by bmote on 4/16/15. | |
| */ | |
| public abstract class PushConfig { | |
| public String gcmSenderId = "0123456789"; | |
| public String restEndPoint; | |
| public String etAppId; | |
| public void validateFields() { | |
| Preconditions.checkNotBlank(gcmSenderId, "PushConfig.gcmSenderId"); | |
| Preconditions.checkNotBlank(restEndPoint, "PushConfig.restEndPoint"); | |
| Preconditions.checkNotBlank(etAppId, "PushConfig.etAppId"); | |
| } | |
| public enum Environment { | |
| QA { | |
| @Override | |
| public PushConfig getPushConfig() { | |
| return new QaPushConfig(); | |
| } | |
| }, | |
| PROD { | |
| @Override | |
| public PushConfig getPushConfig() { | |
| return new ProdPushConfig(); | |
| } | |
| }; | |
| public abstract PushConfig getPushConfig(); | |
| } | |
| private static class QaPushConfig extends PushConfig { | |
| private static final String TAG = "QaPushConfig"; | |
| public QaPushConfig() { | |
| Log.i(TAG, String.format("Using %1$s Configuration", TAG)); | |
| restEndPoint = "http://qa.mysite.com/"; | |
| etAppId = "some-crazy-guid"; | |
| validateFields(); | |
| } | |
| } | |
| private static class ProdPushConfig extends PushConfig { | |
| private static final String TAG = "ProdPushConfig"; | |
| public ProdPushConfig() { | |
| Log.i(TAG, String.format("Using %1$s Configuration", TAG)); | |
| restEndPoint = "http://www.mysite.com/"; | |
| etAppId = "some-other-crazy-id"; | |
| validateFields(); | |
| } | |
| } | |
| } |
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
| public class SomeClass { | |
| public static final String TAG = "ServiceEventHandler"; | |
| private final PushConfig pushConfig; | |
| public SomeClass() { | |
| pushConfig = MyApplication.ENVIRONMENT.getPushConfig(); | |
| } | |
| /** | |
| * Get stuff | |
| */ | |
| public void getStuff(final GetStuffRequest request) { | |
| // use pushConfig.restEndPoint or others here :) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment