Skip to contentMethod: lambda$toCompositePresentationModel$0(Function, As)
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.role.ui.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.function.Function;
35: import java.util.stream.Stream;
36: import java.util.stream.StreamSupport;
37: import it.tidalwave.util.As;
38: import it.tidalwave.util.spi.ArrayListCollectorSupport;
39: import it.tidalwave.role.Composite;
40: import it.tidalwave.role.SimpleComposite;
41: import it.tidalwave.role.ui.PresentationModel;
42: import static it.tidalwave.util.Parameters.r;
43: import static it.tidalwave.role.ui.Presentable._Presentable_;
44:
45: /***********************************************************************************************************************
46: *
47: * A {@link java.util.stream.Collector} which collects a {@link Stream} of {@link PresentationModel}s into a single
48: * {@code PresentationModel} with a {@code Composite<PresentationModel>} role containing them.
49: *
50: * @author Fabrizio Giudici
51: *
52: **********************************************************************************************************************/
53: public class PresentationModelCollectors extends ArrayListCollectorSupport<PresentationModel, PresentationModel>
54: {
55: @Nonnull
56: private final Collection<Object> roles = new ArrayList<>();
57:
58: /*******************************************************************************************************************
59: *
60: * A {@link java.util.stream.Collector} which collects a {@link Stream} of {@link PresentationModel}s into a single
61: * {@code PresentationModel} with a {@link Composite} role containing them. In other words:
62: *
63: * <pre>
64: * List<PresentationModel> pms = ...
65: * PresentationModel compositePm = pms.stream().collect(toCompositePresentationModel());
66: * // same contents as childrenPms
67: * List<PresentationModel> childrenPms = compositePm.as(_Composite_).findChildren().results();
68: * </pre>
69: *
70: * @param roles some extra roles included in the resulting {@code PresentationModel}
71: * @return a {@code PresentationModel}
72: * @since 3.2-ALPHA-3 (refactored)
73: *
74: ******************************************************************************************************************/
75: @Nonnull
76: public static PresentationModelCollectors toCompositePresentationModel (@Nonnull final Collection<Object> roles)
77: {
78: return new PresentationModelCollectors(roles);
79: }
80:
81: @Nonnull
82: public static PresentationModelCollectors toCompositePresentationModel()
83: {
84: return toCompositePresentationModel((Collection<Object>)Collections.emptyList());
85: }
86:
87: /*******************************************************************************************************************
88: *
89: * A facility method that creates a composite {@link PresentationModel} out of an iterable (which means an array,
90: * a collection or a stream) of objects implementing {@link As}. For each object in the iterable, its {@code
91: * PresentationModel} is created by means of invoking its {@link it.tidalwave.role.ui.Presentable} role. Then all
92: * the {@code PresentationModel}s are aggregated into the composite.
93: *
94: * A function which creates specific roles for each {@code PresentationModel} can be specified. The function can
95: * return a single role or multiple roles in form of an array {@code Object[]}.
96: *
97: * @param <T> the type of the objects
98: * @param i the {@code Iterable}
99: * @param roleCreator the function to create roles
100: * @return the composite {@code PresentationModel}
101: *
102: ******************************************************************************************************************/
103: @Nonnull
104: public static <T extends As> PresentationModel toCompositePresentationModel (
105: @Nonnull final Iterable<T> i,
106: @Nonnull final Function<T, Object> roleCreator)
107: {
108: return StreamSupport.stream(i.spliterator(), false)
109: .map(o -> o.as(_Presentable_).createPresentationModel(r(roleCreator.apply(o))))
110: .collect(PresentationModelCollectors.toCompositePresentationModel());
111: }
112:
113: /*******************************************************************************************************************
114: *
115: * A facility simplified version of
116: * {@link #toCompositePresentationModel(java.lang.Iterable, java.util.function.Function)}.
117: *
118: * @param <T> the type of the objects
119: * @param i the {@code Iterable}
120: * @return the composite {@code PresentationModel}
121: *
122: ******************************************************************************************************************/
123: @Nonnull
124: public static <T extends As> PresentationModel toCompositePresentationModel (@Nonnull final Iterable<T> i)
125: {
126: return toCompositePresentationModel(i, o -> new Object[0]);
127: }
128:
129: /*******************************************************************************************************************
130: *
131: * {@inheritDoc}
132: *
133: ******************************************************************************************************************/
134: @Override @Nonnull
135: public Function<List<PresentationModel>, PresentationModel> finisher()
136: {
137: return childrenPms -> PresentationModel.of("", r(roles, SimpleComposite.ofCloned(childrenPms)));
138: }
139:
140: /*******************************************************************************************************************
141: *
142: *
143: ******************************************************************************************************************/
144: private PresentationModelCollectors (@Nonnull final Collection<Object> roles)
145: {
146: this.roles.addAll(roles);
147: }
148: }