Skip to content

Package: DefaultAs

DefaultAs

nameinstructionbranchcomplexitylinemethod
DefaultAs()
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
DefaultAs(Function, Object, Collection)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
DefaultAs(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
DefaultAs(Object, Collection)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
DefaultAs(Object, Object)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
asMany(Class)
M: 0 C: 35
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 7
100%
M: 0 C: 1
100%
maybeAs(Class)
M: 0 C: 36
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
resolveFactories(Collection)
M: 0 C: 32
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
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.util.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Collections;
33: import java.util.List;
34: import java.util.Optional;
35: import java.util.function.Function;
36: import it.tidalwave.util.As;
37: import it.tidalwave.util.Parameters;
38: import it.tidalwave.util.RoleFactory;
39: import it.tidalwave.util.spi.AsDelegate;
40: import it.tidalwave.util.spi.AsDelegateProvider;
41: import static it.tidalwave.util.Parameters.r;
42:
43: /***********************************************************************************************************************
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: public class DefaultAs implements As
49: {
50: @Nonnull
51: private final AsDelegate delegate;
52:
53: @Nonnull
54: private final Object owner;
55:
56: @Nonnull
57: private final List<Object> roles = new ArrayList<>();
58:
59: /*******************************************************************************************************************
60: *
61: * Constructor for use in subclassing.
62: *
63: ******************************************************************************************************************/
64: protected DefaultAs()
65: {
66: owner = this;
67: delegate = AsDelegateProvider.Locator.find().createAsDelegate(this);
68: }
69:
70: /*******************************************************************************************************************
71: *
72: * Constructor for use in composition.
73: *
74: * @param owner the owner
75: * @since 3.2-ALPHA-3 (refactored)
76: *
77: ******************************************************************************************************************/
78: public DefaultAs (@Nonnull final Object owner)
79: {
80: this(owner, Collections.emptyList());
81: }
82:
83: /*******************************************************************************************************************
84: *
85: * Constructor for use in composition. In addition to the mandatory owner, it accepts a single pre-instantiated
86: * role, or a {@link RoleFactory} that will be invoked to create additional roles.
87: *
88: * @param owner the owner
89: * @param role the role or {@link it.tidalwave.util.RoleFactory}
90: * @since 3.2-ALPHA-3
91: *
92: ******************************************************************************************************************/
93: public DefaultAs (@Nonnull final Object owner, @Nonnull final Object role)
94: {
95: this(owner, r(Parameters.mustNotBeArrayOrCollection(role, "role")));
96: }
97:
98: /*******************************************************************************************************************
99: *
100: * Constructor for use in composition. In addition to the mandatory owner, it accepts a collection of
101: * pre-instantiated roles, or instances of {@link RoleFactory} that will be invoked to create additional roles.
102: *
103: * @param owner the owner
104: * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
105: * @since 3.2-ALPHA-3 (refactored)
106: *
107: ******************************************************************************************************************/
108: public DefaultAs (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
109: {
110: this(AsDelegateProvider.Locator.find()::createAsDelegate, owner, roles);
111: }
112:
113: /*******************************************************************************************************************
114: *
115: * Constructor for use in tests. This constructor doesn't call {@link AsDelegateProvider.Locator#find()}.
116: *
117: * @param asDelegateFactory the factory
118: * @param owner the owner
119: * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
120: * @since 3.2-ALPHA-3 (refactored)
121: *
122: ******************************************************************************************************************/
123: public DefaultAs (@Nonnull final Function<Object, AsDelegate> asDelegateFactory,
124: @Nonnull final Object owner,
125: @Nonnull final Collection<Object> roles)
126: {
127: delegate = asDelegateFactory.apply(owner);
128: this.owner = owner;
129: this.roles.addAll(resolveFactories(roles));
130: }
131:
132: /*******************************************************************************************************************
133: *
134: * {@inheritDoc}
135: *
136: * First, local roles are probed; then the owner, in case it directly implements the required role; at last,
137: * the delegate is invoked.
138: *
139: ******************************************************************************************************************/
140: @Override @Nonnull
141: public <T> Optional<T> maybeAs (@Nonnull final Class<T> type)
142: {
143:• for (final Object role : roles)
144: {
145:• if (type.isAssignableFrom(role.getClass()))
146: {
147: return Optional.of(type.cast(role));
148: }
149: }
150:
151: final Collection<? extends T> r = delegate.as(type);
152:• return r.isEmpty() ? Optional.empty() : Optional.of(r.iterator().next());
153: }
154:
155: /*******************************************************************************************************************
156: *
157: * {@inheritDoc}
158: *
159: * The list contains all the relevant local roles, as well as those retrieved by the delegate, in this order.
160: *
161: ******************************************************************************************************************/
162: @Nonnull
163: public <T> Collection<T> asMany (@Nonnull final Class<T> type)
164: {
165: final Collection<T> results = new ArrayList<>();
166:
167:• for (final Object role : roles)
168: {
169:• if (type.isAssignableFrom(role.getClass()))
170: {
171: results.add(type.cast(role));
172: }
173: }
174:
175: results.addAll(delegate.as(type));
176:
177: return results;
178: }
179:
180: /*******************************************************************************************************************
181: *
182: * Resolve the factories: if found, they are invoked and the produced role is added to the list.
183: *
184: * @param roles the list of roles or factory roles
185: * @return a list of roles
186: *
187: ******************************************************************************************************************/
188: @Nonnull
189: private List<Object> resolveFactories (@Nonnull final Collection<Object> roles)
190: {
191: final List<Object> result = new ArrayList<>();
192:
193:• for (final Object roleOrFactory : roles)
194: {
195:• if (roleOrFactory instanceof RoleFactory)
196: {
197: result.add(((RoleFactory<Object>)roleOrFactory).createRoleFor(owner));
198: }
199: else
200: {
201: result.add(roleOrFactory);
202: }
203: }
204:
205: return result;
206: }
207: }