Skip to content

Package: DefaultOwnerRoleFactory$1

DefaultOwnerRoleFactory$1

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