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.graph.version;
20  
21  import java.util.Iterator;
22  import java.util.Objects;
23  import java.util.function.Predicate;
24  
25  import org.eclipse.aether.artifact.Artifact;
26  import org.eclipse.aether.collection.DependencyCollectionContext;
27  import org.eclipse.aether.collection.VersionFilter;
28  import org.eclipse.aether.version.Version;
29  
30  import static java.util.Objects.requireNonNull;
31  
32  /**
33   * A version filter that excludes any version that is blacklisted.
34   *
35   * @since 2.0.0
36   */
37  public final class PredicateVersionFilter implements VersionFilter {
38      private final Predicate<Artifact> artifactPredicate;
39  
40      /**
41       * Creates a new instance of this version filter.
42       */
43      public PredicateVersionFilter(Predicate<Artifact> artifactPredicate) {
44          this.artifactPredicate = requireNonNull(artifactPredicate);
45      }
46  
47      @Override
48      public void filterVersions(VersionFilterContext context) {
49          Artifact dependencyArtifact = context.getDependency().getArtifact();
50          Iterator<Version> it = context.iterator();
51          while (it.hasNext()) {
52              Version version = it.next();
53              if (!artifactPredicate.test(dependencyArtifact.setVersion(version.toString()))) {
54                  it.remove();
55              }
56          }
57      }
58  
59      @Override
60      public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
61          return this;
62      }
63  
64      @Override
65      public boolean equals(Object o) {
66          if (this == o) {
67              return true;
68          }
69          if (o == null || getClass() != o.getClass()) {
70              return false;
71          }
72          PredicateVersionFilter that = (PredicateVersionFilter) o;
73          return Objects.equals(artifactPredicate, that.artifactPredicate);
74      }
75  
76      @Override
77      public int hashCode() {
78          return Objects.hash(artifactPredicate);
79      }
80  }