Skip to content

Package: ContextSampler

ContextSampler

nameinstructionbranchcomplexitylinemethod
ContextSampler(Object)
M: 0 C: 23
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
getContexts()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
runWithContexts(Task)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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:
29: import javax.annotation.Nonnull;
30: import java.util.Collections;
31: import java.util.List;
32: import java.util.concurrent.CopyOnWriteArrayList;
33: import it.tidalwave.util.Task;
34: import it.tidalwave.role.ContextManager;
35: import lombok.ToString;
36: import lombok.extern.slf4j.Slf4j;
37: import static it.tidalwave.role.spi.impl.LogUtil.*;
38:
39: /***********************************************************************************************************************
40: *
41: * A facility that samples the contexts that are current at creation time and make them available later.
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: @Slf4j @ToString(of = "contexts")
47: public class ContextSampler
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;
52:
53: // No @Inject here, we don't want to depend on a DI framework
54: private final ContextManager contextManager = ContextManager.Locator.find();
55:
56: /*******************************************************************************************************************
57: *
58: * Creates a new instance and samples the currently available contexts.
59: *
60: * @param owner the owner
61: *
62: ******************************************************************************************************************/
63: public ContextSampler (@Nonnull final Object owner)
64: {
65: contexts = Collections.unmodifiableList(contextManager.getContexts());
66:
67:• if (log.isTraceEnabled())
68: {
69: log.trace(">>>> contexts for {} at construction time: {}", shortId(owner), shortIds(contexts));
70: }
71: }
72:
73: /*******************************************************************************************************************
74: *
75: * Returns the previously sampled contexts.
76: *
77: * @return the contexts
78: *
79: ******************************************************************************************************************/
80: @Nonnull
81: public List<Object> getContexts()
82: {
83: return new CopyOnWriteArrayList<>(contexts);
84: }
85:
86: /*******************************************************************************************************************
87: *
88: * Runs a {@link Task} associated with the sampled contexts.
89: *
90: * @param <V> the type of the result value
91: * @param <T> the type of the exception
92: * @param task the task
93: * @return the value produced by the task
94: * @throws T the exception(s) thrown by the task
95: *
96: ******************************************************************************************************************/
97: public <V, T extends Throwable> V runWithContexts (@Nonnull final Task<V, T> task)
98: throws T
99: {
100: return contextManager.runWithContexts(contexts, task);
101: }
102: }