Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
void with Generics in Java

I have a function that returns void

public interface IProductService {
   void delete(String id);
}

Generic method

public interface IRequestHandler<C , R> {
    R handler(C c);
    Class<C> commandType();
}

Implementation of generic interface

 @Singleton
    public record DeleteProductCommandHandler(IProductService iProductService)
            implements IRequestHandler<DeleteProductCommand, Void> {


        @Override
        public Void handler(DeleteProductCommand deleteProductCommand) {
            return iProductService.delete(deleteProductCommand.id);
        }

        @Override
        public Class<DeleteProductCommand> commandType() {
            return DeleteProductCommand.class;
        }
    }

How can I use void in IRequestHandler<DeleteProductCommand, Void> so that I can map void from iProductService.delete(deleteProductCommand.id);

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Option 1:

Just return null:

@Override
public Void handler(DeleteProductCommand deleteProductCommand) {
    iProductService.delete(deleteProductCommand.id);
    return null;
}

Option 2:

Update the IProductService::delete method to return something meaningful, e.g. a boolean value like Collection::remove does:

public interface IProductService {
   boolean delete(String id);
}
@Singleton
public record DeleteProductCommandHandler(IProductService iProductService)
            implements IRequestHandler<DeleteProductCommand, Boolean> {

    @Override
    public Boolean handler(DeleteProductCommand deleteProductCommand) {
        return iProductService.delete(deleteProductCommand.id);
    }

    @Override
    public Class<DeleteProductCommand> commandType() {
        return DeleteProductCommand.class;
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!