As.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 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/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.Nullable;
  30. import java.util.Collection;
  31. import java.util.Optional;

  32. /***********************************************************************************************************************
  33.  *
  34.  * Objects implementing this interface can provide am adapter of the required type. The adapter can be found with a
  35.  * variety of approaches that depend on the implementation. This capability can be used to implement a design based
  36.  * on the Data, Context and Interaction pattern (DCI).
  37.  *
  38.  * @author  Fabrizio Giudici
  39.  * @it.tidalwave.javadoc.stable
  40.  *
  41.  **********************************************************************************************************************/
  42. public interface As
  43.   {
  44.     /*******************************************************************************************************************
  45.      *
  46.      * @it.tidalwave.javadoc.stable
  47.      *
  48.      ******************************************************************************************************************/
  49.     public static interface NotFoundBehaviour<T>
  50.       {
  51.         @Nonnull
  52.         public T run (@Nullable final Throwable t);
  53.       }

  54.     /*******************************************************************************************************************
  55.      *
  56.      *
  57.      ******************************************************************************************************************/
  58.     public static final class Defaults
  59.       {
  60.         private Defaults()
  61.           {
  62.           }

  63.         public static <X> NotFoundBehaviour<X> throwAsException (@Nonnull final Class<X> clazz)
  64.           {
  65.             return new NotFoundBehaviour<X>()
  66.               {
  67. //                @Override
  68.                 @Nonnull
  69.                 public X run (@Nonnull final Throwable t)
  70.                   {
  71.                     throw new AsException(clazz, t);
  72.                   }
  73.               };
  74.           }
  75.       }

  76.     /*******************************************************************************************************************
  77.      *
  78.      * Returns an adapter to this object of the specified type. If the implementation can find multiple compliant
  79.      * adapters, only one will be returned.
  80.      *
  81.      * @param   <T>     the static type
  82.      * @param   type    the dynamic type
  83.      * @return          the adapter
  84.      * @throws          AsException if no adapter is found
  85.      *
  86.      ******************************************************************************************************************/
  87.     @Nonnull
  88.     public <T> T as (@Nonnull Class<T> type);

  89.     /*******************************************************************************************************************
  90.      *
  91.      * Returns an adapter to this object of the specified type. If the implementation can find multiple compliant
  92.      * adapters, only one will be returned. If no adapter is found, the result provided by the given default
  93.      * behaviour will be returned.
  94.      *
  95.      * @param   <T>                 the static type
  96.      * @param   type                the dynamic type
  97.      * @param   notFoundBehaviour   the behaviour to apply when an adapter is not found
  98.      * @return                      the adapter
  99.      *
  100.      ******************************************************************************************************************/
  101.     @Nonnull
  102.     public <T> T as (@Nonnull Class<T> type, @Nonnull NotFoundBehaviour<T> notFoundBehaviour);

  103.     /*******************************************************************************************************************
  104.      *
  105.      * Returns the requested role or an empty {@link Optional}.
  106.      *
  107.      * @param   <T>                 the static type
  108.      * @param   type                the dynamic type
  109.      * @return                      the optional role
  110.      * @since                       3.2-ALPHA-3
  111.      *
  112.      ******************************************************************************************************************/
  113.     @Nonnull
  114.     default <T> Optional<T> maybeAs (@Nonnull final Class<T> type)
  115.       {
  116.         return Optional.ofNullable(as(type, throwable -> null));
  117.       }

  118.     /*******************************************************************************************************************
  119.      *
  120.      * @param   <T>     the static type
  121.      * @param   type    the dynamic type
  122.      * @return          the optional role
  123.      * @deprecated Use {@link #maybeAs(Class)}.
  124.      *
  125.      ******************************************************************************************************************/
  126.     @Nonnull @Deprecated
  127.     default <T> Optional<T> asOptional (@Nonnull final Class<T> type)
  128.       {
  129.         return maybeAs(type);
  130.       }

  131.     /*******************************************************************************************************************
  132.      *
  133.      * Searches for multiple adapters of the given type and returns them.
  134.      *
  135.      * @param   <T>     the static type
  136.      * @param   type    the dynamic type
  137.      * @return          a collection of adapters, possibly empty
  138.      *
  139.      ******************************************************************************************************************/
  140.     @Nonnull
  141.     public <T> Collection<T> asMany (@Nonnull Class<T> type);
  142.   }