RoleFactory.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 javax.annotation.Nonnull;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.List;
  31. import java.util.Optional;

  32. /***************************************************************************************************************************************************************
  33.  *
  34.  * A factory of roles.
  35.  *
  36.  * @param   <T>   the type of the owner
  37.  * @author  Fabrizio Giudici
  38.  *
  39.  **************************************************************************************************************************************************************/
  40. @FunctionalInterface
  41. public interface RoleFactory<T, R>
  42.   {
  43.     /***********************************************************************************************************************************************************
  44.      * {@return one or more roles — if multiple ones have to be returned, they must be wrapped in a {@link java.util.Collection}}.
  45.      * @param   owner   the owner
  46.      **********************************************************************************************************************************************************/
  47.     @Nonnull
  48.     public Optional<R> createRoleFor (@Nonnull T owner);

  49.     /***********************************************************************************************************************************************************
  50.      * {@return the role type}.
  51.      * @since   4.0-ALPHA-2
  52.      **********************************************************************************************************************************************************/
  53.     @Nonnull @SuppressWarnings("unchecked")
  54.     public default Class<R> getRoleType()
  55.       {
  56.         return (Class<R>)ReflectionUtils.getTypeArguments(RoleFactory.class, getClass()).get(1);
  57.       }

  58.     /***********************************************************************************************************************************************************
  59.      * {@return a collection of roles resolving factories}; that is, any factory in the collection is invoked so it can eventually create another role.
  60.      * @param   roleOrFactories   a collection of roles or {@link RoleFactory} instances
  61.      * @param   owner             the role owner
  62.      * @since   4.0-ALPHA-2
  63.      **********************************************************************************************************************************************************/
  64.     @Nonnull @SuppressWarnings("unchecked")
  65.     public static <T> Collection<Object> resolveFactories (@Nonnull final T owner, @Nonnull final Iterable<Object> roleOrFactories)
  66.       {
  67.         final List<Object> result = new ArrayList<>();

  68.         for (final var roleOrFactory : roleOrFactories)
  69.           {
  70.             if (roleOrFactory instanceof RoleFactory)
  71.               {
  72.                 ((RoleFactory<T, ?>)roleOrFactory).createRoleFor(owner).ifPresent(result::add);
  73.               }
  74.             else
  75.               {
  76.                 result.add(roleOrFactory);
  77.               }
  78.           }

  79.         return result;
  80.       }
  81.   }