3

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

azro
  • 53,056
  • 7
  • 34
  • 70
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • 1
    Make the `handler` method to contain two statements: 1) simple `iProductService.delete(...)`; 2) and `return null;`. – terrorrussia-keeps-killing Dec 22 '21 at 15:17
  • really null is the return type ?? – San Jaisy Dec 22 '21 at 15:17
  • 7
    Null is not the return type; `Void` is. But there is only one possible value of `Void` -- `null`. So that's what you return. (You can understand this by analogy to the other primitive wrappers. `Integer` has one value that corresponds to each value of `int`, plus the distinguished value `null`. Same is true for all the primitive wrappers. Since `void` has no values, `Void` has all (none) of those, plus `null`.) – Brian Goetz Dec 22 '21 at 15:19
  • @BrianGoetz - Your explanation is a little confusing. It seems I can only return a `null` from a `Void` method. If `Void has all (none) of those, plus null` how come I cannot return any thing such as an `int`? – K.Nicholas Dec 22 '21 at 15:53
  • 3
    @K.Nicholas `Integer` is the reference companion type for `int`. For every value of `int` (all 2^32) of them, there is a corresponding value of `Integer`, plus `Integer` also can take on the value `null`. `Void` is the reference companion for `void`. For every value of `void` (all zero of them), there is a corresponding value of `Void`, plus `Void` can also take on the value `null`. So `null` is the only valid value of `Void`. If you wanted to be able to return _anything_, make your return type `Object`. – Brian Goetz Dec 22 '21 at 16:20
  • @BrianGoetz - Thanks. Sounds like you're using set theory (discrete math?) for your explanation. – K.Nicholas Dec 22 '21 at 16:30
  • 1
    See [Cannot convert void to java.lang.Void](https://stackoverflow.com/q/28762715/2711488) – Holger Dec 22 '21 at 17:11
  • Does this answer your question? [Java generics void/Void types](https://stackoverflow.com/questions/5568409/java-generics-void-void-types) – Nowhere Man Dec 22 '21 at 22:57

1 Answers1

3

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;
    }
}
ETO
  • 6,970
  • 1
  • 20
  • 37