Last active
June 10, 2016 16:19
-
-
Save frontrangerider2004/9d0fbbd81def3c3306287cecba6ce198 to your computer and use it in GitHub Desktop.
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.frontrangrider; | |
import android.util.SparseArray; | |
import java.util.EnumSet; | |
import java.util.HashMap; | |
import java.util.Map; | |
public enum KeyValueEnum { | |
FIRST(1, "FIRST"), | |
SECOND(2, "SECOND"), | |
DEFAULT(3, "DEFAULT") //The default return so we don't return null | |
; | |
//The package prefix is here in case these enums are used for Android Intents, which maintains uniqueness. | |
private static final String PACKAGE_PREFIX = "com.frontrangrider."; | |
private final int key; | |
private final String value; | |
/* | |
Create two maps, one with key, enum and the other with value, enum for doing lookups. | |
We use SparseArray for Android cases where we can map an integer to an object since | |
according to Google's documentation this is more optimized. | |
*/ | |
private static final SparseArray<KeyValueEnum> lookupByInt = new SparseArray<KeyValueEnum>(); | |
private static final Map<String , KeyValueEnum> lookupByString = new HashMap<String, KeyValueEnum>(); | |
//Replace the above with these for normal Java since SparseArray is Android Specific | |
// //Create two maps, one with key, enum and the other with value, enum for doing lookups | |
// private static final Map<Integer, KeyValueEnum> lookupByInt = new HashMap<Integer, KeyValueEnum>(); | |
// private static final Map<String , KeyValueEnum> lookupByString = new HashMap<String, KeyValueEnum>(); | |
//Put all the key, value pairs into a map so we can do lookups | |
static { | |
for(KeyValueEnum action : EnumSet.allOf(KeyValueEnum.class)){ | |
lookupByInt.put(action.getKey(), action); | |
lookupByString.put(action.getValue(), action); | |
} | |
} | |
/** | |
* Initializes each item in the Enum with an integer key and string value. | |
* @param key | |
* @param value | |
*/ | |
private KeyValueEnum(int key, String value){ | |
this.key = key; | |
this.value = PACKAGE_PREFIX + value; | |
} | |
/** | |
* Returns the String value of the enum | |
* @return | |
*/ | |
public String getValue(){ | |
return this.value; | |
} | |
/** | |
* Returns the integer key of the enum | |
* @return | |
*/ | |
public int getKey(){ | |
return this.key; | |
} | |
/** | |
* Attempts to find the enum constant matching the supplied | |
* integer key. If a match cannot be found this method will | |
* return the default enum value. | |
* @param key | |
* @return | |
*/ | |
public static KeyValueEnum lookupByKey(int key){ | |
return lookupByInt.get(key, KeyValueEnum.DEFAULT); | |
} | |
/** | |
* Attempts to find the enum constant matching the supplied | |
* string value. If a match cannot be found this method will | |
* return the default enum value. | |
* @param value | |
* @return | |
*/ | |
public static KeyValueEnum lookupByValue(String value){ | |
KeyValueEnum result = lookupByString.get(value); | |
if(result == null){ | |
result = KeyValueEnum.DEFAULT; | |
} | |
return result; | |
} | |
}//End Enum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment