A
allen
Hi,
Continuing with my exploration of generics, I found the following could
be useful. I wish to get some feedback as to whether this is a terrible
idea, and why. Note that this is a slightly contrived example, so don't
challenge the assumed requirement, rather criticise the implementation.
The idea is that I wish to store a potentially nested collection of
key=>values. The type of value is not constant, the map might hold a
mixture of Integer, String, Double or nested maps. Only the client code
knows the value type for a given key.
class GenericMap
{
private Map<String, Object> map = new HashMap<String, Object>( );
/**Allows the client to specify the type he expects
*back for a given id.
*Any feedback?
*/
public <E> E get( String id )
{
// required cast
return (E) map.get( id );
}
//We can put anything into the map, so no generics used here.
public void put( String id, Object obj )
{
map.put( id, obj );
}
}
// client code:
GenericMap gm = new GenericMap( );
gm.put( "id1", new GenericMap( ) );
gm.<GenericMap>get( "id1" ).put( "id2", new Integer( 5 ) );
System.out.println(
gm.<GenericMap>get( "id1" ).<Integer>get( "id2" ).intValue( ) );
gm.put( "id2", "StringValue" );
System.out.println( gm.<String>get( "id2" ) );
I know that at runtime, the same amount of casting will take place to
access a specific entry. However at compile time it can make the code
look tidier (although I guess beauty is in the eye of the beholder and
all that). There is only 1 line that will generate the unchecked
warning, and the casting is slightly less cumbersome.
Now for eclipse (3.1.0), given that I wish to implement the above code.
An advantage to the above code is that content assist should be
available as you type in the line
gm.<GenericMap>get("id1").<GenericMap>get("id2").<Integer>get("id3").intValue(
);
However it seems to struggle with giving me anything useful. Any
comments, or should I log this as a bug?
thanks
Allen
Continuing with my exploration of generics, I found the following could
be useful. I wish to get some feedback as to whether this is a terrible
idea, and why. Note that this is a slightly contrived example, so don't
challenge the assumed requirement, rather criticise the implementation.
The idea is that I wish to store a potentially nested collection of
key=>values. The type of value is not constant, the map might hold a
mixture of Integer, String, Double or nested maps. Only the client code
knows the value type for a given key.
class GenericMap
{
private Map<String, Object> map = new HashMap<String, Object>( );
/**Allows the client to specify the type he expects
*back for a given id.
*Any feedback?
*/
public <E> E get( String id )
{
// required cast
return (E) map.get( id );
}
//We can put anything into the map, so no generics used here.
public void put( String id, Object obj )
{
map.put( id, obj );
}
}
// client code:
GenericMap gm = new GenericMap( );
gm.put( "id1", new GenericMap( ) );
gm.<GenericMap>get( "id1" ).put( "id2", new Integer( 5 ) );
System.out.println(
gm.<GenericMap>get( "id1" ).<Integer>get( "id2" ).intValue( ) );
gm.put( "id2", "StringValue" );
System.out.println( gm.<String>get( "id2" ) );
I know that at runtime, the same amount of casting will take place to
access a specific entry. However at compile time it can make the code
look tidier (although I guess beauty is in the eye of the beholder and
all that). There is only 1 line that will generate the unchecked
warning, and the casting is slightly less cumbersome.
Now for eclipse (3.1.0), given that I wish to implement the above code.
An advantage to the above code is that content assist should be
available as you type in the line
gm.<GenericMap>get("id1").<GenericMap>get("id2").<Integer>get("id3").intValue(
);
However it seems to struggle with giving me anything useful. Any
comments, or should I log this as a bug?
thanks
Allen