4

When using apache common pool, it can provide me a good implementation of Java object pooling. However their KeyedPoolableObjectFactory interface is not type safe. Is there any object pool library in Java that can provide a generics interface for us to create a type safe object pool?

palacsint
  • 28,416
  • 10
  • 82
  • 109
raymond.mh.ng
  • 343
  • 2
  • 3
  • 21

2 Answers2

5

Apache Commons Pool version 1.6 supports generics

http://commons.apache.org/pool/examples.html

redben
  • 5,578
  • 5
  • 47
  • 63
2

Not as far as I know, but it's easy enough to create a typed wrapper around the commons-pool implementation, so your calling code can use generics.

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • Is is not so easy. For example, the following is not possible. public class X implements GenericKeyedObjectPool { @Override public void activateObject(final Object arg0, final Object arg1) throws Exception { if (arg0 instanceof T) .... => error } – raymond.mh.ng Jun 07 '11 at 10:16
  • Or I can only use this, but it is not so good for creating the required object. public class X implements KeyedPoolableObjectFactory { private Class type; public static X create(Class type) { return new X(type); } – raymond.mh.ng Jun 07 '11 at 10:22
  • 1
    @raymond Why artbristol means it that you can create a class which holds an instance of `KeyedObjectPool`. Then you are free to define your own API, delegating calls to the referenced `KeyedObjectPool`. See http://de.wikipedia.org/wiki/Adapter_%28Entwurfsmuster%29 for more details on how to implement this. – helpermethod Jun 07 '11 at 11:33
  • Thanks artbristol and Helper Method. It seems that I have no other choice at this moment but to implement a wrapper or adaptor to achieve this. :) – raymond.mh.ng Jun 08 '11 at 02:22