ContextSnapshot.java

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

  27. import javax.annotation.Nonnull;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.concurrent.CopyOnWriteArrayList;
  31. import it.tidalwave.util.ContextManager;
  32. import it.tidalwave.util.Task;
  33. import lombok.extern.slf4j.Slf4j;
  34. import static it.tidalwave.util.ShortNames.*;

  35. /***************************************************************************************************************************************************************
  36.  *
  37.  * A facility that takes a snapshot of the contexts that are current at creation time and make them available later.
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **************************************************************************************************************************************************************/
  42. @Slf4j
  43. public class ContextSnapshot
  44.   {
  45.     private final ContextManager contextManager = ContextManager.getInstance();

  46.     // TODO: should be weak references? Should a context be alive as soon as all the objects created with it are
  47.     // alive?
  48.     private final List<Object> contexts = Collections.unmodifiableList(contextManager.getContexts());

  49.     /***********************************************************************************************************************************************************
  50.      * Creates a new instance and samples the currently available contexts.
  51.      *
  52.      * @param owner     the owner
  53.      **********************************************************************************************************************************************************/
  54.     public ContextSnapshot (@Nonnull final Object owner)
  55.       {
  56.         if (log.isTraceEnabled())
  57.           {
  58.             log.trace(">>>> contexts for {} at construction time: {}", shortId(owner), shortIds(contexts));
  59.           }
  60.       }

  61.     /***********************************************************************************************************************************************************
  62.      * Returns the previously sampled contexts.
  63.      *
  64.      * @return    the contexts
  65.      **********************************************************************************************************************************************************/
  66.     @Nonnull
  67.     public List<Object> getContexts()
  68.       {
  69.         return new CopyOnWriteArrayList<>(contexts);
  70.       }

  71.     /***********************************************************************************************************************************************************
  72.      * Runs a {@link Task} associated with the sampled contexts.
  73.      *
  74.      * @param  <V>      the type of the result value
  75.      * @param  <T>      the type of the exception
  76.      * @param  task     the task
  77.      * @return          the value produced by the task
  78.      * @throws T        the exception(s) thrown by the task
  79.      **********************************************************************************************************************************************************/
  80.     public <V, T extends Throwable> V runWithContexts (@Nonnull final Task<V, T> task)
  81.       throws T
  82.       {
  83.         return contextManager.runWithContexts(contexts, task);
  84.       }

  85.     /***********************************************************************************************************************************************************
  86.      * {@inheritDoc}
  87.      **********************************************************************************************************************************************************/
  88.     @Override @Nonnull
  89.     public String toString()
  90.       {
  91.         return String.format("ContextSnapshot%s", shortIds(contexts));
  92.       }
  93.   }