Package: DefaultSiteFinder
DefaultSiteFinder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultSiteFinder(DefaultSiteFinder, Object) |
|
|
|
|
|
||||||||||||||||||||
DefaultSiteFinder(Map, RegexTreeMap, String, String) |
|
|
|
|
|
||||||||||||||||||||
DefaultSiteFinder(String, Map, RegexTreeMap) |
|
|
|
|
|
||||||||||||||||||||
addResults(List, Map, String) |
|
|
|
|
|
||||||||||||||||||||
computeResults() |
|
|
|
|
|
||||||||||||||||||||
fields(Map, RegexTreeMap, String, String) |
|
|
|
|
|
||||||||||||||||||||
result() |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
||||||||||||||||||||
withRelativePath(String) |
|
|
|
|
|
||||||||||||||||||||
withRelativeUri(String) |
|
|
|
|
|
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.CheckForNull;
30: import javax.annotation.Nonnull;
31: import javax.annotation.Nullable;
32: import javax.annotation.concurrent.Immutable;
33: import java.util.ArrayList;
34: import java.util.List;
35: import java.util.Map;
36: import java.util.Objects;
37: import java.util.regex.Pattern;
38: import it.tidalwave.util.NotFoundException;
39: import it.tidalwave.util.spi.HierarchicFinderSupport;
40: import it.tidalwave.northernwind.core.model.SiteFinder;
41: import it.tidalwave.northernwind.core.impl.util.RegexTreeMap;
42: import lombok.AccessLevel;
43: import lombok.RequiredArgsConstructor;
44: import lombok.ToString;
45:
46: /***********************************************************************************************************************
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @Immutable @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
52: @ToString(callSuper = true, exclude = {"mapByRelativePath", "mapByRelativeUri" })
53: public class DefaultSiteFinder<T> extends HierarchicFinderSupport<T, SiteFinder<T>> implements SiteFinder<T>
54: {
55: private static final long serialVersionUID = 3242345356779345L;
56:
57: /* package */
58: @Nonnull final transient Map<String, T> mapByRelativePath;
59:
60: /* package */
61: @Nullable final transient RegexTreeMap<T> mapByRelativeUri;
62:
63: @Nullable
64: private final String relativePath;
65:
66: @Nullable
67: private final String relativeUri;
68:
69: /*******************************************************************************************************************
70: *
71: * Constructor used to create an instance with the given data.
72: *
73: * @param finderName the name (for debugging)
74: * @param mapByRelativePath the map of resources by relative path
75: * @param mapByRelativeUri the map of resources by relative uri
76: *
77: ******************************************************************************************************************/
78: public DefaultSiteFinder (@Nonnull final String finderName,
79: @CheckForNull final Map<String, T> mapByRelativePath,
80: @CheckForNull final RegexTreeMap<T> mapByRelativeUri)
81: {
82: super(finderName);
83: this.mapByRelativePath = mapByRelativePath;
84: this.mapByRelativeUri = mapByRelativeUri;
85: this.relativePath = null;
86: this.relativeUri = null;
87: }
88:
89: /*******************************************************************************************************************
90: *
91: *
92: *
93: ******************************************************************************************************************/
94: @Nonnull
95: private static <T> DefaultSiteFinder<T> fields (@CheckForNull final Map<String, T> mapByRelativePath,
96: @CheckForNull final RegexTreeMap<T> mapByRelativeUri,
97: @CheckForNull final String relativePath,
98: @CheckForNull final String relativeUri)
99: {
100: Objects.requireNonNull(mapByRelativePath, "Searching for a relativePath, but no map");
101: return new DefaultSiteFinder<>(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri);
102: }
103:
104: /*******************************************************************************************************************
105: *
106: * Clone constructor. See documentation of {@link HierarchicFinderSupport} for more information.
107: *
108: * @param other the {@code Finder} to clone
109: * @param override the override object
110: *
111: ******************************************************************************************************************/
112: // FIXME: should be protected
113: public DefaultSiteFinder (@Nonnull final DefaultSiteFinder<T> other, @Nonnull final Object override)
114: {
115: super(other, override);
116: final DefaultSiteFinder<T> source = getSource(DefaultSiteFinder.class, other, override);
117: this.mapByRelativePath = source.mapByRelativePath;
118: this.mapByRelativeUri = source.mapByRelativeUri;
119: this.relativePath = source.relativePath;
120: this.relativeUri = source.relativeUri;
121: }
122:
123: /*******************************************************************************************************************
124: *
125: * {@inheritDoc}
126: *
127: ******************************************************************************************************************/
128: @Override @Nonnull
129: public SiteFinder<T> withRelativePath (@Nonnull final String relativePath)
130: {
131: return clonedWith(fields(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri));
132: }
133:
134: /*******************************************************************************************************************
135: *
136: * {@inheritDoc}
137: *
138: ******************************************************************************************************************/
139: @Override @Nonnull
140: public SiteFinder<T> withRelativeUri (@Nonnull final String relativeUri)
141: {
142: return clonedWith(fields(mapByRelativePath, mapByRelativeUri, relativePath, relativeUri));
143: }
144:
145: /*******************************************************************************************************************
146: *
147: * {@inheritDoc}
148: *
149: ******************************************************************************************************************/
150: @Override @Nonnull
151: public T result()
152: throws NotFoundException
153: {
154: try
155: {
156: return super.result();
157: }
158: catch (NotFoundException e)
159: {
160: var message = "????";
161:
162:• if (relativePath != null)
163: {
164: message = String.format("relativePath: %s", relativePath);
165: // message = String.format("relativePath: %s, set: %s", relativePath, mapByRelativePath.keySet());
166: }
167:• else if (relativeUri != null)
168: {
169: message = String.format("relativeUri: %s", relativeUri);
170: // message = String.format("relativeUri: %s, set: %s", relativeUri, mapByRelativeUri.keySet());
171: }
172:
173: throw new NotFoundException(message);
174: }
175: }
176:
177: /*******************************************************************************************************************
178: *
179: * {@inheritDoc}
180: *
181: ******************************************************************************************************************/
182: @Override @Nonnull
183: protected List<T> computeResults()
184: {
185: final List<T> results = new ArrayList<>();
186:
187:• if (relativePath != null)
188: {
189: addResults(results, mapByRelativePath, relativePath);
190: }
191:
192:• else if (relativeUri != null)
193: {
194:• if (mapByRelativeUri == null)
195: {
196: throw new IllegalArgumentException("Searching for a relativeUri, but no map - " + this);
197: }
198:
199: addResults(results, mapByRelativeUri, relativeUri);
200: }
201:
202: else
203: {
204: results.addAll(mapByRelativePath.values());
205: }
206:
207: return results;
208: }
209:
210: /*******************************************************************************************************************
211: *
212: *
213: *
214: ******************************************************************************************************************/
215: private static <Type> void addResults (@Nonnull final List<? super Type> results,
216: @Nonnull final Map<String, Type> map,
217: @Nonnull final String relativePath)
218: {
219:• if (!relativePath.contains("*")) // FIXME: better way to guess a regexp?
220: {
221: final var result = map.get(relativePath);
222:
223:• if (result != null)
224: {
225: results.add(result);
226: }
227: }
228:
229: else
230: {
231: final var pattern = Pattern.compile(relativePath);
232:
233:• for (final var entry : map.entrySet())
234: {
235:• if (pattern.matcher(entry.getKey()).matches())
236: {
237: results.add(entry.getValue());
238: }
239: }
240: }
241: }
242: }