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
T
being declared at the class level. – Jenninejenningsclass ObjectPool<T>
? Please add an SSCCE and the compiler error to your question. I also recommend unaccepting the existing answer because it's not a safe solution. – Jenninejennings