Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda