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;
27
28 import javax.annotation.Nonnull;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Optional;
33
34 /***************************************************************************************************************************************************************
35 *
36 * A factory of roles.
37 *
38 * @param <T> the type of the owner
39 * @author Fabrizio Giudici
40 *
41 **************************************************************************************************************************************************************/
42 @FunctionalInterface
43 public interface RoleFactory<T, R>
44 {
45 /***********************************************************************************************************************************************************
46 * {@return one or more roles — if multiple ones have to be returned, they must be wrapped in a {@link java.util.Collection}}.
47 * @param owner the owner
48 **********************************************************************************************************************************************************/
49 @Nonnull
50 public Optional<R> createRoleFor (@Nonnull T owner);
51
52 /***********************************************************************************************************************************************************
53 * {@return the role type}.
54 * @since 4.0-ALPHA-2
55 **********************************************************************************************************************************************************/
56 @Nonnull @SuppressWarnings("unchecked")
57 public default Class<R> getRoleType()
58 {
59 return (Class<R>)ReflectionUtils.getTypeArguments(RoleFactory.class, getClass()).get(1);
60 }
61
62 /***********************************************************************************************************************************************************
63 * {@return a collection of roles resolving factories}; that is, any factory in the collection is invoked so it can eventually create another role.
64 * @param roleOrFactories a collection of roles or {@link RoleFactory} instances
65 * @param owner the role owner
66 * @since 4.0-ALPHA-2
67 **********************************************************************************************************************************************************/
68 @Nonnull @SuppressWarnings("unchecked")
69 public static <T> Collection<Object> resolveFactories (@Nonnull final T owner, @Nonnull final Iterable<Object> roleOrFactories)
70 {
71 final List<Object> result = new ArrayList<>();
72
73 for (final var roleOrFactory : roleOrFactories)
74 {
75 if (roleOrFactory instanceof RoleFactory)
76 {
77 ((RoleFactory<T, ?>)roleOrFactory).createRoleFor(owner).ifPresent(result::add);
78 }
79 else
80 {
81 result.add(roleOrFactory);
82 }
83 }
84
85 return result;
86 }
87 }