Skip to content

Package: SpringAsDelegate$1

SpringAsDelegate$1

nameinstructionbranchcomplexitylinemethod
run()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 9
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.spring.spi;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.List;
33: import it.tidalwave.util.As;
34: import it.tidalwave.util.Task;
35: import it.tidalwave.util.spi.AsDelegate;
36: import it.tidalwave.role.spi.ContextSampler;
37: import it.tidalwave.role.spi.RoleManager;
38: import lombok.extern.slf4j.Slf4j;
39: import static it.tidalwave.role.spi.impl.LogUtil.*;
40:
41: /***********************************************************************************************************************
42: *
43: * An implementation for {@link As} based on Spring.
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: @Slf4j
49: class SpringAsDelegate implements AsDelegate
50: {
51: @Nonnull
52: private final Object owner;
53:
54: private final ContextSampler contextSampler;
55:
56: /*******************************************************************************************************************
57: *
58: * Constructor for use with subclassing.
59: *
60: ******************************************************************************************************************/
61: public SpringAsDelegate()
62: {
63: this.owner = this;
64: contextSampler = new ContextSampler(this);
65: }
66:
67: /*******************************************************************************************************************
68: *
69: * Constructor for use with composition.
70: *
71: * @param owner the owner object
72: *
73: ******************************************************************************************************************/
74: public SpringAsDelegate (@Nonnull final Object owner)
75: {
76: this.owner = owner;
77: contextSampler = new ContextSampler(owner);
78: }
79:
80: /*******************************************************************************************************************
81: *
82: * {@inheritDoc}
83: *
84: ******************************************************************************************************************/
85: @Override @Nonnull
86: public <T> Collection<? extends T> as (@Nonnull final Class<T> roleType)
87: {
88: log.trace("as({}) for {}", shortName(roleType), shortId(owner));
89: log.trace(">>>> contexts: {}", contextSampler);
90:
91: final List<T> roles =
92: new ArrayList<>(contextSampler.runWithContexts(new Task<List<? extends T>, RuntimeException>()
93: {
94: @Override @Nonnull
95: public List<? extends T> run ()
96: {
97: return RoleManager.Locator.find().findRoles(owner, roleType);
98: }
99: }));
100:
101: if (roleType.isAssignableFrom(owner.getClass()))
102: {
103: roles.add(roleType.cast(owner));
104: }
105:
106: log.trace(">>>> as() returning {}", shortIds(roles));
107:
108: return roles;
109: }
110: }