Skip to content

Package: DefaultResourceProperties$1

DefaultResourceProperties$1

nameinstructionbranchcomplexitylinemethod
lambda$new$0(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
{...}
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
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.impl.model;
28:
29: import javax.annotation.Nonnull;
30: import java.time.ZonedDateTime;
31: import java.util.Collection;
32: import java.util.HashMap;
33: import java.util.List;
34: import java.util.Map;
35: import java.util.Optional;
36: import java.util.concurrent.CopyOnWriteArrayList;
37: import java.util.function.Function;
38: import java.io.IOException;
39: import it.tidalwave.util.As;
40: import it.tidalwave.util.Id;
41: import it.tidalwave.util.Key;
42: import it.tidalwave.util.NotFoundException;
43: import it.tidalwave.northernwind.core.model.ResourcePath;
44: import it.tidalwave.northernwind.core.model.ResourceProperties;
45: import lombok.Getter;
46: import lombok.ToString;
47: import lombok.experimental.Delegate;
48: import lombok.extern.slf4j.Slf4j;
49: import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;
50: import static java.util.stream.Collectors.*;
51:
52: /***********************************************************************************************************************
53: *
54: * The default implementation of {@link ResourceProperties}.
55: *
56: * @author Fabrizio Giudici
57: *
58: **********************************************************************************************************************/
59: // FIXME: this is a patched copy, needs public constructor for builder - see NW-180
60: @Slf4j @ToString(exclude={"propertyResolver", "asSupport"})
61: public class DefaultResourceProperties implements ResourceProperties
62: {
63: @SuppressWarnings("squid:S1171")
64: private static final Map<Class<?>, Function<String, Object>> CONVERTER_MAP =
65: new HashMap<>()
66: {{
67: put(Integer.class, Integer::parseInt);
68: put(Float.class, Float::parseFloat);
69: put(Double.class, Double::parseDouble);
70: put(Boolean.class, Boolean::parseBoolean);
71: put(ZonedDateTime.class, o -> ZonedDateTime.parse(o, ISO_ZONED_DATE_TIME));
72: put(ResourcePath.class, ResourcePath::of);
73: }};
74:
75: @Nonnull @Getter
76: private final Id id;
77:
78: /* Use String as key, and not Key. In this way properties can be managed both in an untyped fashion - e.g. by means
79: of Key.of("foo", Object.class) - and typed at the same time - e.g. Key.of("foo", Boolean.class). */
80: private final Map<String, Object> propertyMap = new HashMap<>();
81:
82: private final Map<Id, DefaultResourceProperties> groupMap = new HashMap<>();
83:
84: @Nonnull
85: private final PropertyResolver propertyResolver;
86:
87: @Delegate
88: private final As asSupport = As.forObject(this);
89:
90: /*******************************************************************************************************************
91: *
92: *
93: ******************************************************************************************************************/
94: public DefaultResourceProperties (@Nonnull final ResourceProperties.Builder builder)
95: {
96: this.id = builder.getId();
97: this.propertyResolver = builder.getPropertyResolver();
98: this.propertyMap.putAll(builder.getValues());
99: // for (final Entry<Key<?>, Object> entry : builder.getValues().entrySet())
100: // {
101: // final String s = entry.getKey().stringValue();
102: // final Object value = entry.getValue();
103: // propertyMap.put(new Key<>(s) {}, value);
104: // }
105: }
106:
107: /*******************************************************************************************************************
108: *
109: * Deep clone constructor.
110: *
111: ******************************************************************************************************************/
112: public DefaultResourceProperties (@Nonnull final DefaultResourceProperties otherProperties)
113: {
114: this.id = otherProperties.id;
115: this.propertyResolver = otherProperties.propertyResolver;
116:
117: otherProperties.propertyMap.forEach(propertyMap::put); // FIXME: clone the property
118: otherProperties.groupMap.forEach((k, v) -> groupMap.put(k, new DefaultResourceProperties(v)));
119: }
120:
121: /*******************************************************************************************************************
122: *
123: * Legacy code for converting from flat-style properties. This is different than passing from() in the Builder,
124: * since that approach doesn't support nested groups.
125: *
126: ******************************************************************************************************************/
127: public DefaultResourceProperties (@Nonnull final Id id,
128: @Nonnull final Map<String, Object> map,
129: @Nonnull final PropertyResolver propertyResolver)
130: {
131: this.id = id;
132: this.propertyResolver = propertyResolver;
133:
134: final Map<Id, Map<String, Object>> othersMap = new HashMap<>();
135:
136: for (final var entry : map.entrySet())
137: {
138: final var s = entry.getKey();
139: final var value = entry.getValue();
140:
141: if (!s.contains("."))
142: {
143: propertyMap.put(s, value);
144: }
145: else
146: {
147: final var x = s.split("\\.");
148: final var groupId = new Id(x[0]);
149: final var otherMap = othersMap.computeIfAbsent(groupId, __ -> new HashMap<>());
150: otherMap.put(x[1], value);
151: }
152: }
153:
154: for (final var entry : othersMap.entrySet())
155: {
156: groupMap.put(entry.getKey(), new DefaultResourceProperties(entry.getKey(), entry.getValue(), propertyResolver));
157: }
158: }
159:
160: /*******************************************************************************************************************
161: *
162: * {@inheritDoc}
163: *
164: ******************************************************************************************************************/
165: @Override @Nonnull
166: public <T> Optional<T> getProperty (@Nonnull final Key<? extends T> key)
167: {
168: try
169: {
170: final var value = propertyMap.get(key.getName());
171: return Optional.of(convertValue(key, (value != null) ? value : propertyResolver.resolveProperty(id, key)));
172: }
173: catch (IOException e)
174: {
175: log.trace("Could not resolve property", e);
176: return Optional.empty();
177: }
178: catch (NotFoundException e)
179: {
180: log.trace("Could not resolve property {}", e.getMessage());
181: return Optional.empty();
182: }
183: }
184:
185: /*******************************************************************************************************************
186: *
187: * {@inheritDoc}
188: *
189: ******************************************************************************************************************/
190: @Override @Nonnull
191: public ResourceProperties getGroup (@Nonnull final Id id)
192: {
193: final var properties = groupMap.get(id);
194: return properties != null ? properties : new DefaultResourceProperties(this);
195: // : new DefaultResourceProperties(new Builder().withId(id).withPropertyResolver(propertyResolver));
196: }
197:
198: /*******************************************************************************************************************
199: *
200: * {@inheritDoc}
201: *
202: ******************************************************************************************************************/
203: @Override @Nonnull
204: public Collection<Key<?>> getKeys()
205: {
206: return propertyMap.keySet().stream().map(Key::of).collect(toList());
207: }
208:
209: /*******************************************************************************************************************
210: *
211: * {@inheritDoc}
212: *
213: ******************************************************************************************************************/
214: @Override @Nonnull
215: public Collection<Id> getGroupIds() // FIXME: should be a Set
216: {
217: return new CopyOnWriteArrayList<>(groupMap.keySet());
218: }
219:
220: /*******************************************************************************************************************
221: *
222: * {@inheritDoc}
223: *
224: ******************************************************************************************************************/
225: @Override @Nonnull
226: public <T> DefaultResourceProperties withProperty (@Nonnull final Key<T> key, @Nonnull final T value)
227: {
228: final var result = new DefaultResourceProperties(this);
229: result.propertyMap.put(key.getName(), value); // FIXME: clone property
230: return result;
231: }
232:
233: /*******************************************************************************************************************
234: *
235: * {@inheritDoc}
236: *
237: ******************************************************************************************************************/
238: @Override @Nonnull
239: public DefaultResourceProperties withoutProperty (@Nonnull final Key<?> key)
240: {
241: final var result = new DefaultResourceProperties(this);
242: result.propertyMap.remove(key.getName());
243: return result;
244: }
245:
246: /*******************************************************************************************************************
247: *
248: * {@inheritDoc}
249: *
250: ******************************************************************************************************************/
251: @Override @Nonnull
252: public DefaultResourceProperties withProperties (@Nonnull final ResourceProperties properties)
253: {
254: final var result = new DefaultResourceProperties(this);
255: result.groupMap.put(properties.getId(), new DefaultResourceProperties((DefaultResourceProperties)properties));
256: return result;
257: }
258:
259: /*******************************************************************************************************************
260: *
261: * {@inheritDoc}
262: *
263: ******************************************************************************************************************/
264: @Override @Nonnull
265: public ResourceProperties merged (@Nonnull final ResourceProperties properties)
266: {
267: final var otherProperties = (DefaultResourceProperties)properties;
268:
269: if (!id.equals(otherProperties.id))
270: {
271: throw new IllegalArgumentException("Id mismatch " + id + " vs " + otherProperties.id);
272: }
273:
274: ResourceProperties result = new DefaultResourceProperties(this);
275:
276: for (final var entry : otherProperties.propertyMap.entrySet())
277: {
278: result = result.withProperty(Key.of(entry.getKey()), entry.getValue());
279: }
280:
281: for (final var entry : otherProperties.groupMap.entrySet())
282: {
283: final var groupId = entry.getKey();
284: final ResourceProperties propertyGroup = entry.getValue();
285: result = (!groupMap.containsKey(groupId)) ? result.withProperties(propertyGroup)
286: : result.withProperties(groupMap.get(groupId).merged(propertyGroup));
287: }
288:
289: return result;
290: }
291:
292: /*******************************************************************************************************************
293: *
294: * {@inheritDoc}
295: *
296: ******************************************************************************************************************/
297: @Override @Nonnull
298: public ResourceProperties withId (@Nonnull final Id id)
299: {
300: return new DefaultResourceProperties(this);
301: // return new DefaultResourceProperties(new Builder().withId(id).withPropertyResolver(propertyResolver));
302: }
303:
304: /*******************************************************************************************************************
305: *
306: * Converts a property value from String to its expected value. This is because properties are read by unmarshaller
307: * as string.
308: *
309: ******************************************************************************************************************/
310: @Nonnull
311: /* visible for testing */ static <T> T convertValue (@Nonnull final Key<T> key, @Nonnull final Object value)
312: {
313: log.trace("convertValue({}, {})", key, value);
314: final T result;
315:
316: try
317: {
318: if (key.getType().isAssignableFrom(value.getClass()))
319: {
320: result = key.getType().cast(value);
321: }
322: else if ("tags".equals(key.getName())) // workaround as Zephyr stores it as a comma-separated string
323: {
324: result = (T)List.of(((String)value).split(","));
325: }
326: // else if (value instanceof List)
327: // {
328: // final List<Object> list = (List<Object>)value;
329: // Class<?> elementType = String.class; // FIXME: should get the generic of the list
330: //
331: // return (T)list.stream()
332: // .map(i -> CONVERTER_MAP.getOrDefault(elementType, o -> o).apply(value))
333: // .collect(toList());
334: // }
335: else
336: {
337: result = (T)CONVERTER_MAP.getOrDefault(key.getType(), o -> o).apply((String)value);
338: }
339:
340: log.trace(">>>> returning {} ({})", result, result.getClass().getName());
341: return result;
342: }
343: catch (Exception e)
344: {
345: throw new RuntimeException(String.format("Can't convert '%s' to %s(%s)", value, key, key.getType()), e);
346: }
347: }
348: }