Skip to content

Package: Template$Aggregate

Template$Aggregate

nameinstructionbranchcomplexitylinemethod
Template.Aggregate()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
get(String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getMap()
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$with$0(String, Object)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(String, Object)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(String, Optional)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
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%
with(String, Object)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
with(String, Optional)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.core.model;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.HashMap;
32: import java.util.Iterator;
33: import java.util.List;
34: import java.util.Map;
35: import java.util.Optional;
36: import java.util.stream.Collector;
37: import java.util.stream.Stream;
38: import lombok.Getter;
39: import static java.util.Collections.emptyList;
40: import static java.util.stream.Collectors.*;
41:
42: /***********************************************************************************************************************
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: public interface Template
48: {
49: /*******************************************************************************************************************
50: *
51: ******************************************************************************************************************/
52: public static class Aggregate
53: {
54: @Getter
55: private final Map<String, Object> map = new HashMap<>();
56:
57: /***************************************************************************************************************
58: *
59: **************************************************************************************************************/
60: @Nonnull
61: public static Aggregate of (@Nonnull final String name, @Nonnull final Object value)
62: {
63: return new Aggregate().with(name, value);
64: }
65:
66: /***************************************************************************************************************
67: *
68: **************************************************************************************************************/
69: @Nonnull
70: public static Aggregate of (@Nonnull final String name, @Nonnull final Optional<?> value)
71: {
72: return new Aggregate().with(name, value);
73: }
74:
75: /***************************************************************************************************************
76: *
77: **************************************************************************************************************/
78: @Nonnull
79: public Aggregate with (@Nonnull final String name, @Nonnull final Object value)
80: {
81: map.put(name, value);
82: return this;
83: }
84:
85: /***************************************************************************************************************
86: *
87: **************************************************************************************************************/
88: @Nonnull
89: public Aggregate with (@Nonnull final String name, @Nonnull final Optional<?> value)
90: {
91: value.ifPresent(v -> map.put(name, v));
92: return this;
93: }
94:
95: /***************************************************************************************************************
96: *
97: **************************************************************************************************************/
98: @Nonnull
99: public Optional<Object> get (@Nonnull final String name)
100: {
101: return Optional.ofNullable(map.get(name));
102: }
103:
104: /***************************************************************************************************************
105: *
106: **************************************************************************************************************/
107: @Override @Nonnull
108: public String toString()
109: {
110: return map.toString();
111: }
112: }
113:
114: /*******************************************************************************************************************
115: *
116: ******************************************************************************************************************/
117: public static class Aggregates implements Iterable<Aggregate>
118: {
119: public static final Aggregates EMPTY = new Aggregates("", emptyList());
120:
121: @Getter @Nonnull
122: private final String name;
123:
124: @Getter @SuppressWarnings("squid:S1700")
125: private final List<Aggregate> aggregates = new ArrayList<>();
126:
127: /***************************************************************************************************************
128: *
129: **************************************************************************************************************/
130: public Aggregates (@Nonnull final String name, @Nonnull final List<Aggregate> aggregates)
131: {
132: this.name = name;
133: this.aggregates.addAll(aggregates);
134: }
135:
136: /***************************************************************************************************************
137: *
138: **************************************************************************************************************/
139: public boolean isEmpty()
140: {
141: return aggregates.isEmpty();
142: }
143:
144: /***************************************************************************************************************
145: *
146: **************************************************************************************************************/
147: @Override @Nonnull
148: public String toString()
149: {
150: return String.format("{%s: %s}", name, aggregates);
151: }
152:
153: /***************************************************************************************************************
154: *
155: **************************************************************************************************************/
156: @Override @Nonnull
157: public Iterator<Aggregate> iterator()
158: {
159: return aggregates.iterator();
160: }
161:
162: /***************************************************************************************************************
163: *
164: **************************************************************************************************************/
165: @Nonnull
166: public Stream<Aggregate> stream()
167: {
168: return aggregates.stream();
169: }
170:
171: /***************************************************************************************************************
172: *
173: * Returns a {@link Collector} that produces an instance of {@link Aggregates} with the given name.
174: *
175: * @param name the name
176: * @return the collector
177: *
178: **************************************************************************************************************/
179: @Nonnull
180: public static Collector<Aggregate, ?, Aggregates> toAggregates (@Nonnull final String name)
181: {
182: return collectingAndThen(toList(), list -> new Aggregates(name, list));
183: }
184:
185: /***************************************************************************************************************
186: *
187: **************************************************************************************************************/
188: public int getSize()
189: {
190: return aggregates.size();
191: }
192: }
193:
194: /*******************************************************************************************************************
195: *
196: ******************************************************************************************************************/
197: public Template addAttribute (@Nonnull String name, @Nonnull Object value);
198:
199: /*******************************************************************************************************************
200: *
201: ******************************************************************************************************************/
202: @Nonnull
203: public String render (@Nonnull final Aggregates ... aggregatesSet);
204:
205: /*******************************************************************************************************************
206: *
207: ******************************************************************************************************************/
208: @Nonnull
209: public default String render (@Nonnull final List<Aggregates> aggregatesSet)
210: {
211: return render(aggregatesSet.toArray(new Aggregates[0]));
212: }
213: }