Map of generic Java class <T> in Parser <T>
I have a class that parses a data stream. Each piece of data is called Box. There are many different types of Boxes. I want to have different Parserwindows for each type. So basically, I need Registryor something like that that will allow me to pull out the correct 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 this code and try to compile it, you will see this error:
Method parsing (capture # 4-of? Extends GenericsTest.Box) in the GenericsTest.Parser type is not applicable for arguments (capture # 5-of? Extends GenericsTest.Box)
So the question is how
(capture#4-of ? extends GenericsTest.Box)
differ from
(capture#5-of ? extends GenericsTest.Box)
?
And is there a better way than my approach Registrythat does not require use @SuppressWarnings( "unchecked" )?
-, OP. (capture#4-of ? extends GenericsTest.Box) (capture#5-of ? extends GenericsTest.Box)?
, , registry.get(), Class<x> x Box. , T get() x , Parser<x> x Box. ( , "capture # 4-of?" " x4 , x4".) .
, , ( ), , . "" , , .
:
public class WildcardTest {
private < T > void two( Class< T > t1, Class< T > t2 ) {}
private < T > void one( Class< T > t1 ) {
two( t1, t1 ); // compiles; no wildcards involved
}
private void blah() {
two( WildcardTest.class, WildcardTest.class ); // compiles
one( WildcardTest.class ); // compiles
Class< ? extends WildcardTest > wc = this.getClass();
two( wc, wc ); // won't compile! (capture#2 and capture#3)
one( wc ); // compiles
}
}
:
public class WildcardTest {
interface Thing< T > {
void consume( T t );
}
private < T > Thing< T > make( Class< T > c ) {
return new Thing< T >() {
@Override public void consume(T t) {}
};
}
private < T > void makeAndConsume( Object t, Class< T > c ) {
make( c ).consume( c.cast( t ) );
}
private void blah() {
Class< ? extends WildcardTest > wc = this.getClass();
make( wc ).consume( wc.cast( this ) ); // won't compile! (capture#2 and capture#3)
makeAndConsume( this, wc ); // compiles
}
}
- . , , :
private < T extends Box > void getParserAndParse(
Registry registry, Class< T > clazz, Object box
) {
registry.get( clazz ).parse( clazz.cast( box ) );
}
public void runTest() {
Registry registry = new Registry();
registry.add( BoxA.class, new ParserA() );
registry.add( BoxB.class, new ParserB() );
List<Box> boxes = new ArrayList< Box >();
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();
getParserAndParse( registry, clazz, box ); // compiles
}
}
, ad-hoc- , (Box). :
- OO (
parseSelfBox), ,BoxAPI - , :
Box, , OOBoxes Visitor
, "" . , , , , , (, , ).
, clazz , clazz , , , , .
, , T, . , , - .
private <T extends Box> void helperMethod(Class<T> clazz, Box box, Registry registry) {
registry.get( clazz ).parse( clazz.cast( box ) );
}
// then you use it like in the place you had before:
for ( Box box : boxes ) {
Class<? extends Box> clazz = box.getClass();
helperMethod(clazz, box, registry);
}
. , get S, , , Parser<T> , . ? :
@SuppressWarnings( "unchecked" )
<T extends Box> Parser<T> get( Class<T> clazz ) {
return (Parser<T>) unsafeMap.get( clazz );
}
, add . ( ):
<T extends Box> void add( Class<T> clazz, Parser<T> parser ) {
unsafeMap.put( clazz, parser );
}