1 /**
2 * This file Copyright (c) 2015 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
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.Comparator;
42 import java.util.regex.Pattern;
43
44 import com.google.common.base.Function;
45 import com.google.common.base.Predicate;
46 import com.google.common.base.Predicates;
47
48 /**
49 * 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.
50 * They are used to implement {@link info.magnolia.resourceloader.layered.LayeredResourceOrigin}, but could be reused for filtering and other purposes.
51 *
52 * // TODO consistent naming -- handle or path
53 * 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.
54 */
55 public class Functions {
56 public static Predicate<ResourceOrigin> hasPath(final String path) {
57 return new Predicate<ResourceOrigin>() {
58 @Override
59 public boolean apply(ResourceOrigin input) {
60 return input.hasPath(path);
61 }
62 };
63 }
64
65 public static Predicate<Resource> pathEquals(final CharSequence path) {
66 return pathMatches(Predicates.equalTo(path));
67 }
68
69 public static Predicate<Resource> pathStartsWith(final CharSequence pathPrefix) {
70 return pathMatches("^" + pathPrefix);
71 }
72
73 /**
74 * @see #pathMatches(Pattern)
75 * @see #pathMatches(Predicate)
76 */
77 public static Predicate<Resource> pathMatches(final String pattern) {
78 return pathMatches(Pattern.compile(pattern));
79 }
80
81 /**
82 * Matches if the input {@link Resource}'s handle matches the given pattern.
83 * Matches incomplete strings by default, e.g <code>/bar</code> pattern will match the <code>/foo/bar/qux</code> resource,
84 * while <code>^/bar</code> won't.
85 * @see #pathMatches(String)
86 * @see #pathMatches(Predicate)
87 */
88 public static Predicate<Resource> pathMatches(final Pattern pattern) {
89 return pathMatches(Predicates.contains(pattern));
90 }
91
92 /**
93 * @see #pathMatches(String)
94 * @see #pathMatches(Pattern)
95 */
96 public static Predicate<Resource> pathMatches(final Predicate<CharSequence> predicate) {
97 return new Predicate<Resource>() {
98 @Override
99 public boolean apply(Resource input) {
100 return predicate.apply(input.getPath());
101 }
102 };
103 }
104
105 public static Predicate<Resource> isDirectory() {
106 return new Predicate<Resource>() {
107 @Override
108 public boolean apply(Resource input) {
109 return input.isDirectory();
110 }
111 };
112 }
113
114 public static Predicate<Resource> isFile() {
115 return new Predicate<Resource>() {
116 @Override
117 public boolean apply(Resource input) {
118 return input.isFile();
119 }
120 };
121 }
122
123 /**
124 * Convenient Predicate which simply delegates to {@link Predicates#alwaysTrue()}, avoids verbose type parameter declaration and provides a (debatably) better name.
125 */
126 public static Predicate<Resource> always() {
127 return Predicates.alwaysTrue();
128 }
129
130 /**
131 * Convenient Predicate which simply delegates to {@link Predicates#alwaysFalse()}, avoids verbose type parameter declaration, and provides a (debatably) better name.
132 */
133 public static Predicate<Resource> never() {
134 return Predicates.alwaysFalse();
135 }
136
137 public static Function<ResourceOrigin, Resource> getByPath(final String path) {
138 return new Function<ResourceOrigin, Resource>() {
139 @Override
140 public Resource apply(ResourceOrigin origin) {
141 return origin.getByPath(path);
142 }
143 };
144 }
145
146 public static Function<ResourceOrigin, Resource> getRoot() {
147 return new Function<ResourceOrigin, Resource>() {
148 @Override
149 public Resource apply(ResourceOrigin origin) {
150 return origin.getRoot();
151 }
152 };
153 }
154
155 public static Function<Resource, String> getPath() {
156 return new Function<Resource, String>() {
157 @Override
158 public String apply(Resource input) {
159 return input.getPath();
160 }
161 };
162 }
163
164 public static Function<Resource, Resource> getParent() {
165 return new Function<Resource, Resource>() {
166 @Override
167 public Resource apply(Resource input) {
168 return input.getParent();
169 }
170 };
171 }
172
173 /**
174 * This Function simply collects all input into the given target collection.
175 * It adds elements to the given target collection, but it can also be retrieved with {@link CollectorFunction#getTarget()}
176 */
177 public static <T> CollectorFunction<T> collector(final Collection<T> target) {
178 return new CollectorFunction<>(target);
179 }
180
181 /**
182 * This Function simply collects all input into a new collection.
183 * The collection can be retrieved with {@link CollectorFunction#getTarget()}
184 */
185 public static <T> CollectorFunction<T> collector() {
186 return new CollectorFunction<>(new ArrayList<T>());
187 }
188
189 public static <R extends Resource> Comparator<R> compareByHandle() {
190 return new Comparator<R>() {
191 @Override
192 public int compare(R o1, R o2) {
193 return o1.getPath().compareTo(o2.getPath());
194 }
195 };
196 }
197
198 /**
199 * A simple Function which aggregates arguments from all its invocations.
200 */
201 public static class CollectorFunction<T> extends VoidFunction<T> {
202 private final Collection<T> target;
203
204 private CollectorFunction(Collection<T> target) {
205 this.target = target;
206 }
207
208 @Override
209 public void doWith(T input) {
210 target.add(input);
211 }
212
213 public Collection<T> getTarget() {
214 return target;
215 }
216 }
217 }