I have a use case where a verification batch job (non-generic) verifies domains based on its input e.g domain_type, batch_pointer etc. The verification handles it based on the domain type which uses generics.
class VerificationJob {
public void execute() {
String domainType = "user";
long batchPointer = 123;
// how to handle generics at this abstraction level to avoid using ?
Batch<?> batch = getRepositoryInstance(domainType).fetchNextNRecords(batchPointer, 10);
new VerificationTask(batch.contents(), (T t) -> true).verify();
if(batch.hasNext()) {
// schedule next job with new batch pointer
}
}
Repository<?> getRepositoryInstance(String domainType) {
return domainType.equals("user") ? new UserRepository() : null; // or a switch case etc.
}
}
class VerificationTask<T> {
Collection<T> ts; // construction inject
Predicate<T> verifier;
boolean verify() {
// verification based on verifier for each t
}
}
class UserRepository implements Repository<User> {
Batch<User> fetchNextNRecords(int batchPointer, int limit) {
// code omitted
}
} // similarly we could have for Employee
class Repository<T> {
Batch<T> fetchNextNRecords(int batchPointer, int limit);
}
class Batch<T> {
List<T> contents = new ArrayList(); // getter & setter omitted
boolean hasNext = false; // getter & setter
}
class User {
// code omitted
} // similarly have for Employee
One solution I could think of is to have the generics code in another abstraction level and return the batch with verification result above. However, this will be repeated for all concrete impls e.g user, employee. Is there any other solution to solve this?
// in job class in case domainType is "user"
BatchVerificationResult result = new UserVerificationTask().verify(123, 10);
if(result.hasNext()) {
// schedule next
}
class UserVerificationTask<User> implements BatchVerification {
BatchVerificationResult verify(long batchPointer, int limit) {
// do the above verification which is in job class
}
}
interface BatchVerification {
BatchVerificationResult verify(long batchPointer, int limit);
}
class BatchVerificationResult {
boolean verificationResult;
boolean hasNext;
}
```