Optional Functional Java Rating – Code Review Stack Exchange

Until now, I could write this code:

Public class ReferenceResourceImpl implements ReferenceResource {

final transient private CacheControl cacheControl;
request for private final transitional request;

final transient private ReferenceService referenceService;

public ReferenceResourceImpl (
@CacheControlConfig (maxAge = 20) CacheControl cacheControl,
@Context request request,
ReferenceService referenceService
) {
this.cacheControl = cacheControl;
this.request = request;

this.referenceService = referenceService;
}

/ **
* Calculate {@link Reference} s {@link EntityTag}.
* @param reference {@link Reference} to look up
* @back calculated {@link EntityTag}
* /
private EntityTag eTag (Reference Reference) {
return EntityTag.valueOf (Integer.toString (reference.hashCode ()));
}

/ **
* {@inheritDoc}
* /
@Override
public Response download (
Channel ID
) {
LOG.info (RestConstants.Logs.UPLOAD_START, id);

Optional reference = this.referenceService.get (id);

ResponseBuilder responseBuilder = this.request.evaluatePrecisions (this.eTag (reference.get ()));
if (Objects.isNull (responseBuilder)) {
responseBuilder = Answer
.ok (reference.get ())
.cacheControl (this.cacheControl)
.tag (this.eTag (reference.get ()));
}

return responseBuilder.build ();
}
}

I would like to refactor this code using a functional "notation":

Optional reference = this.referenceService.get (id);

ResponseBuilder responseBuilder = this.request.evaluatePrecisions (this.eTag (reference.get ()));
if (Objects.isNull (responseBuilder)) {
responseBuilder = Answer
.ok (reference.get ())
.cacheControl (this.cacheControl)
.tag (this.eTag (reference.get ()));
}

return responseBuilder.build ();

I have tried this code:

return this.referenceService.get (id) <<<< Extract the entity from the database
.map (this :: eTag)
.map (this.request :: assessmentPreconditions)
.orElse (Response.status (Status.NOT_FOUND))
.cacheControl (this.cacheControl)
.tag (this.eTag (this.referenceService.get (id) .get ())) <<<< Get the entity from the database
.to build();

Here, I find the code more elegant, but I will search my entity twice on the database using this.referenceService.get (id).

Any ideas on how to solve this?