R
R
Hi All,
I'm new to Java, recently I've written my first pool and asked for
comments,
I got reply that I could use generics to create more independent pool.
But I have I problem with creating instance of the <T> type
on http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
an example is given:
"
This is a generic method. It infers the value of its type parameter T
from its argument, and returns an appropriate instance of T, as
illustrated by the following snippet:
Author a = Othello.class.getAnnotation(Author.class);
"
But how can I write it using the generics?
I tried something like this:
pool.add(GenericPool.class.getAnnotation(<T>.class));
(in various forms i.e. T.class and so on - but without luck)
So my question is how can I instatiate a generic class?
thanks in advance for your help
best regards
R
here is full code of my GenericPool class:
class GenericPool <T> {
public static final int POOL_MAX_SIZE = 5;
private List<T> pool;
public GenericPool()
{
pool = new ArrayList<T>(GenericPool.POOL_MAX_SIZE);
for (int i = 0; i < GenericPool.POOL_MAX_SIZE; i++) {
// this is my problem I have to fill my pool with T classes
pool.add(GenericPool.class.getAnnotation(<T>.class));
}
}
public T get()
{
synchronized (pool) {
if (!pool.isEmpty()) {
return pool.remove(pool.size() - 1);
}
}
return null;
}
public boolean release(T df)
{
synchronized (pool) {
return pool.add(df);
}
}
public int getSize() {
synchronized (pool) {
return pool.size();
}
}
}
I'm new to Java, recently I've written my first pool and asked for
comments,
I got reply that I could use generics to create more independent pool.
But I have I problem with creating instance of the <T> type
on http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
an example is given:
"
This is a generic method. It infers the value of its type parameter T
from its argument, and returns an appropriate instance of T, as
illustrated by the following snippet:
Author a = Othello.class.getAnnotation(Author.class);
"
But how can I write it using the generics?
I tried something like this:
pool.add(GenericPool.class.getAnnotation(<T>.class));
(in various forms i.e. T.class and so on - but without luck)
So my question is how can I instatiate a generic class?
thanks in advance for your help
best regards
R
here is full code of my GenericPool class:
class GenericPool <T> {
public static final int POOL_MAX_SIZE = 5;
private List<T> pool;
public GenericPool()
{
pool = new ArrayList<T>(GenericPool.POOL_MAX_SIZE);
for (int i = 0; i < GenericPool.POOL_MAX_SIZE; i++) {
// this is my problem I have to fill my pool with T classes
pool.add(GenericPool.class.getAnnotation(<T>.class));
}
}
public T get()
{
synchronized (pool) {
if (!pool.isEmpty()) {
return pool.remove(pool.size() - 1);
}
}
return null;
}
public boolean release(T df)
{
synchronized (pool) {
return pool.add(df);
}
}
public int getSize() {
synchronized (pool) {
return pool.size();
}
}
}