Search code examples
javagenericspool

incompatible types: com.rh.host.Validator<T> cannot be converted to com.rh.host.Pool.Validator<T>


I using generic object pooling to reuse Cipher object.

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);
    }

Pool

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

Validator

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;
    }
}

I get error in PoolFactory. It shows a red line under validator.

Error

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>)

I follow A Generic and Concurrent Object Pool


Solution

  • Some of the article's code appears to have been munged.

    The error is attempting to assign a

    com.rh.host.Validator
    

    to a

    com.rh.host.Pool.Validator
    

    Written like that it's obvious what has happened. You have two unrelated types both called Validator. The article appears to present a nested type in a file of its own.

    So make sure you have only one definition for each name. And probably find a better presented article.