AsDelegate.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.role.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.role.spi.OwnerRoleFactory;
  40. import it.tidalwave.role.spi.OwnerRoleFactoryProvider;
  41. import static it.tidalwave.util.Parameters.r;

  42. /***********************************************************************************************************************
  43.  *
  44.  * An implementation of {@link As} that keeps some local roles and queries {@link OwnerRoleFactory}.
  45.  *
  46.  * @author  Fabrizio Giudici
  47.  *
  48.  **********************************************************************************************************************/
  49. public class AsDelegate implements As
  50.   {
  51.     @Nonnull
  52.     private final Object owner;

  53.     @Nonnull
  54.     private final LazySupplier<OwnerRoleFactory> ownerRoleFactory;


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

  57.     /*******************************************************************************************************************
  58.      *
  59.      * Constructor for use in subclassing.
  60.      *
  61.      ******************************************************************************************************************/
  62.     protected AsDelegate ()
  63.       {
  64.         owner = this;
  65.         ownerRoleFactory = LazySupplier.of(() -> OwnerRoleFactoryProvider.getInstance().createRoleFactory(this));
  66.       }

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

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

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

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

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

  143.         final var r = ownerRoleFactory.get().findRoles(type);
  144.         return r.isEmpty() ? Optional.empty() : Optional.of(r.iterator().next());
  145.       }

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

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

  164.         results.addAll(ownerRoleFactory.get().findRoles(type));

  165.         return results;
  166.       }

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

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

  190.         return result;
  191.       }
  192.   }