Skip to content

Package: PresentationModelCollectors

PresentationModelCollectors

nameinstructionbranchcomplexitylinemethod
PresentationModelCollectors(Collection)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
finisher()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$finisher$2(List)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$toCompositePresentationModel$0(Function, As)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$toCompositePresentationModel$1(As)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toCompositePresentationModel()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toCompositePresentationModel(Collection)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toCompositePresentationModel(Iterable)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toCompositePresentationModel(Iterable, Function)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 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/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.core.role.spi;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.util.Collections;
32: import java.util.List;
33: import java.util.function.Function;
34: import java.util.stream.Stream;
35: import java.util.stream.StreamSupport;
36: import it.tidalwave.util.As;
37: import it.tidalwave.util.spi.ArrayListCollectorSupport;
38: import it.tidalwave.role.Composite;
39: import it.tidalwave.role.SimpleComposite;
40: import it.tidalwave.ui.core.role.PresentationModel;
41: import static it.tidalwave.util.Parameters.r;
42: import static it.tidalwave.ui.core.role.Presentable._Presentable_;
43:
44: /***************************************************************************************************************************************************************
45: *
46: * A {@link java.util.stream.Collector} which collects a {@link Stream} of {@link PresentationModel}s into a single
47: * {@code PresentationModel} with a {@code Composite<PresentationModel>} role containing them.
48: *
49: * @since 2.0-ALPHA-1
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: * A {@link java.util.stream.Collector} which collects a {@link Stream} of {@link PresentationModel}s into a single
60: * {@code PresentationModel} with a {@link Composite} role containing them. In other words:
61: *
62: * <pre>
63: * List<PresentationModel> pms = ...
64: * PresentationModel compositePm = pms.stream().collect(toCompositePresentationModel());
65: * // same contents as childrenPms
66: * List<PresentationModel> childrenPms = compositePm.as(_Composite_).findChildren().results();
67: * </pre>
68: *
69: * @param roles some extra roles included in the resulting {@code PresentationModel}
70: * @return a {@code PresentationModel}
71: * @since 3.2-ALPHA-3 (refactored)
72: **********************************************************************************************************************************************************/
73: @Nonnull
74: public static PresentationModelCollectors toCompositePresentationModel (@Nonnull final Collection<?> roles)
75: {
76: return new PresentationModelCollectors(roles);
77: }
78:
79: @Nonnull
80: public static PresentationModelCollectors toCompositePresentationModel()
81: {
82: return toCompositePresentationModel((Collection<Object>)Collections.emptyList());
83: }
84:
85: /***********************************************************************************************************************************************************
86: * A facility method that creates a composite {@link PresentationModel} out of an iterable (which means an array,
87: * a collection or a stream) of objects implementing {@link As}. For each object in the iterable, its {@code
88: * PresentationModel} is created by means of invoking its {@link it.tidalwave.ui.core.role.Presentable} role. Then all
89: * the {@code PresentationModel}s are aggregated into the composite.
90: *
91: * A function which creates specific roles for each {@code PresentationModel} can be specified. The function can
92: * return a single role or multiple roles in form of an array {@code Object[]}.
93: *
94: * @param <T> the type of the objects
95: * @param i the {@code Iterable}
96: * @param roleCreator the function to create roles
97: * @return the composite {@code PresentationModel}
98: **********************************************************************************************************************************************************/
99: @Nonnull
100: public static <T extends As> PresentationModel toCompositePresentationModel (
101: @Nonnull final Iterable<? extends T> i,
102: @Nonnull final Function<? super T, Object> roleCreator)
103: {
104: return StreamSupport.stream(i.spliterator(), false)
105: .map(o -> o.as(_Presentable_).createPresentationModel(r(roleCreator.apply(o))))
106: .collect(toCompositePresentationModel());
107: }
108:
109: /***********************************************************************************************************************************************************
110: * A facility simplified version of
111: * {@link #toCompositePresentationModel(java.lang.Iterable, java.util.function.Function)}.
112: *
113: * @param <T> the type of the objects
114: * @param i the {@code Iterable}
115: * @return the composite {@code PresentationModel}
116: **********************************************************************************************************************************************************/
117: @Nonnull
118: public static <T extends As> PresentationModel toCompositePresentationModel (@Nonnull final Iterable<T> i)
119: {
120: return toCompositePresentationModel(i, o -> new Object[0]);
121: }
122:
123: /***********************************************************************************************************************************************************
124: * {@inheritDoc}
125: **********************************************************************************************************************************************************/
126: @Override @Nonnull
127: public Function<List<PresentationModel>, PresentationModel> finisher()
128: {
129: return childrenPms -> PresentationModel.of("", r(roles, SimpleComposite.ofCloned(childrenPms)));
130: }
131:
132: /***********************************************************************************************************************************************************
133: *
134: **********************************************************************************************************************************************************/
135: private PresentationModelCollectors (@Nonnull final Collection<?> roles)
136: {
137: this.roles.addAll(roles);
138: }
139: }