A riddle with generics, cannot get it to work.

Discussion in 'Java' started by HK, Jul 13, 2005.

  1. HK

    HK Guest

    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");
    }
    }
    /**********************************************************************/
    /**********************************************************************/
    HK, Jul 13, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    5,991
    John C. Bollinger
    May 20, 2005
  2. Sharp Tool

    Riddle me this

    Sharp Tool, Nov 6, 2005, in forum: Java
    Replies:
    29
    Views:
    1,185
    John C. Bollinger
    Nov 10, 2005
  3. magmike

    Database Results Riddle

    magmike, Aug 5, 2004, in forum: HTML
    Replies:
    18
    Views:
    680
    Toby Inkster
    Aug 8, 2004
  4. Big Bill
    Replies:
    33
    Views:
    2,597
    Big Bill
    Oct 8, 2004
  5. Soul
    Replies:
    0
    Views:
    519
Loading...

Share This Page