I’ve implemented a little helper class that provides an easy fail-safe implementation of the enum’s valueOf method. This means in case the value is not found, it returns null instead of an exception.
Here’s the code:
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* <p>
* This permits to easily implement a failsafe implementation of the enums's valueOf method
* </p>
*
* <p>
* Basic usage exemple on an enum class called MyEnum:
* FailSafeValueOf.get(MyEnum.class).valueOf("EnumName");
* </p>
*
* @author Sebastien Lorber <i>(lorber.sebastien@gmail.com)</i>
*/
public class FailSafeValueOf<T extends Enum<T>> implements Serializable {
/**
* This will cache a FailSafeValueOf for each enum so that we do not need to recompute a map each time
*/
private static final Map< Class<? extends Enum<?>> , FailSafeValueOf<? extends Enum<?>> > CACHE = Maps.newHashMap();
private final Map<String,T> nameToEnumMap;
private FailSafeValueOf(Class<T> enumClass) {
Map<String,T> map = Maps.newHashMap();
for ( T value : EnumSet.allOf(enumClass)) {
map.put( value.name() , value);
}
this.nameToEnumMap = ImmutableMap.copyOf(map);
}
/**
* Returns the value of the given enum element
* If the element is not found, null will be returned, and no exception will be thrown
* @param enumName
* @return
*/
public T valueOf(String enumName) {
return nameToEnumMap.get(enumName);
}
/**
* Get a failsafe value of implementation for a given enum
* @param enumClass
* @param <U>
* @return
*/
public static <U extends Enum<U>> FailSafeValueOf<U> get(Class<U> enumClass) {
FailSafeValueOf<U> fsvo = (FailSafeValueOf<U>)CACHE.get(enumClass);
if ( fsvo == null ) {
synchronized (FailSafeValueOf.class) {
fsvo = (FailSafeValueOf<U>)CACHE.get(enumClass);
if ( fsvo == null ) {
fsvo = new FailSafeValueOf<U>(enumClass);
CACHE.put(enumClass,fsvo);
}
}
}
return fsvo;
}
}
Because i don’t want the (little) overhead of creating a new FailSafeValueOf at each access, i’ve made a cache that keeps for each enum already accessed an already built FailSafeValueOf instance.
I’m not used to handle concurrency. A concurrent access may not be a big problem in such a case as FailSafeValueOf is immutable and 2 different instances of FailSafeValueOf can be returned by the get method for a same enum.
But i’d like to know if my implementation of thread-safety is the way to do, and if it’s really thread-safe? (mostly for learning purpose)
I don’t want to make my method synchronized because after some time, all FailSafeValueOf are created in the cache, and there is no need to forbid concurrent threads to enter the get method.
So what i’ve made is to check first if there’s a cache miss, and then create a synchronized block that will atomically: check again the cache and eventually create the instance.
Is it thread-safe and the way to do for such a need?
By the way, enums often have a small number of values. In such a case, is the HashMap an appropriate structure? Is it faster to iterate over the EnumSet and get the appropriate value, instead of using a cache?
Edit:
Please notice that my class is not so useful because the Guava team has releated a method Enums.getIfPresent() which returns an Optional
Your class is not thread-safe because you are not synchronizing around the
CACHE.get()method. This assumes thatMaps.newHashMap()returns aHashMapand not aConcurrentHashMapclass. You can see this in this code snippet:You would need to move the
synchronizedaround theCACHE.get(...)or switch to using aConcurrentHashMapif that method is called frequently. The problem is that theHashMapwould be updated by another thread while the current thread is reading from it — this can easily cause problems because of race conditions.Although slightly different, you should also look into the class “Double Check Locking” documentation to understand more the difficulties about trying to save yourself from synchronization like this.
Lastly, I would synchronize on the
CACHEobject instead of the class which is not recommended unless you really need to have that lock granularity.