2017-04-17 103 views
0

我使用generic object pooling重用Cipher对象。不兼容的类型:com.rh.host.Validator <T>无法转换为com.rh.host.Pool.Validator <T>

Eracom

Pool< Cipher> pool = PoolFactory.newBoundedBlockingPool(10, new CipherPicker("DESede/CBC/NoPadding"), new CipherPickerValidator()); 

PoolFactory

public static <T> Pool<T> newBoundedBlockingPool(int size, ObjectFactory<T> factory, Validator<T> validator) { 
     return new BoundedBlockingPool<T>(size, factory, validator); 
    } 

游泳池

public interface Pool<T> 
{ 
T get(); 
void shutdown(); 
public boolean isValid(T t); 
public void invalidate(T t); 
} 
} 

验证

public interface Validator<T> 
{ 

    public boolean isValid(T t); 

    public void invalidate(T t); 
} 

CipherPickerValidator

public final class CipherPickerValidator implements Validator <Cipher> 
{ 

    @Override 
    public boolean isValid(Cipher t) { 
     return true; 
    } 

    @Override 
    public void invalidate(Cipher t) { 
//  return false; 
    } 
} 

我得到错误PoolFactory。它在validator下显示一条红线。

错误

incompatible types: com.rh.host.Validator<T> cannot be converted to com.rh.host.Pool.Validator<T> where T is a type-variable: 
T extends Object declared in method <T>newBoundedBlockingPool(int,ObjectFactory<T>,com.rh.host.Validator<T>) 

我跟着A Generic and Concurrent Object Pool

回答

2

一些文章的代码似乎已被改写的。

错误是试图分配

com.rh.host.Validator 

com.rh.host.Pool.Validator 

写的,这显然发生了什么事。你有两个无关的类型,都称为Validator。该文章似乎在其自己的文件中呈现嵌套类型。

因此,确保每个名称只有一个定义。并可能找到更好的文章。

相关问题