|
22 | 22 | import java.util.Collections;
|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Map;
|
| 25 | +import java.util.function.Function; |
25 | 26 |
|
26 | 27 | import org.eclipse.aether.ConfigurationProperties;
|
27 | 28 | import org.eclipse.aether.RepositorySystemSession;
|
|
31 | 32 | * Helps to sort pluggable components by their priority.
|
32 | 33 | */
|
33 | 34 | final class PrioritizedComponents<T> {
|
| 35 | + /** |
| 36 | + * Reuses or creates and stores (if session data does not contain yet) prioritized components under this key into |
| 37 | + * given session. Same session is used to configure prioritized components. |
| 38 | + * <p> |
| 39 | + * The {@code components} are expected to be Sisu injected {@link Map<String, C>}-like component maps. There is a |
| 40 | + * simple "change detection" in place, as injected maps are dynamic, they are atomically expanded or contracted |
| 41 | + * as components are dynamically discovered or unloaded. |
| 42 | + * |
| 43 | + * @since TBD |
| 44 | + */ |
| 45 | + @SuppressWarnings("unchecked") |
| 46 | + public static <C> PrioritizedComponents<C> reuseOrCreate( |
| 47 | + RepositorySystemSession session, Map<String, C> components, Function<C, Float> priorityFunction) { |
| 48 | + boolean cached = ConfigUtils.getBoolean( |
| 49 | + session, ConfigurationProperties.DEFAULT_CACHED_PRIORITIES, ConfigurationProperties.CACHED_PRIORITIES); |
| 50 | + if (cached) { |
| 51 | + String key = PrioritizedComponents.class.getName() + ".pc" + Integer.toHexString(components.hashCode()); |
| 52 | + return (PrioritizedComponents<C>) |
| 53 | + session.getData().computeIfAbsent(key, () -> create(session, components, priorityFunction)); |
| 54 | + } else { |
| 55 | + return create(session, components, priorityFunction); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static <C> PrioritizedComponents<C> create( |
| 60 | + RepositorySystemSession session, Map<String, C> components, Function<C, Float> priorityFunction) { |
| 61 | + PrioritizedComponents<C> newInstance = new PrioritizedComponents<>(session); |
| 62 | + components.values().forEach(c -> newInstance.add(c, priorityFunction.apply(c))); |
| 63 | + return newInstance; |
| 64 | + } |
34 | 65 |
|
35 | 66 | private static final String FACTORY_SUFFIX = "Factory";
|
36 | 67 |
|
|
0 commit comments