Skip to main content


Showing posts from February, 2012

Guava: ClassToInstanceMap

If you have requirement of associating the class to an instance of it’s type then ClassToInstanceMap is the best choice for you. It comes with additional type safety.

It’s an extended interface of Map

public interface ClassToInstanceMap<B> extends Map<Class<? extends B>,B>
ImmutableClassToInstanceMap, MutableClassToInstanceMap
Here is a example where you can use this map
Some classes 

interface Writer{ //SOME CODE } class TemplateWriter implements Writer{ //SOME CODE } class PropertyWriter implements Writer{ //SOME CODE } class PropertySetWriter implements Writer{ //SOME CODE }
And we have one Factory class

class WriterFactory{ private static final ClassToInstanceMap<Writer> factoryMap = new ImmutableClassToInstanceMap.Builder<Writer>(). put(TemplateWriter.class, new TemplateWriter()). put(PropertyWriter.class, new PropertyWriter()). put(PropertySetWriter.class, new PropertySetWriter()). build(); public static Wr…