So I have a series of related interfaces one that contains other 3 (enforced by setters and getters).
While instances of this interfaces are OK to remain as just interface contracts while they are managed (stored and retrieved from a map); when they reach a processing bean they need to be able to reveal them-self as the implemented class they are.
So I was able to this doing the following:
public interface IState<S extends IState> {
<G extends GameData> G getGameData();
<G> S setGameData(G gameDataIO);
<P> P getPlayer();
<P> S setPlayer(P player);
<GS> GS getGameState();
<GS> S setGameState(GS gameState);}
The default implementation:
public class DefaultStateNG implements IStateNG<DefaultStateNG> {
private GameData gameDataIO;
private IPlayerState player;
private IGameState gameState;
@Override
public <G extends GameData> G getGameData() {
return (G) gameDataIO;
}
@Override
public <G> DefaultStateNG setGameData(G gameDataIO) {
this.gameDataIO = (GameData) gameDataIO;
return this;
} //..... other accessor methods implemented
So when I use them in the processor bean they do not need to be casted into the appropriate implementation of the interface.
The problem arises when a unchecked cast in throw when casting in the returned type. I don't know how to handle such warning. I mean I am setting gameDataIO as Game data type and the return is supposed to be a GameData type. Why the warning?. I tried every kind of casting, the only one that works without warning so far is is pass the expected class as argument and then use type.cast(returnedObject).
Any hint will be appreciated.