View Javadoc
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.role.impl;
27  
28  import jakarta.annotation.Nonnull;
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.concurrent.CopyOnWriteArrayList;
32  import it.tidalwave.util.ContextManager;
33  import it.tidalwave.util.Task;
34  import lombok.extern.slf4j.Slf4j;
35  import static it.tidalwave.util.ShortNames.*;
36  
37  /***************************************************************************************************************************************************************
38   *
39   * A facility that takes a snapshot of the contexts that are current at creation time and make them available later.
40   *
41   * @author  Fabrizio Giudici
42   *
43   **************************************************************************************************************************************************************/
44  @Slf4j
45  public class ContextSnapshot
46    {
47      private final ContextManager contextManager = ContextManager.getInstance();
48  
49      // TODO: should be weak references? Should a context be alive as soon as all the objects created with it are
50      // alive?
51      private final List<Object> contexts = Collections.unmodifiableList(contextManager.getContexts());
52  
53      /***********************************************************************************************************************************************************
54       * Creates a new instance and samples the currently available contexts.
55       *
56       * @param owner     the owner
57       **********************************************************************************************************************************************************/
58      public ContextSnapshot (@Nonnull final Object owner)
59        {
60          if (log.isTraceEnabled())
61            {
62              log.trace(">>>> contexts for {} at construction time: {}", shortId(owner), shortIds(contexts));
63            }
64        }
65  
66      /***********************************************************************************************************************************************************
67       * Returns the previously sampled contexts.
68       *
69       * @return    the contexts
70       **********************************************************************************************************************************************************/
71      @Nonnull
72      public List<Object> getContexts()
73        {
74          return new CopyOnWriteArrayList<>(contexts);
75        }
76  
77      /***********************************************************************************************************************************************************
78       * Runs a {@link Task} associated with the sampled contexts.
79       *
80       * @param  <V>      the type of the result value
81       * @param  <T>      the type of the exception
82       * @param  task     the task
83       * @return          the value produced by the task
84       * @throws T        the exception(s) thrown by the task
85       **********************************************************************************************************************************************************/
86      public <V, T extends Throwable> V runWithContexts (@Nonnull final Task<V, T> task)
87        throws T
88        {
89          return contextManager.runWithContexts(contexts, task);
90        }
91  
92      /***********************************************************************************************************************************************************
93       * {@inheritDoc}
94       **********************************************************************************************************************************************************/
95      @Override @Nonnull
96      public String toString()
97        {
98          return String.format("ContextSnapshot%s", shortIds(contexts));
99        }
100   }