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 */
32 public class CumulativeScopeArtifactFilter extends AbstractScopeArtifactFilter {
33
34 private Set<String> scopes;
35
36 /**
37 * Create a new filter with the specified scopes and their implied scopes enabled.
38 *
39 * @param scopes The scopes to enable, along with all implied scopes, may be {@code null}.
40 */
41 public CumulativeScopeArtifactFilter(Collection<String> scopes) {
42 this.scopes = new HashSet<>();
43
44 addScopes(scopes);
45 }
46
47 /**
48 * Creates a new filter that combines the specified filters.
49 *
50 * @param filters The filters to combine, may be {@code null}.
51 */
52 public CumulativeScopeArtifactFilter(CumulativeScopeArtifactFilter... filters) {
53 this.scopes = new HashSet<>();
54
55 if (filters != null) {
56 for (CumulativeScopeArtifactFilter filter : filters) {
57 addScopes(filter.getScopes());
58 }
59 }
60 }
61
62 private void addScopes(Collection<String> scopes) {
63 if (scopes != null) {
64 for (String scope : scopes) {
65 addScope(scope);
66 }
67 }
68 }
69
70 private void addScope(String scope) {
71 this.scopes.add(scope);
72
73 addScopeInternal(scope);
74 }
75
76 public Set<String> getScopes() {
77 return scopes;
78 }
79
80 @Override
81 public int hashCode() {
82 int hash = 17;
83
84 hash = hash * 31 + scopes.hashCode();
85
86 return hash;
87 }
88
89 @Override
90 public boolean equals(Object obj) {
91 if (this == obj) {
92 return true;
93 }
94
95 if (!(obj instanceof CumulativeScopeArtifactFilter)) {
96 return false;
97 }
98
99 CumulativeScopeArtifactFilter that = (CumulativeScopeArtifactFilter) obj;
100
101 return scopes.equals(that.scopes);
102 }
103 }