Have you ever wondered how to map enums from one to another ? Did you use switch statement to make it work ? Now you can use method immutableEnumMap from class Maps which is part of Guava. Below you can find an example.
public class EnumSourceMapper { static ImmutableMap<EnumSource, EnumDestination> mapper; static { mapper = Maps.immutableEnumMap(ImmutableMap.<EnumSource, EnumDestination>builder() .put(EnumSource.SUCCESS, EnumDestination.SUCCESS) .put(EnumSource.INCOMPLETE, EnumDestination.ERROR) .put(EnumSource.FAIL, EnumDestination.ERROR) .build()); } public EnumDestination getEnumDestination(EnumSource enumSource) { return mapper.get(enumSource); } }
Reference:
Guava Enum Mapper
2016-05-23T20:21:00+01:00
GarciaPL
Java|