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.apache.maven.artifact.resolver.filter;
20
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Set;
24
25 /**
26 * Filter to only retain objects in the given scope or better. This implementation allows the accumulation of multiple
27 * scopes and their associated implied scopes, so that the user can filter apply a series of implication rules in a
28 * single step. This should be a more efficient implementation of multiple standard {@link ScopeArtifactFilter}
29 * instances ORed together.
30 *
31 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
32 * @author jdcasey
33 */
34 public class CumulativeScopeArtifactFilter extends AbstractScopeArtifactFilter {
35
36 private Set<String> scopes;
37
38 /**
39 * Create a new filter with the specified scopes and their implied scopes enabled.
40 *
41 * @param scopes The scopes to enable, along with all implied scopes, may be {@code null}.
42 */
43 public CumulativeScopeArtifactFilter(Collection<String> scopes) {
44 this.scopes = new HashSet<>();
45
46 addScopes(scopes);
47 }
48
49 /**
50 * Creates a new filter that combines the specified filters.
51 *
52 * @param filters The filters to combine, may be {@code null}.
53 */
54 public CumulativeScopeArtifactFilter(CumulativeScopeArtifactFilter... filters) {
55 this.scopes = new HashSet<>();
56
57 if (filters != null) {
58 for (CumulativeScopeArtifactFilter filter : filters) {
59 addScopes(filter.getScopes());
60 }
61 }
62 }
63
64 private void addScopes(Collection<String> scopes) {
65 if (scopes != null) {
66 for (String scope : scopes) {
67 addScope(scope);
68 }
69 }
70 }
71
72 private void addScope(String scope) {
73 this.scopes.add(scope);
74
75 addScopeInternal(scope);
76 }
77
78 public Set<String> getScopes() {
79 return scopes;
80 }
81
82 @Override
83 public int hashCode() {
84 int hash = 17;
85
86 hash = hash * 31 + scopes.hashCode();
87
88 return hash;
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) {
94 return true;
95 }
96
97 if (!(obj instanceof CumulativeScopeArtifactFilter)) {
98 return false;
99 }
100
101 CumulativeScopeArtifactFilter that = (CumulativeScopeArtifactFilter) obj;
102
103 return scopes.equals(that.scopes);
104 }
105 }