OwnerRoleFactoryProvider.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.spi;

  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import it.tidalwave.util.LazySupplier;
  32. import lombok.RequiredArgsConstructor;
  33. import static it.tidalwave.role.impl.ServiceLoaderLocator.lazySupplierOf;

  34. /***********************************************************************************************************************
  35.  *
  36.  * The provider of the singleton {@link OwnerRoleFactory}.
  37.  *
  38.  * @author  Fabrizio Giudici
  39.  *
  40.  **********************************************************************************************************************/
  41. @FunctionalInterface
  42. public interface OwnerRoleFactoryProvider
  43.   {
  44.     static class Inner // TODO: will go away with Java 17
  45.       {
  46.         private static final LazySupplier<OwnerRoleFactoryProvider> PROVIDER_REF =
  47.                 lazySupplierOf(OwnerRoleFactoryProvider.class);

  48.         private static final LazySupplier<EmptyOwnerRoleFactory> EMPTY_REF =
  49.                 LazySupplier.of(EmptyOwnerRoleFactory::new);
  50.       }

  51.     /*******************************************************************************************************************
  52.      *
  53.      * Returns the singleton instance of {@link OwnerRoleFactoryProvider}
  54.      *
  55.      * @return    the singleton instance
  56.      *
  57.      ******************************************************************************************************************/
  58.     @Nonnull
  59.     public static OwnerRoleFactoryProvider getInstance ()
  60.       {
  61.         return Inner.PROVIDER_REF.get();
  62.       }

  63.     /*******************************************************************************************************************
  64.      *
  65.      * Creates an {@link OwnerRoleFactory} for the given object
  66.      *
  67.      * @param     owner   the object
  68.      * @return            {@code AsDelegate}
  69.      *
  70.      ******************************************************************************************************************/
  71.     @Nonnull
  72.     public OwnerRoleFactory createRoleFactory (@Nonnull Object owner);

  73.     /*******************************************************************************************************************
  74.      *
  75.      * Installs a {@link OwnerRoleFactory}. <b>This method is for testing only (used to set up a testing context).</b>
  76.      *
  77.      * @param   ownerRoleFactory    the {@link OwnerRoleFactory}
  78.      * @see     #reset()
  79.      *
  80.      ******************************************************************************************************************/
  81.     public static void set (@Nonnull final OwnerRoleFactory ownerRoleFactory)
  82.       {
  83.         Inner.PROVIDER_REF.set(new SimpleOwnerRoleFactoryProvider(ownerRoleFactory));
  84.       }

  85.     /*******************************************************************************************************************
  86.      *
  87.      * Removes a previously installed {@link OwnerRoleFactory}. <b>This method is for testing only (used to clean up a
  88.      * testing context).</b>
  89.      *
  90.      * @see     #set(OwnerRoleFactory)
  91.      *
  92.      ******************************************************************************************************************/
  93.     public static void reset()
  94.       {
  95.         Inner.PROVIDER_REF.clear();
  96.       }

  97.     /*******************************************************************************************************************
  98.      *
  99.      * Returns an empty implementation of factory. Useful for setting up a test environment.
  100.      *
  101.      * @return    the empty implementation
  102.      * @since     3.2-ALPHA-1
  103.      *
  104.      ******************************************************************************************************************/
  105.     @Nonnull
  106.     public static OwnerRoleFactory emptyRoleFactory()
  107.       {
  108.         return Inner.EMPTY_REF.get();
  109.       }

  110.     /*******************************************************************************************************************
  111.      *
  112.      ******************************************************************************************************************/
  113.     @RequiredArgsConstructor
  114.     static class SimpleOwnerRoleFactoryProvider implements OwnerRoleFactoryProvider
  115.       {
  116.         @Nonnull
  117.         private final OwnerRoleFactory ownerRoleFactory;

  118.         @Override @Nonnull
  119.         public OwnerRoleFactory createRoleFactory (@Nonnull final Object owner)
  120.           {
  121.             return ownerRoleFactory;
  122.           }
  123.       }

  124.     /*******************************************************************************************************************
  125.      *
  126.      ******************************************************************************************************************/
  127.     static class EmptyOwnerRoleFactory implements OwnerRoleFactory
  128.       {
  129.         @Override @Nonnull
  130.         public <T> Collection<T> findRoles (@Nonnull final Class<? extends T> type)
  131.           {
  132.             return new ArrayList<>(); // must be mutable
  133.           }
  134.       }
  135.   }