I have a class that parses a stream of data. Each chunk of data is called a Box
. There are many different kinds of Box
es. I want to have a different Parser
for each type of box. So basically I need a Registry
or something like one that will let me pull out the right parser for each Box
. Here is a simplified version of my problem:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GenericsTest {
class Box {
private String data;
public String getData() {
return data;
}
}
class BoxA extends Box {
private String adata;
BoxA( String adata ) {
this.adata = adata;
}
public String getAData() {
return adata;
}
}
class BoxB extends Box {
private String bdata;
BoxB( String bdata ) {
this.bdata = bdata;
}
public String getBData() {
return bdata;
}
}
interface Parser<T> {
public void parse( T box );
}
class ParserA implements Parser<BoxA> {
@Override
public void parse( BoxA box ) {
System.out.print( "BoxA: " + box.getAData() );
}
}
class ParserB implements Parser<BoxB> {
@Override
public void parse( BoxB box ) {
System.out.print( "BoxB: " + box.getBData() );
}
}
class Registry {
Map<Class<?>, Parser<?>> unsafeMap = new HashMap<>();
<T extends Box, S extends Parser<T>> void add( Class<T> clazz, S parser ) {
unsafeMap.put( clazz, parser );
}
<T extends Box> boolean containsKey( Class<T> clazz ) {
return unsafeMap.containsKey( clazz );
}
@SuppressWarnings( "unchecked" )
<T extends Box, S extends Parser<T>> S get( Class<T> clazz ) {
return (S) unsafeMap.get( clazz );
}
}
public void runTest() {
Registry registry = new Registry();
registry.add( BoxA.class, new ParserA() );
registry.add( BoxB.class, new ParserB() );
List<Box> boxes = new ArrayList<>();
boxes.add( new BoxA( "Silly" ) );
boxes.add( new BoxB( "Funny" ) );
boxes.add( new BoxB( "Foo" ) );
boxes.add( new BoxA( "Bar" ) );
for ( Box box : boxes ) {
Class<? extends Box> clazz = box.getClass();
registry.get( clazz ).parse( clazz.cast( box ) );
}
}
public static void main( String[] args ) {
new GenericsTest().runTest();
}
}
If you take that code and try to compile it, you see this error:
The method parse(capture#4-of ? extends GenericsTest.Box) in the type GenericsTest.Parser is not applicable for the arguments (capture#5-of ? extends GenericsTest.Box)
So the question is, how does
(capture#4-of ? extends GenericsTest.Box)
differ from
(capture#5-of ? extends GenericsTest.Box)
?
And, is there a better way than my Registry
approach that would not require the use of the @SuppressWarnings( "unchecked" )
?