Skip to content

Instantly share code, notes, and snippets.

@athieriot
Created November 24, 2011 19:56
Show Gist options
  • Save athieriot/1392161 to your computer and use it in GitHub Desktop.
Save athieriot/1392161 to your computer and use it in GitHub Desktop.
Little utility class to easily label any Java enum into a value in a message file
package fr.gist.util;
import java.lang.reflect.Method;
import java.util.ResourceBundle;
/*
* Auto Label enums matching a label.properties message file
*
* The message key has the format : <type>.<name>
*
* Chain:
* - if there is a getLabel method => call getLabel method
* - then if there is a match in message file => go for it
* - else => uppercase name of the enum
*/
public class EnumLabel {
private final static ResourceBundle bundle = ResourceBundle.getBundle("fr.gist.util.label");
private final static String KEY_SEPARATOR = ".";
private final static String LABEL_METHOD = "getLabel";
//TODO: The call chain should not be write in hard code
public static String autoLabel(Enum toConvert) {
return getLabel(toConvert);
}
private static String getLabel(Enum toConvert) {
Method getLabel = hasGetLabelMethod(toConvert);
if(getLabel != null) {
try {
return (String) getLabel.invoke(toConvert);
} catch (Exception e) {
return getAlternativeName(toConvert);
}
}
return getAlternativeName(toConvert);
}
private static String getAlternativeName(Enum toConvert) {
String searchKey = getEnumType(toConvert) + KEY_SEPARATOR + getEnumName(toConvert);
return bundle.containsKey(searchKey) ? bundle.getString(searchKey) : getStandardName(toConvert);
}
// Made this Class usable with every Enumeration
private static String getStandardName(Enum toConvert) {
return toConvert.name();
}
// Message file related methods
private static String getEnumName(Enum toConvert) {
return getStandardName(toConvert).toLowerCase();
}
private static String getEnumType(Enum toConvert) {
return toConvert.getDeclaringClass().getSimpleName().toLowerCase();
}
// Unit DuckTyping
private static Method hasGetLabelMethod(Enum toConvert) {
Method method = null;
try {
method = toConvert.getClass().getMethod(LABEL_METHOD);
} catch (NoSuchMethodException e) {
return method;
}
return method;
}
}
package fr.gist.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
enum BaseEnumTest {
MESSAGE, BLANK, NO_MESSAGE
}
enum LabelEnumTest {
LABEL("inLabel");
String label;
LabelEnumTest(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}
public class EnumLabelTest {
@Test
public void testGetLabel() {
// When
String availableLabel = EnumLabel.autoLabel(BaseEnumTest.MESSAGE);
// Then
assertThat(availableLabel, is("message"));
}
@Test
public void testGetLabelBlank() {
// When
String availableLabel = EnumLabel.autoLabel(BaseEnumTest.BLANK);
// Then
assertThat(availableLabel, is(""));
}
@Test
public void testGetLabelIfNoMessage() {
// When
String availableLabel = EnumLabel.autoLabel(BaseEnumTest.NO_MESSAGE);
// Then
assertThat(availableLabel, is("NO_MESSAGE"));
}
@Test
public void testGetLabelIfLabel() {
// When
String availableLabel = EnumLabel.autoLabel(LabelEnumTest.LABEL);
// Then
assertThat(availableLabel, is("inLabel"));
}
}
#BaseEnumTest
baseenumtest.message=message
baseenumtest.blank=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment