RestResponse.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.rest;

  28. import javax.annotation.Nonnull;
  29. import java.util.NoSuchElementException;
  30. import java.util.Optional;
  31. import java.util.function.Consumer;
  32. import java.util.function.Function;
  33. import lombok.AllArgsConstructor;
  34. import lombok.Getter;
  35. import lombok.extern.slf4j.Slf4j;
  36. import static lombok.AccessLevel.*;

  37. /***********************************************************************************************************************
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **********************************************************************************************************************/
  42. @AllArgsConstructor(access = PROTECTED) @Slf4j
  43. public class RestResponse<T>
  44.   {
  45.     @Nonnull
  46.     private final Optional<T> datum;

  47.     @Getter @Nonnull
  48.     private final String responseStatus;

  49.     @Nonnull
  50.     public static <X> RestResponse<X> empty()
  51.       {
  52.         return new RestResponse<>(Optional.empty(), "");
  53.       }

  54.     /*******************************************************************************************************************
  55.      *
  56.      * Returns the datum, if available.
  57.      *
  58.      * @return      the datum
  59.      * @throws      NoSuchElementException if the datum is not available
  60.      *
  61.      ******************************************************************************************************************/
  62.     @Nonnull
  63.     public T get()
  64.       throws NoSuchElementException
  65.       {
  66.         return datum.get();
  67.       }

  68.     /*******************************************************************************************************************
  69.      *
  70.      * Returns <code>true</code> if the datum is available.
  71.      *
  72.      * @return      <code>true</code> if the datum is available
  73.      *
  74.      ******************************************************************************************************************/
  75.     public boolean isPresent()
  76.       {
  77.         return datum.isPresent();
  78.       }

  79.     /*******************************************************************************************************************
  80.      *
  81.      * Maps the result, if present, to a new value using a mapping function.
  82.      *
  83.      * @param <U>       the type of the result
  84.      * @param mapper    a mapping function to apply to the value, if present
  85.      * @return          an {@code Optional} result
  86.      *
  87.      ******************************************************************************************************************/
  88.     @Nonnull
  89.     public <U> Optional<U> map (@Nonnull final Function<? super T, ? extends U> mapper)
  90.       {
  91.         return datum.map(mapper);
  92.       }

  93.     /*******************************************************************************************************************
  94.      *
  95.      * Maps the result, if present, to a new value using a mapping function, avoiding wrapping with multiple
  96.      * {@code Optional}s.
  97.      *
  98.      * @param <U>       the type of the result
  99.      * @param mapper    a mapping function to apply to the value, if present
  100.      * @return          an {@code Optional} result
  101.      *
  102.      ******************************************************************************************************************/
  103.     @Nonnull
  104.     public <U> Optional<U> flatMap (@Nonnull final Function<? super T, Optional<U>> mapper)
  105.       {
  106.         return datum.flatMap(mapper);
  107.       }

  108.     /*******************************************************************************************************************
  109.      *
  110.      * If a value is present, invoke the specified consumer with the value, otherwise do nothing.
  111.      *
  112.      * @param consumer  code to be executed if a value is present
  113.      *
  114.      ******************************************************************************************************************/
  115.     public void ifPresent (@Nonnull final Consumer<? super T> consumer)
  116.       {
  117.         datum.ifPresent(consumer);
  118.       }
  119.   }