As.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 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 the License.
  12.  * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.Optional;
  30. import it.tidalwave.util.impl.AsDelegate;
  31. import lombok.EqualsAndHashCode;
  32. import lombok.RequiredArgsConstructor;
  33. import lombok.ToString;

  34. /***************************************************************************************************************************************************************
  35.  *
  36.  * Objects implementing this interface can provide am adapter of the required type. The adapter can be found with a
  37.  * variety of approaches that depend on the implementation. This capability can be used to implement a design based
  38.  * on the Data, Context and Interaction pattern (DCI). For further details, please look at the
  39.  * <a href="http://tidalwave.it/projects/thesefoolishthings">project website</a>, where a tutorial is available.
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  * @it.tidalwave.javadoc.stable
  43.  *
  44.  **************************************************************************************************************************************************************/
  45. public interface As
  46.   {
  47.     /***********************************************************************************************************************************************************
  48.      * A type reference for roles that can be used in place of a class literal, especially when roles with generics are
  49.      * used. Example of usage:
  50.      * <pre>
  51.      *
  52.      *     interface DataRetriever&lt;T&gt;
  53.      *       {
  54.      *         public List&lt;T&gt; retrieve();
  55.      *       }
  56.      *
  57.      *     class CodeSample
  58.      *       {
  59.      *         private static final As.Type&lt;DataRetriever&lt;String&gt;&gt; _StringRetriever_ = As.type(DataRetriever.class);
  60.      *
  61.      *         public void method (As object)
  62.      *           {
  63.      *             List&lt;String&gt; f3 = object.as(_StringRetriever_).retrieve();
  64.      *           }
  65.      *       }
  66.      * </pre>
  67.      *
  68.      * @since     3.2-ALPHA-12
  69.      **********************************************************************************************************************************************************/
  70.     @RequiredArgsConstructor @EqualsAndHashCode @ToString
  71.     public static final class Type<T>
  72.       {
  73.         @Nonnull
  74.         private final Class<?> type;


  75.         @Nonnull @SuppressWarnings("unchecked")
  76.         /* package */ Class<T> getType()
  77.           {
  78.             return (Class<T>)type;
  79.           }
  80.       }

  81.     /***********************************************************************************************************************************************************
  82.      * Creates an {@code As} implementation delegate for the given object (or returns the object itself if it is the
  83.      * default implementation of {@code As}).
  84.      *
  85.      * @param     object         the object
  86.      * @return                   the implementation
  87.      * @since     3.2-ALPHA-12
  88.      **********************************************************************************************************************************************************/
  89.     @Nonnull
  90.     public static As forObject (@Nonnull final Object object)
  91.       {
  92.         return (object instanceof AsDelegate) ? (As)object : new AsDelegate(object);
  93.       }

  94.     /***********************************************************************************************************************************************************
  95.      * Creates an {@code As} implementation delegate for the given object. It accepts a single pre-instantiated role,
  96.      * or a {@link RoleFactory} that will be invoked to create additional roles.
  97.      *
  98.      * @param     object         the object
  99.      * @param     role           the role or {@link it.tidalwave.util.RoleFactory}
  100.      * @return                   the implementation
  101.      * @since     3.2-ALPHA-13
  102.      **********************************************************************************************************************************************************/
  103.     @Nonnull
  104.     public static As forObject (@Nonnull final Object object, @Nonnull final Object role)
  105.       {
  106.         return new AsDelegate(object, role);
  107.       }

  108.     /***********************************************************************************************************************************************************
  109.      * Creates an {@code As} implementation delegate for the given object. It accepts a collection of pre-instantiated
  110.      * roles, or instances of {@link RoleFactory} that will be invoked to create additional roles.
  111.      *
  112.      * @param     object         the object
  113.      * @param     roles          roles or {@link it.tidalwave.util.RoleFactory} instances
  114.      * @return                   the implementation
  115.      * @since     3.2-ALPHA-13
  116.      **********************************************************************************************************************************************************/
  117.     @Nonnull
  118.     public static As forObject (@Nonnull final Object object, @Nonnull final Collection<Object> roles)
  119.       {
  120.         return new AsDelegate(object, roles);
  121.       }

  122.     /***********************************************************************************************************************************************************
  123.      * Returns an adapter to this object of the specified type. If the implementation can find multiple compliant
  124.      * adapters, only one will be returned.
  125.      *
  126.      * @param   <T>     the static type
  127.      * @param   type    the dynamic type
  128.      * @return          the adapter
  129.      * @throws          AsException if no adapter is found
  130.      **********************************************************************************************************************************************************/
  131.     @Nonnull
  132.     public default <T> T as (@Nonnull final Class<? extends T> type)
  133.       {
  134.         return maybeAs(type).orElseThrow(() -> new AsException(type));
  135.       }

  136.     /***********************************************************************************************************************************************************
  137.      * Returns the requested role or an empty {@link Optional}.
  138.      *
  139.      * @param   <T>                 the static type
  140.      * @param   type                the dynamic type
  141.      * @return                      the optional role
  142.      * @since                       3.2-ALPHA-3
  143.      **********************************************************************************************************************************************************/
  144.     @Nonnull
  145.     public <T> Optional<T> maybeAs (@Nonnull Class<? extends T> type);

  146.     /***********************************************************************************************************************************************************
  147.      * Searches for multiple adapters of the given type and returns them.
  148.      *
  149.      * @param   <T>     the static type
  150.      * @param   type    the dynamic type
  151.      * @return          a collection of adapters, possibly empty
  152.      **********************************************************************************************************************************************************/
  153.     @Nonnull
  154.     public <T> Collection<T> asMany (@Nonnull Class<? extends T> type);

  155.     /***********************************************************************************************************************************************************
  156.      * Creates a role type reference.
  157.      *
  158.      * @param   <T>                 the static type
  159.      * @param   type                the dynamic type
  160.      * @return                      the type reference
  161.      * @since                       3.2-ALPHA-12
  162.      **********************************************************************************************************************************************************/
  163.     @Nonnull
  164.     public static <T> Type<T> type (@Nonnull final Class<?> type) // FIXME: there's no static check of the argument
  165.       {
  166.         return new Type<>(type);
  167.       }

  168.     /***********************************************************************************************************************************************************
  169.      * Returns a role for this object of the specified type. If the implementation can find multiple compliant
  170.      * roles, only one will be returned.
  171.      *
  172.      * @param   <T>                 the static type
  173.      * @param   type                the type reference
  174.      * @return                      the role
  175.      * @since                       3.2-ALPHA-12
  176.      **********************************************************************************************************************************************************/
  177.     @Nonnull
  178.     public default <T> T as (@Nonnull final Type<? extends T> type)
  179.       {
  180.         return as(type.getType());
  181.       }

  182.     /***********************************************************************************************************************************************************
  183.      * Returns the requested role or an empty {@link Optional}.
  184.      *
  185.      * @param   <T>                 the static type
  186.      * @param   type                the type reference
  187.      * @return                      the optional role
  188.      * @since                       3.2-ALPHA-12
  189.      **********************************************************************************************************************************************************/
  190.     @Nonnull
  191.     public default <T> Optional<T> maybeAs (@Nonnull final Type<? extends T> type)
  192.       {
  193.         return maybeAs(type.getType());
  194.       }

  195.     /***********************************************************************************************************************************************************
  196.      * Returns the requested role or an empty {@link Optional}.
  197.      *
  198.      * @param   <T>                 the static type
  199.      * @param   type                the type reference
  200.      * @return                      the roles
  201.      * @since                       3.2-ALPHA-12
  202.      **********************************************************************************************************************************************************/
  203.     @Nonnull
  204.     public default <T> Collection<T> asMany (@Nonnull final Type<? extends T> type)
  205.       {
  206.         return asMany(type.getType());
  207.       }
  208.   }