StateMachine, Java Generics issue

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.

State<?>
http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

you could use …

State<T> defaultState;
State<T> currentState;
State<T> previousState

while T is a class-generic :

public class StateMachine<T> { ... }

results in a bit clearer code.

Why not using Fettle API?

Never actually heard of that, I created the one I have atm for simplicity as it took maybe, 15 minutes to write.

If i understand exactly what u mean

you completely reverting the relationship between the state and the state machine
this is way it can not work

you want generalize the concept of State ad specialize it for T object that has to maintain 3 internal state!!!
previous, actual, next
it is a relation that has not sense in Object Oriented Programming

may be this is the implementation that you was looking for?

this is the interface Manageable that is implemented by objects that have a state


public interface Manageable{

		State getDefaultState();
		State getCurrentState();
		State getPreviousState();
	
}

this the interface of your state machine


public interface StateMachine<T extends Manageable> {

	 
	   void enter(T object);

	 
	   void execute(T object);

	 
	   void exit(T object);

	}

now you can manage a specify State Machine for each kind of object


public interface StateMachine<Player> 

and StateMachine can access to all the 3 state variables for the manage object

	State getDefaultState();
	State getCurrentState();
	State getPreviousState();

obviously Player has to implement the interface Manageable

now your internal method of the state machine can become


public void changeState(T managed) {
      /* 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 (managed.getNewState == managed.getCurrentState)
         return;
      /* Set the previous state to the current state */
      managed.setPreviousState = managed.setCurrentState;
      /* If the current state is not null, we call its exit method */
      if (managed.getCurrentState != null)
         exit(managed);

      /* Set the current state to the new state */
      managed.SetCurrentState = managed.getNewState;

      /* If the current state is not null, enter it */
      if (managed.getCurrentState != null)
               enter(managed);
   }

i have also a final advice

move the 3 methods: enter execute exit
in the interface Manageable

so each specific implementation (like player) know what to do when its state chagne

and mantain in the stateMachine interface only the method changeState
remove the generics for the state machine and define the method
public void changeState(T managed)
as
public void changeState(Managed managed)

put a list of Managed in the state machine
in this way you will have only one state machine that is able to manage all the kind of objects

still the state machine can call the 3 method becouse it is managin object of type Manageable
so inside the method changeState can call
managed.exit()
managed.enter()
managed.executed()