how to return a generic class?

I am trying to develop an ObjectPool, which can be used with any Object without changing the source of neither the Pool nor the Object - but I can´t find any way to write the get()-function ("Maincode" getting some Object from Pool) as there is a Type-Mismatch (Cannot convert from Object to TestObject)

Here´s my code so far:

ObjectPool:

public Object get() {
        int first = availableObjects.get(0);
        availableObjects.remove(0);
        return objects.get(first);
    }

In objects (ArrayList) there are all the objects in the pool, availableObjects just contains a list of all available objects. I am basically just returning the first available object and mark it as unavailable.

Maincode:

    TestObject test = objectPoolS.get();


How can I make the get()-Method more generic/specific so it works without adapting the pool everytime I use a different class and without parsing on the maincode?


Update:

With a more generic version of that Pool, how would the get()-method look like correctly?
(Any other feedback greatly appreciated!)
Update: Working (Fixed) Version of the MainCode below

Update 2:
I just realized that it can´t work like this, I need to create new Objects in the Pool - which seems hardly possible to do without adapting the Pool for every kind of Object.
So can I somehow remove the reference between all the Objects in the Pool? (As I use baseObject to initialize all Objects, I actually only have 5 references to 1 Object)
Or is there a better way to manage it? (Without forcing the user to create the Objects in his Code)

Complete Code so far:

ObjectPool

public class ObjectPoolS<T> {

    int numberOfObjects;
    boolean autoExtending;

    T baseObject;
    ArrayList<T> objects;
    ArrayList<Integer> availableObjects;


    public  ObjectPoolS(T baseObject, int capacity, boolean allowAutoExtending) {
        this.numberOfObjects = capacity;
        this.baseObject = baseObject;
        this.autoExtending = allowAutoExtending;

        objects = new ArrayList<T>(capacity);
        availableObjects = new ArrayList<Integer>(capacity);

        initialize(baseObject, capacity);
    }


    private void initialize(T baseObject, int capacity) {

        // Initialize List of Objects
        for(int i = 0; i < capacity; i++) {
            objects.add(baseObject);
        }

        // Initialize Index of Objects
        for(int i = 0; i < capacity; i++) {
            availableObjects.add(new Integer(i));
        }
    }


    public T get() {
        int first = availableObjects.get(0);
        availableObjects.remove(0);
        return objects.get(first);
    }
}

Maincode (Original)

ObjectPoolS objectPoolS = new ObjectPoolS(new TestObject(0), 5, true);
TestObject test = objectPoolS.get();

Maincode (Fixed)

ObjectPoolS<TestObject> objectPoolS = new ObjectPoolS<TestObject>(new TestObject(0), 5, true);
TestObject test = objectPoolS.get();

TestObject only contains a single int for testing purposes

Error

Type mismatch: cannot convert from Object to TestObject

You are not pooling: you are using just single instance of the object.

Generic object pool looks something like this:

public class ObjectPool<T> {
    private List<T> objects;
    private Class<T> clazz;
    private int size;

    public ObjectPool(Class<T> clazz, int size) throws IllegalStateException {
        this.clazz = clazz;
        this.size = size;
        this.objects = new ArrayList<T>();
        for (int i = 0;i < size;i++) {
            objects.add(newInstance());
        }
    }

    // You can override this method with anonymous class
    protected T newInstance() {
        try {
            return clazz.newInstance()
        } catch (Exception exception) {
            throw new IllegalStateException(exception);
        }
    }

    public synchronized T getInstance() {
        if (objects.isEmpty()) {
            return null;
        }
        return objects.remove(objects.size() - 1);
    }

    public synchronized void returnInstance(T instance) {
        if (objects.size() < size) {
            objects.add(instance);
        }
    }
}

Unfortunately I do not have compiler to test the code, but it should give you a hint what to change.


Oh, in your main code

ObjectPoolS objectPoolS = new ObjectPoolS(new TestObject(0), 5, true);

you must add "<TestObject>" and becomes

ObjectPoolS<TestObject> objectPoolS
=new ObjectPoolS<TestObject> (new TestObject(0),5,true);

just like where you declare ArrayList, it contains "<T>" and "<Integer>", isn't it?

However, I believe that there should be no problem in your original code. It maybe another problem.

Tips: at the get() method, you can rewrite to:

public T get(){
    return this.list.get(this.available.remove(0));
}

你可以试试 :

public <T> T get() {
    // Your code for objectToReturn
    return (T) objectToReturn;
}
链接地址: http://www.djcxy.com/p/76444.html

上一篇: 我可以在没有Visual Studio的情况下使用Visual C ++编译器吗?

下一篇: 如何返回一个泛型类?