Skip to content

Package: ResourceProperties$PropertyResolver

ResourceProperties$PropertyResolver

nameinstructionbranchcomplexitylinemethod
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * NorthernWind - lightweight CMS
5: * http://tidalwave.it/projects/northernwind
6: *
7: * Copyright (C) 2011 - 2025 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/northernwind-src
22: * git clone https://github.com/tidalwave-it/northernwind-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.northernwind.core.model;
27:
28: import javax.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.Collections;
31: import java.util.List;
32: import java.util.Map;
33: import java.util.Optional;
34: import java.util.stream.Collectors;
35: import java.io.IOException;
36: import it.tidalwave.util.As;
37: import it.tidalwave.util.Id;
38: import it.tidalwave.util.Key;
39: import it.tidalwave.util.NotFoundException;
40: import it.tidalwave.util.TypeSafeMap;
41: import it.tidalwave.role.Identifiable;
42: import lombok.AccessLevel;
43: import lombok.AllArgsConstructor;
44: import lombok.Getter;
45: import lombok.RequiredArgsConstructor;
46: import lombok.ToString;
47: import lombok.With;
48:
49: /***************************************************************************************************************************************************************
50: *
51: * A bag of properties for a {@link Resource}s.
52: *
53: * @author Fabrizio Giudici
54: *
55: **************************************************************************************************************************************************************/
56: public interface ResourceProperties extends As, Identifiable
57: {
58: /***********************************************************************************************************************************************************
59: * A builder of a {@link ResourceProperties}.
60: **********************************************************************************************************************************************************/
61: @AllArgsConstructor(access = AccessLevel.PRIVATE) @RequiredArgsConstructor
62: @Getter @ToString(exclude = "callBack")
63: public final class Builder
64: {
65: // Workaround for a Lombok limitation with Wither and subclasses
66: @FunctionalInterface
67: public static interface CallBack
68: {
69: @Nonnull
70: public ResourceProperties build (@Nonnull Builder builder);
71: }
72:
73: @Nonnull
74: private final ModelFactory modelFactory;
75:
76: @Nonnull
77: private final CallBack callBack;
78:
79: @With
80: private Id id = new Id("");
81:
82: @With @Deprecated
83: private Map<String, Object> values = Collections.emptyMap();
84:
85: @With
86: private PropertyResolver propertyResolver = PropertyResolver.DEFAULT;
87:
88: @Nonnull
89: public ResourceProperties build()
90: {
91: return callBack.build(this);
92: }
93:
94: /***************************************************************************************************************
95: *
96: * TODO: deprecate withValues(Map<String, Object>) and rename this to withValues().
97: *
98: **************************************************************************************************************/
99: @Nonnull
100: public Builder withSafeValues (@Nonnull final TypeSafeMap values)
101: {
102: return withValues(values.asMap().entrySet().stream()
103: .collect(Collectors.toMap(e -> e.getKey().getName(), Map.Entry::getValue)));
104: }
105: }
106:
107: public static interface PropertyResolver // FIXME: drop this
108: {
109: public static PropertyResolver DEFAULT = new PropertyResolver()
110: {
111: @Nonnull
112: @Override
113: public <T> T resolveProperty (@Nonnull final Id propertyGroupId, @Nonnull final Key<T> key)
114: throws NotFoundException
115: {
116: throw new NotFoundException(key.stringValue());
117: }
118: };
119:
120: @Nonnull
121: public <T> T resolveProperty (@Nonnull Id propertyGroupId, @Nonnull Key<T> key)
122: throws NotFoundException, IOException;
123: }
124:
125: /***********************************************************************************************************************************************************
126: * Retrieves a property.
127: *
128: * @param key the property key
129: * @return the property value
130: **********************************************************************************************************************************************************/
131: @Nonnull
132: public <T> Optional<T> getProperty (@Nonnull Key<? extends T> key);
133:
134: /***********************************************************************************************************************************************************
135: * Retrieves a property, searching through a sequence of keys.
136: *
137: * @param keys the property keys
138: * @return the property value
139: **********************************************************************************************************************************************************/
140: @Nonnull
141: public default <T> Optional<T> getProperty (@Nonnull final List<? extends Key<T>> keys)
142: {
143: return keys.stream().flatMap(key -> getProperty(key).stream()).findFirst();
144: }
145:
146: /***********************************************************************************************************************************************************
147: * Retrieves a subgroup of properties.
148: *
149: * @param id the id
150: * @return the property group
151: **********************************************************************************************************************************************************/
152: @Nonnull
153: public ResourceProperties getGroup (@Nonnull Id id);
154:
155: /***********************************************************************************************************************************************************
156: * Retrieves the collection of property keys.
157: *
158: * @return the property keys
159: **********************************************************************************************************************************************************/
160: @Nonnull
161: public Collection<Key<?>> getKeys();
162:
163: /***********************************************************************************************************************************************************
164: * Retrieves the collection of ids of groups.
165: *
166: * @return the group ids
167: **********************************************************************************************************************************************************/
168: @Nonnull
169: public Collection<Id> getGroupIds();
170:
171: /***********************************************************************************************************************************************************
172: * Returns a new instance with an additional property.
173: **********************************************************************************************************************************************************/
174: @Nonnull
175: public <T> ResourceProperties withProperty (@Nonnull Key<T> key, @Nonnull T value);
176:
177: /***********************************************************************************************************************************************************
178: * Returns a new instance without a property.
179: **********************************************************************************************************************************************************/
180: @Nonnull
181: public ResourceProperties withoutProperty (@Nonnull Key<?> key);
182:
183: /***********************************************************************************************************************************************************
184: * Returns a new instance with an additional property group.
185: **********************************************************************************************************************************************************/
186: @Nonnull
187: public ResourceProperties withProperties (@Nonnull ResourceProperties properties);
188:
189: /***********************************************************************************************************************************************************
190: * Returns a new instance which is the logical merge with other properties.
191: **********************************************************************************************************************************************************/
192: @Nonnull
193: public ResourceProperties merged (@Nonnull ResourceProperties properties);
194:
195: /***********************************************************************************************************************************************************
196: * Returns a clone with a new id.
197: **********************************************************************************************************************************************************/
198: @Nonnull
199: public ResourceProperties withId (@Nonnull Id id);
200: }