Skip to content

Package: SimpleCompositePresentable$1

SimpleCompositePresentable$1

nameinstructionbranchcomplexitylinemethod
run()
M: 0 C: 34
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
{...}
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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 javax.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.List;
31: import it.tidalwave.util.As;
32: import it.tidalwave.util.RoleFactory;
33: import it.tidalwave.util.Task;
34: import it.tidalwave.util.spi.ContextSnapshot;
35: import it.tidalwave.util.spi.SimpleFinderSupport;
36: import it.tidalwave.role.SimpleComposite;
37: import it.tidalwave.ui.core.role.Presentable;
38: import it.tidalwave.ui.core.role.PresentationModel;
39: import it.tidalwave.ui.core.role.PresentationModelFactory;
40: import lombok.RequiredArgsConstructor;
41: import lombok.extern.slf4j.Slf4j;
42: import static java.util.Collections.emptyList;
43: import static java.util.stream.Collectors.*;
44: import static it.tidalwave.util.ShortNames.*;
45: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
46:
47: /***************************************************************************************************************************************************************
48: *
49: * An implementation of {@link Presentable} for datum instances having the {@link SimpleComposite} role.
50: *
51: * @stereotype Role
52: * @since 2.0-ALPHA-1
53: * @author Fabrizio Giudici
54: *
55: **************************************************************************************************************************************************************/
56: @Slf4j
57: public class SimpleCompositePresentable implements Presentable
58: {
59: @RequiredArgsConstructor
60: static class SCPFinder extends SimpleFinderSupport<PresentationModel>
61: {
62: // private static final long serialVersionUID = -3235827383866946732L;
63:
64: @Nonnull
65: private final SimpleCompositePresentable scp;
66:
67: @Nonnull
68: private final Collection<Object> roles;
69:
70: public SCPFinder (@Nonnull final SCPFinder other, @Nonnull final Object override)
71: {
72: super(other, override);
73: final var source = getSource(SCPFinder.class, other, override);
74: this.scp = source.scp;
75: this.roles = source.roles;
76: }
77:
78: @Override @Nonnull
79: protected List<PresentationModel> computeResults()
80: {
81: return scp.contextSnapshot.runWithContexts(new Task<>()
82: {
83: @Override @Nonnull
84: public List<PresentationModel> run()
85: {
86: final List<As> children = scp.datum.maybeAs(_SimpleComposite_)
87: .map(c -> c.findChildren().results()).orElse(emptyList());
88: return children.stream()
89: .map(child -> child.maybeAs(_Presentable_)
90: .orElseGet(() -> new SimpleCompositePresentable(child)))
91: .map(presentable -> presentable.createPresentationModel(roles))
92: .collect(toList());
93: }
94: });
95: }
96: }
97:
98: private static final long serialVersionUID = 324646965695684L;
99:
100: @Nonnull
101: private final As datum;
102:
103: // This is not @Injected to avoid a dependency on Spring AOP
104: @Nonnull
105: private final PresentationModelFactory defaultPresentationModelFactory;
106:
107: private final ContextSnapshot contextSnapshot;
108:
109: /***********************************************************************************************************************************************************
110: * @param datum the owner
111: **********************************************************************************************************************************************************/
112: public SimpleCompositePresentable (@Nonnull final As datum)
113: {
114: this(datum, new DefaultPresentationModelFactory());
115: }
116:
117: /***********************************************************************************************************************************************************
118: * @param datum the owner
119: * @param defaultPresentationModelFactory the {@code PresentationModelFactory}
120: **********************************************************************************************************************************************************/
121: public SimpleCompositePresentable (@Nonnull final As datum,
122: @Nonnull final PresentationModelFactory defaultPresentationModelFactory)
123: {
124: this.datum = datum;
125: this.defaultPresentationModelFactory = defaultPresentationModelFactory;
126: contextSnapshot = new ContextSnapshot(datum);
127: }
128:
129: /***********************************************************************************************************************************************************
130: * {@inheritDoc}
131: **********************************************************************************************************************************************************/
132: @Override @Nonnull
133: public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
134: {
135: return internalCreatePresentationModel(datum, roles);
136: }
137:
138: /***********************************************************************************************************************************************************
139: *
140: **********************************************************************************************************************************************************/
141: @Nonnull
142: private PresentationModel internalCreatePresentationModel (@Nonnull final As datum,
143: @Nonnull final Collection<Object> roles)
144: {
145: final var pmFinder = new SCPFinder(this, roles);
146:
147: return contextSnapshot.runWithContexts(new Task<>()
148: {
149: @Override @Nonnull
150: public PresentationModel run()
151: {
152: final var r = RoleFactory.resolveFactories(datum, roles);
153:
154:• if (datum.maybeAs(_SimpleComposite_).isPresent())
155: {
156: r.add(SimpleComposite.of(pmFinder));
157: }
158:
159: log.trace(">>>> r for {}: {}", shortId(datum), shortIds(r));
160:
161: return defaultPresentationModelFactory.createPresentationModel(datum, r);
162: }
163: });
164: }
165: }