I am developing a JAX-RS based Java application using Google Guice for dependency injection. I have the following interface in my code:
public interface LockProvider<L extends Lock> {
Optional<L> acquireLock(String lockId);
void releaseLock(L lock);
}
In the above interface, Lock is an interface defined as follows:
public interface Lock {
String getLockId();
}
Lock interface is implemented by the following class:
public class DynamoDBLock implements Lock {
private final String lockId;
private final LockItem underLyingLock;
public DynamoDBLock(final String lockId, final LockItem underLyingLock) {
this.lockId = lockId;
this.underLyingLock = underLyingLock;
}
@Override
public String getLockId() {
return lockId;
}
public LockItem getUnderlyingLock() {
return underlyingLock;
}
}
LockProvider interface is implemented by the following class:
public class DynamoDBLockProvider implements LockProvider<DynamoDBLock> {
Optional<DynamoDBLock> acquireLock(String lockId) {
//implementation here
}
void releaseLock(DynamoDBLock lock) {
LockItem underlyingLockItem = lock.getUnderlyingLockItem();
//do something with underlyingLockItem
}
}
I don't want classes in my application other than LockProvider to know about underLying lock item, which is why I haven't included getUnderlyingLockItem in the Lock interface.
Now, when I try to bind LockProvider to DynamoDBLockProvider as follows:
bind(new TypeLiteral<LockProvider<Lock>>() {}).to(DynamoDBLockProvider.class);
I get the following compilation error in Eclipse:
The method to(Class<? extends LockProvider<Lock>>) in the type LinkedBindingBuilder<LockProvider<Lock>> is not applicable for the arguments (Class<DynamoDBLockProvider>)
I understand that DynamoDBLockProvider<DynamoDBLock> is not a subclass of LockProvider<Lock>. Is it possible to accomplish what I am trying to do, i.e. bind LockProvider to DynamoDBLockProvider (in a clean and efficient way)?