Raistlin
Raistlin

Reputation: 1127

Generics in Constructor Parameters

I know that this is a very silly question since I once knew stuff like that. But I have a really hard time now getting this done. I have a constructor for a Class which should get an ArrayList which is filled with Objects that implement my interface called CollisionObserver. Here is my constructor-head:

public Cursor(GL gl, LibraryFinger finger, Vector direction, float radius, 
       int index, ArrayList<T extends CollisionObserver> observerList)

Can anybody tell me where I make the big mistake? I tried ArrayList<CollisionObserver> too, but this doesn't work too, since when I call the constructor with Objects that implement CollisionObserver the error message says the constructor is undefined.

Ok, here is the complete Cursor class:

public class Cursor implements CollisionSubject{

private Vector direction;
private GL gl;
private float radius;
private LibraryFinger finger;
private GLUT glut;
protected static float[] sphere_center = new float[3];
private ArrayList<CollisionObserver> observer = new ArrayList<CollisionObserver>();

public Cursor(GL gl, LibraryFinger finger, Vector direction, float radius, int index, ArrayList<T extends CollisionObserver> observerList){
    this.gl = gl;
    this.finger = finger;
    this.direction = direction;
    this.radius = radius;
    glut = new GLUT();
    
//Finetuning for fingermovements! The vector delivered by the LeapListener is to be considered as
//raw-data. It has to be adjusted to the environment in which the fingertracking is used.
    sphere_center[0] = (float) (finger.getX()/15);
    sphere_center[1] = (float) ((finger.getY()/20)*-1);
    sphere_center[2] = (float) (finger.getZ()/5);
    
    gl.glTranslatef(sphere_center[0], sphere_center[1], sphere_center[2]);
    gl.glEnable(GL.GL_CULL_FACE);
    gl.glCullFace(GL.GL_BACK);
    gl.glColor3f(0.757f, 0.804f, 0.804f);
    
    glut.glutSolidSphere(radius, 16, 16);
    for (int i = 0; i < index; i++){
        registerCollisionObserver(observerList.get(i));
    }
    myPosition();
}

@Override
public void registerCollisionObserver(CollisionObserver o) {
    observer.add(o);        
}

@Override
public void removeCollisionObserver(CollisionObserver o) {
    if (!observer.isEmpty()) {
        int i = observer.indexOf(o);
        if (i >= 0)
            observer.remove(i);
    }
}


@Override
public void myPosition() {
    for (int i = 0; i < observer.size(); i++) {
        CollisionObserver o = observer.get(i);
        o.collision(finger, direction, radius);
    }
}

and here is how I invoke the constructor. I do this in two different classes. First class:

private ArrayList<Picture> picList = new ArrayList<Picture>();

The type "Picture" implements the interface CollisionObserver

 Cursor finger = new Cursor(gl, lfl[i], listener.getFl().get(i).direction(), 0.5f, picList.size(), picList);

Same thing with class two. Type Shelf implements the CollisionObserver interface:

    private ArrayList<Shelf> shelfList = new ArrayList<Shelf>();
    Cursor finger = new Cursor(gl, lfl[i], listener.getFl().get(i).direction(), 0.5f, shelfList.size(), shelfList);

Upvotes: 0

Views: 78

Answers (2)

Makoto
Makoto

Reputation: 106390

In one of your constructor parameters, you have this declared:

ArrayList<T extends CollisionObserver>

Where'd the T come from? If you want to have this ArrayList contain elements that are type-bound to this class, then the use of T would be appropriate here.

From what it seems like, you want a list of objects that just implement CollisionObserver, so this would be the declaration you want:

ArrayList<? extends CollisionObserver>

Upvotes: 2

christopher
christopher

Reputation: 27336

You need to declare your class to use the generic type T.

Example

public class MyClass<T>

Upvotes: 1

Related Questions