The following gives this error:
public class Account
{
...
}
public class DB_Account extends Account implements DBObject
{
...
}
public class Cache<E extends DBObject>
{
protected Map<Long,E> m_contents;
...
public Collection<E> values()
{
return m_contents.values();
}
}
public class DB_Imp implements Database
{
protected Cache<DB_Account> m_accounts;
...
@Override
public Collection<Account> getAccounts()
{
if (m_accounts.isValid())
/* Compiler gives error here */
return m_accounts.values();
...
}
}
I've currently worked around the compiler error in the DB_Imp
class by adding a Collections.class.cast()
around the call to accounts.values()
and added a @SuppressWarnings
. There must be a better way. The other way is to modify the Cache class to:
@SuppressWarnings("unchecked")
public <T> Collection<T> values()
{
return Collections.class.cast(m_contents.values());
}