jeudi 11 mai 2023

Adding a new attribute in Request object using lombok Builder

Background

I want to add one more attribute TemplateId to the Request object. Currently the Request object is constructed in the function transform using builder like below:

public class DocFactory { 

ModuleProvider<Collection<Scenario>> dollarScenarioProvider;

public Request transform(String requestId, Product product) {
    Request.RequestBuilder.builder = Request.builder();

    BiFunction<Throwable, Runnable, Throwable> handler = (e, runnable) -> {
       if (e == null) {
          runnable.run();
          return null;
       } else {
          Log.error ("Failed to process DocumentRequestModule(())", requestId, e.getCause);
          return e.getCause);

    String errorMessage = Stream.of(dollarScenarioProvider.getAsync(product)
                         .handle((result, e) -> handler.apply(e, () ->                                                             
                           builder.scenarioDollar(result)))))
                                .map(CompletableFuture::join)
                                .filter(Objects::nonNull)
                                .map(Throwable::getMessage)
                                ...
}}

For the getAsync method in ModuleProvider, it's defined like so:

public interface ModuleProvider<R> {

default CompletableFuture<R> getAsync (Product product) {
     return CompletableFuture.completedFuture(get(product));
}

//other methods omitted

My questions:

  1. The attribute I want to add in Request is TemplateId, which is a string. This string will be returned by a function getTemplateId (which takes in the argument Product product and returns a String). It seems like maybe I should do something like:
TemplateIdProvider.getAsync(product)
                         .handle((result, e) -> handler.apply(e, () ->                                                             
                           builder.TemplateId(result)))))

But where should I put the function getTemplateId?

  1. I don't exactly understand what this code is doing. Is it related to Factory Pattern? I know the code that I shared is trying to construct a Request using builder, but the way is it done (using handler & ModuleProvider, ... seems obscure to me , could you please share with me your insight on why it would be done this way?
TemplateIdProvider.getAsync(product)
                         .handle((result, e) -> handler.apply(e, () ->                                                             
                           builder.TemplateId(result)))))

Thank you so much in advance.

Aucun commentaire:

Enregistrer un commentaire