(Easy?) Java Interface Question

Status
Not open for further replies.

gumbo

Ars Tribunus Angusticlavius
6,107
I have an interface an interface for an event listener.

code:<HR>
Code:
public interface eventListener {
   public void eventChanged(Event event, int oldValue, int newValue) {
   }
}

Now, in a class that sends the event to all listeners, I have this:
code:<HR>
Code:
eventListener el = (previously created event listener by intrested class);
el.eventChanged(new Event, 0, 0);

In other people's code (where I tried to figure out how to do this), this works fine. In my code, javac/jikes cannot find the eventChanged method due to eventListener being an interface.

Is there any way to call all methods that implement an interface by calling the interface? None of the registered listeners are known until registration, obviously.

This seems simple, but won't work...
thx,
gubmo

[This message was edited by gubmo on January 11, 2002 at 22:12.]
 
Can you post the specific error?

I think if your code is like

code:<HR>
Code:
public class someclass implements eventListener
{
   public void eventChanged(Event event, int oldValue, int newValue)
   {
      ...
   }
} <P>

and you have

code:<HR>
Code:
someclass instance; 
...
eventListener el = instance;
el.eventChanged(new Event, 0, 0);

seems that that would work fine
 

gumbo

Ars Tribunus Angusticlavius
6,107
mebe: I meant to only declare it. Please replace the braces with a semi colon. Thank you. :D

The problem is I can't control the class that implements the interface. I only have the interface to work with. Some of the code that uses the interface is already written, so I cannot force a new method upon them. I am merely reusing the interface in another application.

I create a copy of the listener by using it in the constructor to my new class (hacked to provide compatibility and get the implementation.) Therefore, I have the reference to the correct method, and want to use the interface declaration as a C++ function pointer to call it. The other code did this, only using an array instead of a single instance.

Gah, I am trying to use the SensorListener interface from
this package to write a new event handler for this, as part of a simulator package (link in profile). I do exactly the same as Sensor.java, but without an array of SensorListeners. Help any?

gubmo
 

dmcnaught

Ars Scholae Palatinae
684
I do this stuff all the time, so you've gotta be missing something simple. ;)

Where is the object that implements EventListener coming from?

There needs to be a class somewhere that implements that interface, and you need an object of that class. You can then call the method through the object.

I have a lot of code that basically does:

code:<HR>
Code:
public interface Foo {
  public void doStuff(int bar);
}<P>...<P>   Foo f = baz.getFooImpl();   // baz is a "factory object"
   f.doStuff(5);<P>

Note that my code doesn't know or care what actual class 'f' points to, as long as it satisfies the contract laid out by the interface definition. The 'getFooImpl()' method needs to refer somehow to an actual class that implements the interface, and return an instance of that class.

If this doesn't help, post more details of your code, and maybe we can give you some wisdom.
 

gumbo

Ars Tribunus Angusticlavius
6,107
The listeners are registered by other classes created after the fact. Think of javax.swing.JButton. It has an actionPerformed event, and an addActionListener() method, both inherited from AbstractButton. AbstractButton uses EventListenerList (what seems to be a vector of Listeners), to manage the listeners, then does what my code does.

Sun's programmers have no idea how/where/when a listener will want to be added, yet they are called with no object usage. They all seem to be diverted to a listener list class, which handles calling the event.

I would just create a class that implements EventListener, but I don't see how that helps me use other people's registered listeners.


The specific code from the packages I linked to:

SensorListener.java:
code:<HR>
Code:
public interface SensorListener
{
   public void stateChanged (Sensor aSource, int aOldValue, int aNewValue);
}

Sensor.java (relavent parts):
code:<HR>
Code:
private final SensorListener[] iListeners = new SensorListener[8];
...
//Adding a listener
iListeners[iNumListeners++] = aListener;
...
private synchronized void callListeners() {
    int newValue = readSensorValue( iSensorId, 1);
    for (int i = 0; i < iNumListeners; i++) {
      iListeners[i].stateChanged( this, iPreviousValue, newValue);
    }
    iPreviousValue = newValue;
  }

My handler simPublicContactSensor.java:
code:<HR>
Code:
Sensor hSensor;
SensorListener rListener;<P>public simPublicContactHandler(Sensor aSensor, SensorListener aListener) {
        hSensor = aSensor;
        rListener = aListener;
    }
...    
   public void process(RsContactSensorEvent event){     
      int newValue = event.status?1:0;       
      rListener.stateChanged(hSensor, oldValue, newValue);      
      oldValue = newValue;
   }

One difference is that I must create a new simPublicContactListener every time the user of the Sensor class requests a listener for the sensor. This is due to Rossum's event architecture, so it makes it easiest for a listener to be directly linked to a Rossum listener through my listener. Whew. :)

RsContactSensorListener -> simPublicContactListener -> SensorListener

The test cases I have made all work correctly, implementing an interface, assigning it to an object, and the other class works correctly only getting the iTest interface object.

AFAICT, this is the same, but I haven't created a class that implements the interface - that is for the end-programmer to do.

gubmo

[This message was edited by gubmo on January 12, 2002 at 13:01.]
 

dmcnaught

Ars Scholae Palatinae
684
Then I don't see why it wouldn't compile. It certainly won't run until the user supplies an implementation.

I created the following:

code:<HR>
Code:
// TestInterface.java<P>public interface TestInterface {
	public void doStuff();
}<P>// Test.java<P>public class Test {
	TestInterface foo = null;<P>	public Test (TestInterface ti)
	{
		foo = ti;
	}<P>	public void doSomething()
	{
		foo.doStuff();
	}
}<P>

They compile just fine with the Sun JDK.
 

gumbo

Ars Tribunus Angusticlavius
6,107
AAAAAAAAAARRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGHHHHHHHHHHHHHHHHH!!!!!!
:mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad: :mad:

Mother f*cking Jikes!!!!!

javac compiled it fine. jikes always gave me the error. I could have sworn I tried javac. Anyway, normally it is the other way around - Jikes lets stuff through that javac doesn't - like initializing variables in loop declarations. Jikes accepts it, javac almost always throws an error.

Dammit!!
:mad: :mad:

Edit: It appears that javac doesn't like my hacked classpath, and the Jikes error was cryptic enough to not tell me josx.platform.rcx.Sensor was not compatible with ./Sensor javac did, but complains about the classpath. Wonderful. At least jikes compiles it.

gubmo
 
Status
Not open for further replies.