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.Parameters;
  37. import it.tidalwave.util.RoleFactory;
  38. import it.tidalwave.util.spi.AsDelegate;
  39. import it.tidalwave.util.spi.AsDelegateProvider;
  40. import static it.tidalwave.util.Parameters.r;

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

  50.     @Nonnull
  51.     private final Object owner;

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

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

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

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

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

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

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

  140.         final Collection<? extends T> r = delegate.as(type);
  141.         return r.isEmpty() ? Optional.empty() : Optional.of(r.iterator().next());
  142.       }

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

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

  161.         results.addAll(delegate.as(type));

  162.         return results;
  163.       }

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

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

  187.         return result;
  188.       }
  189.   }