I am having a little bit of an issue here and it is possible due to me being new to using Java generics, I have a StateMachine class and State interface that use generics, since I coded them to be used with anything that requires state handling.
So my State interface looks like this:
public interface State<T> {
/**
* Called when the {@link StateMachine} changes to this state
*
* @param object
*/
void enter(T object);
/**
* Called in the {@link StateMachine} update method, the actual state should
* be updated in here
*
* @param object
*/
void execute(T object);
/**
* Called when the {@link StateMachine} changes to another state
*
* @param object
*/
void exit(T object);
}
Very straight forward, so a user can implement their own states, something like this:
public class TestState implements State<Player> {
@Override
public void enter(Player arg0) {
}
@Override
public void execute(Player arg0) {
}
@Override
public void exit(Player arg0) {
}
}
Now the problem is, I have the StateMachine with fields like this:
/**
* The default {@link State}, should be set to the Objects default
* {@link State} to avoid a stateless object
*/
State defaultState;
/**
* The current {@link State} the object is in, this state will be executed
* in the StateMachines update method
*/
State currentState;
/**
* The previous {@link State}, when the Objects {@link State} is changed,
* the one that was set before is automatically put in this variable
*/
State previousState;
Which flags a whole lot of yellow, as I am not using generic arguments. Now any one of those states could be of any type, so how should I go about this?
My first thought would be using <?> since technically the type is unknown at this time, but that does not seem to be the solution. After reading through the Oracle tutorials I can’t seem to figure out how to approach this.
I could leave it all flagged yellow, it still works but it looks horrid with a ton of suppress warning annotations.
Baring in mind I also have a bunch of methods that alter these State fields, such as this :
public void changeState(State newState) {
/* If the StateMachine is locked, can't change State */
if (isLocked)
return;
/*
* If the newState is the same as the currentState, no point in
* continuing
*/
if (newState == currentState)
return;
/* Set the previous state to the current state */
previousState = currentState;
/* If the current state is not null, we call its exit method */
if (currentState != null)
currentState.exit(owner);
/* Set the current state to the new state */
currentState = newState;
/* If the current state is not null, enter it */
if (currentState != null)
currentState.enter(owner);
}
What kind of generic argument should I be using for method parameters? I am not sure at all.