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