A riddle with generics, cannot get it to work.

H

HK

Sorry for the lenghty post. I tried to keep the
code as short as possible. It comes out a bit
contrieved, but it demonstrates the problem.

Here is the setup:

The class Machine stores a list of objects
implementing the Action interface. The method
Machine.filter() calls the Action.invoke() method
of each action.

General purpose actions are available which
manipulate the inner state of the machine. This
kind of actions is represented by the example
OtherAction.

Application specific actions make use of the
Machine.clientData field to communicate with each
other or collect data. An example is SomeAction.

The Goal:

In a java-1.4 implementation, the
Machine.clientData field would be of type Object
and an Action interested in it has to cast,
possibly resulting in a runtime error. By using
generics I hope it is possible to avoid casts
altogether.

The Problem:

I can't get the four classes compiled with
-Xlint:unchecked without any warning. The code
shown is only one of the many versions I
tried. Whenever I correct one problem, another
error or warning pops up. Can anyone get it
going?

Thanks,
Harald.


==> Machine.java <==
import java.util.ArrayList;
import java.util.List;

public class Machine<CDTA> {
// a container for the benefit of actions called, may be null
public CDTA clientData;

// the list of actions to run
private List<Action> callbacks = new ArrayList<Action>();

public void add(Action a) { callbacks.add(a); }

public void changeState(int state) { /* demo only */ }

public void filter() {
for(Action a : callbacks) {
a.invoke(this);
}
}

/**********************************************************************/
public static void main(String[] argv) {
Machine<String> r = new Machine<String>();
r.clientData = "blabla";
r.add(new SomeAction());
r.add(new OtherAction());
r.filter();
}
}
/**********************************************************************/
/**********************************************************************/

==> Action.java <==
public interface Action<CDTA> {
void invoke(Machine<CDTA> r);
}
/**********************************************************************/
/**********************************************************************/

==> SomeAction.java <==
/**
* an Action that interacts with Machine.clientData.
*/
public class SomeAction implements Action<String> {
public void invoke(Machine<String> r) {
String s = r.clientData;
System.out.println("clientData="+s);
}
}
/**********************************************************************/
/**********************************************************************/

==> OtherAction.java <==
/**
* A general Action not interested in Machine.clientData
*/
public class OtherAction implements Action {
public void invoke(Machine r) {
r.changeState(3)
System.out.println("otheraction called");
}
}
/**********************************************************************/
/**********************************************************************/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top