I have a toString( Object ) method that delegates the conversion to handlers. Handlers are defined as follows:
public interface IToStringService<T> { public String toString( T value ); }
The code is as follows:
// (1) How can I say that these two wildcards must in fact be the same type? private Map<Class<?>, IToStringService<?>> specialHandlers = Maps.newHashMap(); // Generic method, must accept Object (any type really) @Override public String toString( Object value ) { if( null == value ) { return "null"; } Class<?> type = value.getClass(); if( type.isArray() ) { return arrayToString( value ); } // (2) How can I get rid of this SuppressWarnings? @SuppressWarnings( "unchecked" ) IToStringService<Object> handler = (IToStringService<Object>) specialHandlers.get( type ); if( null != handler ) { return handler.toString( value ); } return value.toString(); } public <T> void addSpecialHandler( Class<T> type, IToStringService<T> handler ) { specialHandlers.put( type, handler ); }
And one implementation looks like this:
@SuppressWarnings( "rawtypes" )
I have a few problems here:
How can I say that the handlers on the specialHandlers map must match the type used as the key?
How can I use the same information inside a method to avoid casting and @SuppressWarnings ?
When I change the ClassToStringService to implement IToStringService<Class<?>> , I get a compilation error when addSpecialHandler( Class.class, new ClassToStringService() ); How to solve this?
source share