Skip to content

Method: PropertyUtilities()

1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone git@bitbucket.org:tidalwave/northernwind-rca-src.git
7: * %%
8: * Copyright (C) 2013 - 2021 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.rca.util;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Locale;
31: import java.time.ZoneId;
32: import java.time.format.DateTimeFormatter;
33: import java.time.temporal.TemporalAccessor;
34: import it.tidalwave.util.Key;
35: import it.tidalwave.role.ui.Displayable;
36: import it.tidalwave.northernwind.core.model.ResourceProperties;
37: import lombok.Getter;
38: import lombok.Setter;
39: import lombok.experimental.UtilityClass;
40:
41: /***********************************************************************************************************************
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: @UtilityClass
47: public class PropertyUtilities
48: {
49: @Getter @Setter
50: private static Locale locale = Locale.getDefault();
51:
52: @Getter @Setter
53: private static ZoneId zone = ZoneId.systemDefault();
54:
55: @FunctionalInterface
56: static interface Format
57: {
58: @Nonnull
59: public String format (Object object);
60:
61: static final Format DEFAULT_FORMAT = object -> "" + object;
62: static final Format DATE_TIME_FORMAT = object ->
63: DateTimeFormatter.ISO_DATE_TIME.format((TemporalAccessor)object).replaceAll("\\[.*\\]", "");
64:
65: @Nonnull
66: public static Format formatFor (@Nonnull final Key<?> key)
67: {
68: if (TemporalAccessor.class.isAssignableFrom(key.getType()))
69: {
70: return DATE_TIME_FORMAT;
71: }
72:
73: return DEFAULT_FORMAT;
74: }
75: }
76:
77: /*******************************************************************************************************************
78: *
79: * Creates a {@link Displayable} for a property value.
80: *
81: * @param properties the {@link ResourceProperties} containing the value
82: * @param key the key associated to the value
83: * @return the {@code Displayable}
84: *
85: ******************************************************************************************************************/
86: @Nonnull
87: public static Displayable displayableForValue (@Nonnull final ResourceProperties properties, @Nonnull final Key<?> key)
88: {
89: return properties.getProperty(key).map(value -> Displayable.of(Format.formatFor(key).format(value)))
90: .orElse(Displayable.DEFAULT);
91: }
92: }