Skip to content

Package: PriorityAsSupport$RoleProvider

PriorityAsSupport$RoleProvider

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 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/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util.spi;
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 it.tidalwave.util.As;
36: import it.tidalwave.util.AsException;
37: import it.tidalwave.dci.annotation.DciRole;
38: import lombok.extern.slf4j.Slf4j;
39:
40: /***********************************************************************************************************************
41: *
42: * A specialisation of {@link AsSupport} that deals with multiple roles of the same type by prioritising them; they
43: * are ordered from most relevant to least relevant (where relevance is associated to specialisation, that is most
44: * specialised roles, or roles associated via {@code @DciRole} to most specialised datum classes, are most relevant).
45: *
46: * FIXME: could be seen as a replacement to {@code AsSupport}?
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @Slf4j
52: public class PriorityAsSupport extends AsSupport implements As
53: {
54: @FunctionalInterface
55: public static interface RoleProvider
56: {
57: @Nonnull
58: public <T> Collection<T> findRoles (@Nonnull final Class<T> type);
59: }
60:
61: @Nonnull
62: private final Object owner; // for logging only
63:
64: @Nonnull
65: private final Optional<RoleProvider> additionalRoleProvider;
66:
67: public PriorityAsSupport (final Object owner)
68: {
69: this(owner, Collections.emptyList());
70: }
71:
72: public PriorityAsSupport (@Nonnull final Object owner, @Nonnull final Collection<Object> rolesOrFactories)
73: {
74: super(owner, rolesOrFactories);
75: this.owner = owner;
76: this.additionalRoleProvider = Optional.empty();
77: }
78:
79: public PriorityAsSupport (@Nonnull final Object owner,
80: @Nonnull final RoleProvider additionalRoleProvider,
81: @Nonnull final Collection<Object> rolesOrFactories)
82: {
83: super(owner, rolesOrFactories);
84: this.owner = owner;
85: this.additionalRoleProvider = Optional.of(additionalRoleProvider);
86: }
87:
88: /*******************************************************************************************************************
89: *
90: * {@inheritDoc}
91: *
92: * Returned roles can be associated both to this type and to the delegate; the one with the higher priority is
93: * returned. See {@link #asMany(java.lang.Class)} for further details.
94: *
95: * @see #asMany(java.lang.Class)
96: *
97: ******************************************************************************************************************/
98: @Override @Nonnull
99: public <T> T as (@Nonnull final Class<T> type)
100: {
101: return as(type, As.Defaults.throwAsException(type));
102: }
103:
104: /*******************************************************************************************************************
105: *
106: * {@inheritDoc}
107: *
108: * Returned roles can be associated both to this type and to the delegate; the one with the higher priority is
109: * returned. See {@link #asMany(java.lang.Class)} for further details.
110: *
111: * @see #asMany(java.lang.Class)
112: *
113: ******************************************************************************************************************/
114: @Override @Nonnull
115: public <T> T as (@Nonnull final Class<T> type, @Nonnull final NotFoundBehaviour<T> notFoundBehaviour)
116: {
117: return maybeAs(type).orElseGet(() -> notFoundBehaviour.run(new AsException(type)));
118: }
119:
120: /*******************************************************************************************************************
121: *
122: * {@inheritDoc}
123: *
124: * Returned roles can be associated both to this type and to the delegate; the one with the higher priority is
125: * returned. See {@link #asMany(java.lang.Class)} for further details.
126: *
127: * @see #asMany(java.lang.Class)
128: *
129: ******************************************************************************************************************/
130: @Override @Nonnull
131: public <T> Optional<T> maybeAs (@Nonnull final Class<T> type)
132: {
133: return asMany(type).stream().findFirst();
134: }
135:
136: /*******************************************************************************************************************
137: *
138: * {@inheritDoc}
139: *
140: * Returned roles can be associated both to this type and to the delegate; the one with the higher priority is
141: * returned. The ones associated to this type come with higher priority (this makes sense, being this class a
142: * decorator, specific roles could be associated to it). But given that the default implementation of asMany()
143: * doesn't guarantee ant order (see TFT-192) there's something to take care of. Currently this method contains
144: * some hardwired priority logics.
145: *
146: ******************************************************************************************************************/
147: @Override @Nonnull
148: public <T> Collection<T> asMany (@Nonnull final Class<T> type)
149: {
150: log.trace("asMany({}) - {}", type, owner);
151: final List<T> unordered = new ArrayList<>(super.asMany(type));
152: additionalRoleProvider.ifPresent(r -> unordered.addAll(r.findRoles(type)));
153: //
154: // Need a kind of bubble sort, because:
155: // a) the original sequence might have a meaning; for instance, additional roles added by
156: // additionalRoleProvider are appended and, generally, they should stay low in priority.
157: // b) there is not always a well-defined way to define a relation order between the elements.
158: //
159: final List<T> result = new ArrayList<>();
160: unordered.forEach(item -> addInOrder(result, item));
161: log.trace(">>>> returning {}", result);
162:
163: return result;
164: }
165:
166: /*******************************************************************************************************************
167: *
168: * Adds an item to the list, just before the first existing item which whose datum class is an instance of a
169: * subclass of its datum class.
170: *
171: ******************************************************************************************************************/
172: private static <T> void addInOrder (@Nonnull final List<T> list, @Nonnull final T item)
173: {
174: log.trace(">>>> add in order {} into {}", item, list);
175: final Optional<T> firstAncestor = list.stream().filter(i -> isDatumAncestor(i, item)).findFirst();
176: final int index = firstAncestor.map(list::indexOf).orElse(list.size());
177: list.add(index, item);
178: log.trace(">>>>>>>> add in order {} ", list);
179: }
180:
181: /*******************************************************************************************************************
182: *
183: ******************************************************************************************************************/
184: private static <T> boolean isDatumAncestor (@Nonnull final T a, @Nonnull final T b)
185: {
186: final DciRole aBoundDatumClass = a.getClass().getAnnotation(DciRole.class);
187: final DciRole bBoundDatumClass = b.getClass().getAnnotation(DciRole.class);
188:
189: if ((aBoundDatumClass != null) && (bBoundDatumClass != null))
190: {
191: return aBoundDatumClass.datumType()[0].isAssignableFrom(bBoundDatumClass.datumType()[0]); // FIXME: multiple classes?
192: }
193:
194: return a.getClass().isAssignableFrom(b.getClass());
195: }
196: }