不兼容的类型:com.rh.host.Validator <t>无法转换为com.rh.host.Pool.Validator <t>

时间:2017-04-17 15:25:56

标签: java generics pool

我使用generic object pooling重用Cipher对象。

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

1 个答案:

答案 0 :(得分:2)

这篇文章的一些代码似乎已被删除。

错误是尝试分配

com.rh.host.Validator

com.rh.host.Pool.Validator

写得很明显发生了什么。您有两个不相关的类型,都称为Validator。该文章似乎在自己的文件中呈现嵌套类型。

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

相关问题