001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.filter;
020
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashSet;
025
026import org.eclipse.aether.graph.DependencyFilter;
027import org.eclipse.aether.util.artifact.JavaScopes;
028
029/**
030 * A utility class assisting in the creation of dependency node filters.
031 */
032public final class DependencyFilterUtils {
033
034    private DependencyFilterUtils() {
035        // hide constructor
036    }
037
038    /**
039     * Creates a new filter that negates the specified filter.
040     *
041     * @param filter the filter to negate, must not be {@code null}
042     * @return the new filter, never {@code null}
043     */
044    public static DependencyFilter notFilter(DependencyFilter filter) {
045        return new NotDependencyFilter(filter);
046    }
047
048    /**
049     * Creates a new filter that combines the specified filters using a logical {@code AND}. If no filters are
050     * specified, the resulting filter accepts everything.
051     *
052     * @param filters the filters to combine, may be {@code null}
053     * @return the new filter, never {@code null}
054     */
055    public static DependencyFilter andFilter(DependencyFilter... filters) {
056        if (filters != null && filters.length == 1) {
057            return filters[0];
058        } else {
059            return new AndDependencyFilter(filters);
060        }
061    }
062
063    /**
064     * Creates a new filter that combines the specified filters using a logical {@code AND}. If no filters are
065     * specified, the resulting filter accepts everything.
066     *
067     * @param filters the filters to combine, may be {@code null}
068     * @return the new filter, never {@code null}
069     */
070    public static DependencyFilter andFilter(Collection<DependencyFilter> filters) {
071        if (filters != null && filters.size() == 1) {
072            return filters.iterator().next();
073        } else {
074            return new AndDependencyFilter(filters);
075        }
076    }
077
078    /**
079     * Creates a new filter that combines the specified filters using a logical {@code OR}. If no filters are specified,
080     * the resulting filter accepts nothing.
081     *
082     * @param filters the filters to combine, may be {@code null}
083     * @return the new filter, never {@code null}
084     */
085    public static DependencyFilter orFilter(DependencyFilter... filters) {
086        if (filters != null && filters.length == 1) {
087            return filters[0];
088        } else {
089            return new OrDependencyFilter(filters);
090        }
091    }
092
093    /**
094     * Creates a new filter that combines the specified filters using a logical {@code OR}. If no filters are specified,
095     * the resulting filter accepts nothing.
096     *
097     * @param filters the filters to combine, may be {@code null}
098     * @return the new filter, never {@code null}
099     */
100    public static DependencyFilter orFilter(Collection<DependencyFilter> filters) {
101        if (filters != null && filters.size() == 1) {
102            return filters.iterator().next();
103        } else {
104            return new OrDependencyFilter(filters);
105        }
106    }
107
108    /**
109     * Creates a new filter that selects dependencies whose scope matches one or more of the specified classpath types.
110     * A classpath type is a set of scopes separated by either {@code ','} or {@code '+'}.
111     *
112     * @param classpathTypes the classpath types, may be {@code null} or empty to match no dependency
113     * @return the new filter, never {@code null}
114     * @see JavaScopes
115     * @deprecated resolver is oblivious about "scopes", it is consumer project which needs to lay these down and
116     * also assign proper semantics. Moreover, Resolver is oblivious about notions of "classpath", "modulepath", and
117     * any other similar uses. These should be handled by consumer project.
118     */
119    @Deprecated
120    public static DependencyFilter classpathFilter(String... classpathTypes) {
121        return classpathFilter((classpathTypes != null) ? Arrays.asList(classpathTypes) : null);
122    }
123
124    /**
125     * Creates a new filter that selects dependencies whose scope matches one or more of the specified classpath types.
126     * A classpath type is a set of scopes separated by either {@code ','} or {@code '+'}.
127     *
128     * @param classpathTypes the classpath types, may be {@code null} or empty to match no dependency
129     * @return the new filter, never {@code null}
130     * @see JavaScopes
131     * @deprecated resolver is oblivious about "scopes", it is consumer project which needs to lay these down and
132     * also assign proper semantics. Moreover, Resolver is oblivious about notions of "classpath", "modulepath", and
133     * any other similar uses. These should be handled by consumer project.
134     */
135    @Deprecated
136    public static DependencyFilter classpathFilter(Collection<String> classpathTypes) {
137        Collection<String> types = new HashSet<>();
138
139        if (classpathTypes != null) {
140            for (String classpathType : classpathTypes) {
141                String[] tokens = classpathType.split("[+,]");
142                for (String token : tokens) {
143                    token = token.trim();
144                    if (!token.isEmpty()) {
145                        types.add(token);
146                    }
147                }
148            }
149        }
150
151        Collection<String> included = new HashSet<>();
152        for (String type : types) {
153            if (JavaScopes.COMPILE.equals(type)) {
154                Collections.addAll(included, JavaScopes.COMPILE, JavaScopes.PROVIDED, JavaScopes.SYSTEM);
155            } else if (JavaScopes.RUNTIME.equals(type)) {
156                Collections.addAll(included, JavaScopes.COMPILE, JavaScopes.RUNTIME);
157            } else if (JavaScopes.TEST.equals(type)) {
158                Collections.addAll(
159                        included,
160                        JavaScopes.COMPILE,
161                        JavaScopes.PROVIDED,
162                        JavaScopes.SYSTEM,
163                        JavaScopes.RUNTIME,
164                        JavaScopes.TEST);
165            } else {
166                included.add(type);
167            }
168        }
169
170        Collection<String> excluded = new HashSet<>();
171        Collections.addAll(
172                excluded,
173                JavaScopes.COMPILE,
174                JavaScopes.PROVIDED,
175                JavaScopes.SYSTEM,
176                JavaScopes.RUNTIME,
177                JavaScopes.TEST);
178        excluded.removeAll(included);
179
180        return new ScopeDependencyFilter(null, excluded);
181    }
182}