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