View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.filter;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  
26  import org.eclipse.aether.graph.DependencyFilter;
27  import org.eclipse.aether.util.artifact.JavaScopes;
28  
29  /**
30   * A utility class assisting in the creation of dependency node filters.
31   */
32  public final class DependencyFilterUtils {
33  
34      private DependencyFilterUtils() {
35          // hide constructor
36      }
37  
38      /**
39       * Creates a new filter that negates the specified filter.
40       *
41       * @param filter The filter to negate, must not be {@code null}.
42       * @return The new filter, never {@code null}.
43       */
44      public static DependencyFilter notFilter(DependencyFilter filter) {
45          return new NotDependencyFilter(filter);
46      }
47  
48      /**
49       * Creates a new filter that combines the specified filters using a logical {@code AND}. If no filters are
50       * specified, the resulting filter accepts everything.
51       *
52       * @param filters The filters to combine, may be {@code null}.
53       * @return The new filter, never {@code null}.
54       */
55      public static DependencyFilter andFilter(DependencyFilter... filters) {
56          if (filters != null && filters.length == 1) {
57              return filters[0];
58          } else {
59              return new AndDependencyFilter(filters);
60          }
61      }
62  
63      /**
64       * Creates a new filter that combines the specified filters using a logical {@code AND}. If no filters are
65       * specified, the resulting filter accepts everything.
66       *
67       * @param filters The filters to combine, may be {@code null}.
68       * @return The new filter, never {@code null}.
69       */
70      public static DependencyFilter andFilter(Collection<DependencyFilter> filters) {
71          if (filters != null && filters.size() == 1) {
72              return filters.iterator().next();
73          } else {
74              return new AndDependencyFilter(filters);
75          }
76      }
77  
78      /**
79       * Creates a new filter that combines the specified filters using a logical {@code OR}. If no filters are specified,
80       * the resulting filter accepts nothing.
81       *
82       * @param filters The filters to combine, may be {@code null}.
83       * @return The new filter, never {@code null}.
84       */
85      public static DependencyFilter orFilter(DependencyFilter... filters) {
86          if (filters != null && filters.length == 1) {
87              return filters[0];
88          } else {
89              return new OrDependencyFilter(filters);
90          }
91      }
92  
93      /**
94       * Creates a new filter that combines the specified filters using a logical {@code OR}. If no filters are specified,
95       * the resulting filter accepts nothing.
96       *
97       * @param filters The filters to combine, may be {@code null}.
98       * @return The new filter, never {@code null}.
99       */
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      *
116      * @deprecated Resolver is oblivious about "scopes", it is consumer project which needs to lay these down and
117      * also assign proper semantics. Moreover, Resolver is oblivious about notions of "classpath", "modulepath", and
118      * any other similar uses. These should be handled by consumer project.
119      */
120     @Deprecated
121     public static DependencyFilter classpathFilter(String... classpathTypes) {
122         return classpathFilter((classpathTypes != null) ? Arrays.asList(classpathTypes) : null);
123     }
124 
125     /**
126      * Creates a new filter that selects dependencies whose scope matches one or more of the specified classpath types.
127      * A classpath type is a set of scopes separated by either {@code ','} or {@code '+'}.
128      *
129      * @param classpathTypes The classpath types, may be {@code null} or empty to match no dependency.
130      * @return The new filter, never {@code null}.
131      * @see JavaScopes
132      *
133      * @deprecated Resolver is oblivious about "scopes", it is consumer project which needs to lay these down and
134      * also assign proper semantics. Moreover, Resolver is oblivious about notions of "classpath", "modulepath", and
135      * any other similar uses. These should be handled by consumer project.
136      */
137     @Deprecated
138     public static DependencyFilter classpathFilter(Collection<String> classpathTypes) {
139         Collection<String> types = new HashSet<>();
140 
141         if (classpathTypes != null) {
142             for (String classpathType : classpathTypes) {
143                 String[] tokens = classpathType.split("[+,]");
144                 for (String token : tokens) {
145                     token = token.trim();
146                     if (!token.isEmpty()) {
147                         types.add(token);
148                     }
149                 }
150             }
151         }
152 
153         Collection<String> included = new HashSet<>();
154         for (String type : types) {
155             if (JavaScopes.COMPILE.equals(type)) {
156                 Collections.addAll(included, JavaScopes.COMPILE, JavaScopes.PROVIDED, JavaScopes.SYSTEM);
157             } else if (JavaScopes.RUNTIME.equals(type)) {
158                 Collections.addAll(included, JavaScopes.COMPILE, JavaScopes.RUNTIME);
159             } else if (JavaScopes.TEST.equals(type)) {
160                 Collections.addAll(
161                         included,
162                         JavaScopes.COMPILE,
163                         JavaScopes.PROVIDED,
164                         JavaScopes.SYSTEM,
165                         JavaScopes.RUNTIME,
166                         JavaScopes.TEST);
167             } else {
168                 included.add(type);
169             }
170         }
171 
172         Collection<String> excluded = new HashSet<>();
173         Collections.addAll(
174                 excluded,
175                 JavaScopes.COMPILE,
176                 JavaScopes.PROVIDED,
177                 JavaScopes.SYSTEM,
178                 JavaScopes.RUNTIME,
179                 JavaScopes.TEST);
180         excluded.removeAll(included);
181 
182         return new ScopeDependencyFilter(null, excluded);
183     }
184 }