DefaultAs.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 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.impl;

  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import java.util.List;
  33. import java.util.Optional;
  34. import java.util.function.Function;
  35. import it.tidalwave.util.As;
  36. import it.tidalwave.util.LazySupplier;
  37. import it.tidalwave.util.Parameters;
  38. import it.tidalwave.util.RoleFactory;
  39. import it.tidalwave.util.spi.AsDelegate;
  40. import it.tidalwave.util.spi.AsDelegateProvider;
  41. import static it.tidalwave.util.Parameters.r;

  42. /***********************************************************************************************************************
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. public class DefaultAs implements As
  48.   {
  49.     @Nonnull
  50.     private final LazySupplier<AsDelegate> delegate;

  51.     @Nonnull
  52.     private final Object owner;

  53.     @Nonnull
  54.     private final List<Object> roles = new ArrayList<>();

  55.     /*******************************************************************************************************************
  56.      *
  57.      * Constructor for use in subclassing.
  58.      *
  59.      ******************************************************************************************************************/
  60.     protected DefaultAs()
  61.       {
  62.         owner = this;
  63.         delegate = LazySupplier.of(() -> AsDelegateProvider.Locator.find().createAsDelegate(this));
  64.       }

  65.     /*******************************************************************************************************************
  66.      *
  67.      * Constructor for use in composition.
  68.      *
  69.      * @param  owner             the owner
  70.      * @since  3.2-ALPHA-3 (refactored)
  71.      *
  72.      ******************************************************************************************************************/
  73.     public DefaultAs (@Nonnull final Object owner)
  74.       {
  75.         this(owner, Collections.emptyList());
  76.       }

  77.     /*******************************************************************************************************************
  78.      *
  79.      * Constructor for use in composition. In addition to the mandatory owner, it accepts a single pre-instantiated
  80.      * role, or a {@link RoleFactory} that will be invoked to create additional roles.
  81.      *
  82.      * @param  owner          the owner
  83.      * @param  role           the role or {@link it.tidalwave.util.RoleFactory}
  84.      * @since  3.2-ALPHA-3
  85.      *
  86.      ******************************************************************************************************************/
  87.     public DefaultAs (@Nonnull final Object owner, @Nonnull final Object role)
  88.       {
  89.         this(owner, r(Parameters.mustNotBeArrayOrCollection(role, "role")));
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      * Constructor for use in composition. In addition to the mandatory owner, it accepts a collection of
  94.      * pre-instantiated roles, or instances of {@link RoleFactory} that will be invoked to create additional roles.
  95.      *
  96.      * @param  owner          the owner
  97.      * @param  roles          roles or {@link it.tidalwave.util.RoleFactory} instances
  98.      * @since  3.2-ALPHA-3 (refactored)
  99.      *
  100.      ******************************************************************************************************************/
  101.     public DefaultAs (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
  102.       {
  103.         this(o -> AsDelegateProvider.Locator.find().createAsDelegate(o), owner, roles);
  104.       }

  105.     /*******************************************************************************************************************
  106.      *
  107.      * Constructor for use in tests. This constructor doesn't call {@link AsDelegateProvider.Locator#find()}.
  108.      *
  109.      * @param  asDelegateFactory  the factory
  110.      * @param  owner          the owner
  111.      * @param  roles          roles or {@link it.tidalwave.util.RoleFactory} instances
  112.      * @since  3.2-ALPHA-3 (refactored)
  113.      *
  114.      ******************************************************************************************************************/
  115.     public DefaultAs (@Nonnull final Function<Object, AsDelegate> asDelegateFactory,
  116.                       @Nonnull final Object owner,
  117.                       @Nonnull final Collection<Object> roles)
  118.       {
  119.         delegate = LazySupplier.of(() -> asDelegateFactory.apply(owner));
  120.         this.owner = owner;
  121.         this.roles.addAll(resolveFactories(roles));
  122.       }

  123.     /*******************************************************************************************************************
  124.      *
  125.      * {@inheritDoc}
  126.      *
  127.      * First, local roles are probed; then the owner, in case it directly implements the required role; at last,
  128.      * the delegate is invoked.
  129.      *
  130.      ******************************************************************************************************************/
  131.     @Override @Nonnull
  132.     public <T> Optional<T> maybeAs (@Nonnull final Class<? extends T> type)
  133.       {
  134.         for (final var role : roles)
  135.           {
  136.             if (type.isAssignableFrom(role.getClass()))
  137.               {
  138.                 return Optional.of(type.cast(role));
  139.               }
  140.           }

  141.         final var r = delegate.get().as(type);
  142.         return r.isEmpty() ? Optional.empty() : Optional.of(r.iterator().next());
  143.       }

  144.     /*******************************************************************************************************************
  145.      *
  146.      * {@inheritDoc}
  147.      *
  148.      * The list contains all the relevant local roles, as well as those retrieved by the delegate, in this order.
  149.      *
  150.      ******************************************************************************************************************/
  151.     @Nonnull
  152.     public <T> Collection<T> asMany (@Nonnull final Class<? extends T> type)
  153.       {
  154.         final Collection<T> results = new ArrayList<>();

  155.         for (final var role : roles)
  156.           {
  157.             if (type.isAssignableFrom(role.getClass()))
  158.               {
  159.                 results.add(type.cast(role));
  160.               }
  161.           }

  162.         results.addAll(delegate.get().as(type));

  163.         return results;
  164.       }

  165.     /*******************************************************************************************************************
  166.      *
  167.      * Resolve the factories: if found, they are invoked and the produced role is added to the list.
  168.      *
  169.      * @param  roles  the list of roles or factory roles
  170.      * @return                   a list of roles
  171.      *
  172.      ******************************************************************************************************************/
  173.     @Nonnull
  174.     private List<Object> resolveFactories (@Nonnull final Collection<Object> roles)
  175.       {
  176.         final List<Object> result = new ArrayList<>();

  177.         for (final var roleOrFactory : roles)
  178.           {
  179.             if (roleOrFactory instanceof RoleFactory)
  180.               {
  181.                 result.add(((RoleFactory<Object>)roleOrFactory).createRoleFor(owner));
  182.               }
  183.             else
  184.               {
  185.                 result.add(roleOrFactory);
  186.               }
  187.           }

  188.         return result;
  189.       }
  190.   }