1 /**
2 * This file Copyright (c) 2015-2018 Magnolia International
3 * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4 *
5 *
6 * This file is dual-licensed under both the Magnolia
7 * Network Agreement and the GNU General Public License.
8 * You may elect to use one or the other of these licenses.
9 *
10 * This file is distributed in the hope that it will be
11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14 * Redistribution, except as permitted by whichever of the GPL
15 * or MNA you select, is prohibited.
16 *
17 * 1. For the GPL license (GPL), you can redistribute and/or
18 * modify this file under the terms of the GNU General
19 * Public License, Version 3, as published by the Free Software
20 * Foundation. You should have received a copy of the GNU
21 * General Public License, Version 3 along with this program;
22 * if not, write to the Free Software Foundation, Inc., 51
23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * 2. For the Magnolia Network Agreement (MNA), this file
26 * and the accompanying materials are made available under the
27 * terms of the MNA which accompanies this distribution, and
28 * is available at http://www.magnolia-cms.com/mna.html
29 *
30 * Any modifications to this file must keep this entire header
31 * intact.
32 *
33 */
34 package info.magnolia.resourceloader.util;
35
36 import info.magnolia.resourceloader.Resource;
37 import info.magnolia.resourceloader.ResourceOrigin;
38 import info.magnolia.resourceloader.layered.LayeredResourceOrigin;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Comparator;
43 import java.util.Objects;
44 import java.util.function.Function;
45 import java.util.function.Predicate;
46 import java.util.regex.Pattern;
47
48 import com.google.common.base.Predicates;
49
50 /**
51 * Functional-ish {@link Function}, {@link Predicate} and {@link Comparator} implementations that are useful when working with {@link ResourceOrigin} and {@link Resource}, and some more useful ones that had no better place for now.
52 * They are used to implement {@link LayeredResourceOrigin}, but could be reused for filtering and other purposes.
53 *
54 * // TODO consistent naming -- handle or path
55 * Most of this would be completely redundant with Java 8, since all these do is delegate to a given method of the input object: simple method references as lambdas would suffice.
56 */
57 public class Functions {
58
59 /**
60 * @deprecated since 6.1 use {@link ResourceOrigin#hasPath} and lambdas
61 */
62 @Deprecated
63 public static Predicate<ResourceOrigin> hasPath(final String path) {
64 return origin -> origin.hasPath(path);
65 }
66
67 public static Predicate<Resource> pathEquals(final CharSequence path) {
68 return pathMatches(path2 -> Objects.equals(path2, path));
69 }
70
71 public static Predicate<Resource> pathStartsWith(final CharSequence pathPrefix) {
72 return pathMatches("^" + pathPrefix);
73 }
74
75 /**
76 * @see #pathMatches(Pattern)
77 * @see #pathMatches(Predicate)
78 */
79 public static Predicate<Resource> pathMatches(final String pattern) {
80 return pathMatches(Predicates.containsPattern(pattern));
81 }
82
83 /**
84 * Matches if the input {@link Resource}'s handle matches the given pattern.
85 * Matches incomplete strings by default, e.g <code>/bar</code> pattern will match the <code>/foo/bar/qux</code> resource,
86 * while <code>^/bar</code> won't.
87 * @see #pathMatches(String)
88 * @see #pathMatches(Predicate)
89 */
90 public static Predicate<Resource> pathMatches(final Pattern pattern) {
91 return pathMatches(Predicates.contains(pattern));
92 }
93
94 /**
95 * @see #pathMatches(String)
96 * @see #pathMatches(Pattern)
97 */
98 public static Predicate<Resource> pathMatches(final Predicate<CharSequence> predicate) {
99 return input -> predicate.test(input.getPath());
100 }
101
102 /**
103 * @deprecated since 6.1 use {@link Resource#isDirectory()} method reference
104 */
105 @Deprecated
106 public static Predicate<Resource> isDirectory() {
107 return Resource::isDirectory;
108 }
109
110 /**
111 * @deprecated since 6.1 use {@link Resource#isFile()} method reference
112 */
113 @Deprecated
114 public static Predicate<Resource> isFile() {
115 return Resource::isFile;
116 }
117
118 /**
119 * Convenient Predicate which simply returns true, avoids verbose type parameter declaration and provides a (debatably) better name.
120 * @deprecated since 6.1 use concise lambdas with better parameter name
121 */
122 @Deprecated
123 public static Predicate<Resource> always() {
124 return resource -> true;
125 }
126
127 /**
128 * Convenient Predicate which simply returns false, avoids verbose type parameter declaration, and provides a (debatably) better name.
129 * @deprecated since 6.1 use concise lambdas with better parameter name
130 */
131 @Deprecated
132 public static Predicate<Resource> never() {
133 return resource -> false;
134 }
135
136 /**
137 * Convenient Predicate which simply returns true, avoids verbose type parameter declaration and provides a (debatably) better name.
138 *
139 * @deprecated since 6.1 use {@link ResourceOrigin#getByPath(String)} and lambdas
140 */
141 @Deprecated
142 public static Function<ResourceOrigin, Resource> getByPath(final String path) {
143 return origin -> origin.getByPath(path);
144 }
145
146 /**
147 * @deprecated since 6.1 use {@link ResourceOrigin#getRoot()} method reference
148 */
149 @Deprecated
150 public static Function<ResourceOrigin, Resource> getRoot() {
151 return ResourceOrigin::getRoot;
152 }
153
154 /**
155 * @deprecated since 6.1 use {@link Resource#getPath()} method reference
156 */
157 @Deprecated
158 public static Function<Resource, String> getPath() {
159 return Resource::getPath;
160 }
161
162 /**
163 * @deprecated since 6.1 use {@link Resource#getParent()} method reference
164 */
165 @Deprecated
166 public static Function<Resource, Resource> getParent() {
167 return Resource::getParent;
168 }
169
170 /**
171 * This Function simply collects all input into the given target collection.
172 * It adds elements to the given target collection, but it can also be retrieved with {@link CollectorFunction#getTarget()}
173 */
174 public static <T> CollectorFunction<T> collector(final Collection<T> target) {
175 return new CollectorFunction<>(target);
176 }
177
178 /**
179 * This Function simply collects all input into a new collection.
180 * The collection can be retrieved with {@link CollectorFunction#getTarget()}
181 */
182 public static <T> CollectorFunction<T> collector() {
183 return new CollectorFunction<>(new ArrayList<>());
184 }
185
186 /**
187 * @deprecated since 6.1 use static {@link Comparator#comparing(Function)} with {@link Resource#getPath()} method reference
188 */
189 @Deprecated
190 public static <R extends Resource> Comparator<R> compareByHandle() {
191 return Comparator.comparing(Resource::getPath);
192 }
193
194 /**
195 * A simple Function which aggregates arguments from all its invocations.
196 *
197 * @param <T> the argument type for the function
198 */
199 public static class CollectorFunction<T> extends VoidFunction<T> {
200 private final Collection<T> target;
201
202 private CollectorFunction(Collection<T> target) {
203 this.target = target;
204 }
205
206 @Override
207 public void doWith(T input) {
208 target.add(input);
209 }
210
211 public Collection<T> getTarget() {
212 return target;
213 }
214 }
215 }