Skip to content

Commit d9c6948

Browse files
authored
Add more filters; open classes up (#1516)
Changes: * generally remove `final` kw; open up classes for reuse * add `as` as contextual ancestor snapshot version filter * introduce version predicate filter * redo `ns` and `rs` filters based on it
1 parent fa0f5f0 commit d9c6948

File tree

10 files changed

+239
-45
lines changed

10 files changed

+239
-45
lines changed

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/version/ChainedVersionFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* A version filter that combines multiple version filters into a chain where each filter gets invoked one after the
3030
* other, thereby accumulating their filtering effects.
3131
*/
32-
public final class ChainedVersionFilter implements VersionFilter {
32+
public class ChainedVersionFilter implements VersionFilter {
3333

3434
private final VersionFilter[] filters;
3535

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 org.eclipse.aether.artifact.Artifact;
22+
import org.eclipse.aether.collection.DependencyCollectionContext;
23+
import org.eclipse.aether.collection.VersionFilter;
24+
25+
/**
26+
* A version filter that blocks "*-SNAPSHOT" versions if the
27+
* {@link VersionFilterContext#getDependency()} ancestor whose range is being filtered is not a snapshot.
28+
*/
29+
public class ContextualAncestorSnapshotVersionFilter implements VersionFilter {
30+
private final SnapshotVersionFilter filter;
31+
32+
/**
33+
* Creates a new instance of this version filter.
34+
*/
35+
public ContextualAncestorSnapshotVersionFilter() {
36+
filter = new SnapshotVersionFilter();
37+
}
38+
39+
@Override
40+
public void filterVersions(VersionFilterContext context) {
41+
Artifact ancestor = context.getDependency().getArtifact();
42+
if (!ancestor.isSnapshot()) {
43+
filter.filterVersions(context);
44+
}
45+
}
46+
47+
@Override
48+
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
49+
return this;
50+
}
51+
52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
} else if (null == obj || !getClass().equals(obj.getClass())) {
57+
return false;
58+
}
59+
return true;
60+
}
61+
62+
@Override
63+
public int hashCode() {
64+
return getClass().hashCode();
65+
}
66+
}

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/version/ContextualSnapshotVersionFilter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
/**
2929
* A version filter that blocks "*-SNAPSHOT" versions if the
3030
* {@link org.eclipse.aether.collection.CollectRequest#getRootArtifact() root artifact} of the dependency graph is not a
31-
* snapshot. Alternatively, this filter can be forced to always ban snapshot versions by setting the boolean
32-
* {@link RepositorySystemSession#getConfigProperties() configuration property} {@link #CONFIG_PROP_ENABLE} to
33-
* {@code true}.
31+
* snapshot.
3432
*/
35-
public final class ContextualSnapshotVersionFilter implements VersionFilter {
33+
public class ContextualSnapshotVersionFilter implements VersionFilter {
3634
/**
3735
* The key in the repository session's {@link RepositorySystemSession#getConfigProperties() configuration
3836
* properties} used to store a {@link Boolean} flag whether this filter should be forced to ban snapshots. By
@@ -41,7 +39,9 @@ public final class ContextualSnapshotVersionFilter implements VersionFilter {
4139
* @configurationSource {@link RepositorySystemSession#getConfigProperties()}
4240
* @configurationType {@link java.lang.Boolean}
4341
* @configurationDefaultValue false
42+
* @deprecated Use snapshot filter instead to always ban snapshots.
4443
*/
44+
@Deprecated
4545
public static final String CONFIG_PROP_ENABLE = ConfigurationProperties.PREFIX_AETHER + "snapshotFilter";
4646

4747
private final SnapshotVersionFilter filter;

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/version/HighestVersionFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* A version filter that excludes any version except the highest one.
2929
*/
30-
public final class HighestVersionFilter implements VersionFilter {
30+
public class HighestVersionFilter implements VersionFilter {
3131
private final int count;
3232

3333
/**

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/version/LowestVersionFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @since 2.0.0
3131
*/
32-
public final class LowestVersionFilter implements VersionFilter {
32+
public class LowestVersionFilter implements VersionFilter {
3333
private final int count;
3434

3535
/**

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/version/PredicateVersionFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @since 2.0.0
3636
*/
37-
public final class PredicateVersionFilter implements VersionFilter {
37+
public class PredicateVersionFilter implements VersionFilter {
3838
private final Predicate<Artifact> artifactPredicate;
3939

4040
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/**
22+
* A version filter that (unconditionally) blocks non "*-SNAPSHOT" versions.
23+
*/
24+
public class ReleaseVersionFilter extends VersionPredicateVersionFilter {
25+
26+
/**
27+
* Creates a new instance of this version filter.
28+
*/
29+
public ReleaseVersionFilter() {
30+
super(v -> v.toString().endsWith("SNAPSHOT"));
31+
}
32+
}

maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/version/SnapshotVersionFilter.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,16 @@
1818
*/
1919
package org.eclipse.aether.util.graph.version;
2020

21-
import java.util.Iterator;
22-
23-
import org.eclipse.aether.collection.DependencyCollectionContext;
24-
import org.eclipse.aether.collection.VersionFilter;
25-
import org.eclipse.aether.version.Version;
26-
2721
/**
2822
* A version filter that (unconditionally) blocks "*-SNAPSHOT" versions. For practical purposes,
2923
* {@link ContextualSnapshotVersionFilter} is usually more desirable.
3024
*/
31-
public final class SnapshotVersionFilter implements VersionFilter {
25+
public class SnapshotVersionFilter extends VersionPredicateVersionFilter {
3226

3327
/**
3428
* Creates a new instance of this version filter.
3529
*/
36-
public SnapshotVersionFilter() {}
37-
38-
@Override
39-
public void filterVersions(VersionFilterContext context) {
40-
for (Iterator<Version> it = context.iterator(); it.hasNext(); ) {
41-
String version = it.next().toString();
42-
if (version.endsWith("SNAPSHOT")) {
43-
it.remove();
44-
}
45-
}
46-
}
47-
48-
@Override
49-
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
50-
return this;
51-
}
52-
53-
@Override
54-
public boolean equals(Object obj) {
55-
if (this == obj) {
56-
return true;
57-
} else if (null == obj || !getClass().equals(obj.getClass())) {
58-
return false;
59-
}
60-
return true;
61-
}
62-
63-
@Override
64-
public int hashCode() {
65-
return getClass().hashCode();
30+
public SnapshotVersionFilter() {
31+
super(v -> !v.toString().endsWith("SNAPSHOT"));
6632
}
6733
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.collection.DependencyCollectionContext;
26+
import org.eclipse.aether.collection.VersionFilter;
27+
import org.eclipse.aether.version.Version;
28+
29+
import static java.util.Objects.requireNonNull;
30+
31+
/**
32+
* A version filter that excludes any version that is blacklisted.
33+
*
34+
* @since 2.0.11
35+
*/
36+
public class VersionPredicateVersionFilter implements VersionFilter {
37+
private final Predicate<Version> versionPredicate;
38+
39+
/**
40+
* Creates a new instance of this version filter. It will filter out versions not matched by predicate.
41+
* Note: filter always operates with baseVersions.
42+
*/
43+
public VersionPredicateVersionFilter(Predicate<Version> versionPredicate) {
44+
this.versionPredicate = requireNonNull(versionPredicate);
45+
}
46+
47+
@Override
48+
public void filterVersions(VersionFilterContext context) {
49+
for (Iterator<Version> it = context.iterator(); it.hasNext(); ) {
50+
if (!versionPredicate.test(it.next())) {
51+
it.remove();
52+
}
53+
}
54+
}
55+
56+
@Override
57+
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
58+
return this;
59+
}
60+
61+
@Override
62+
public boolean equals(Object o) {
63+
if (this == o) {
64+
return true;
65+
}
66+
if (o == null || getClass() != o.getClass()) {
67+
return false;
68+
}
69+
VersionPredicateVersionFilter that = (VersionPredicateVersionFilter) o;
70+
return Objects.equals(versionPredicate, that.versionPredicate);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return getClass().hashCode();
76+
}
77+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.versions;
20+
21+
import org.eclipse.aether.collection.VersionFilter.VersionFilterContext;
22+
import org.eclipse.aether.util.graph.version.ReleaseVersionFilter;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
27+
import static org.junit.jupiter.api.Assertions.assertSame;
28+
29+
public class ReleaseVersionFilterTest extends AbstractVersionFilterTest {
30+
31+
@Test
32+
void testFilterVersions() {
33+
ReleaseVersionFilter filter = new ReleaseVersionFilter();
34+
VersionFilterContext ctx = newContext("g:a:[1,9]", "1", "2-SNAPSHOT", "3.1", "4.0-SNAPSHOT", "5.0.0");
35+
filter.filterVersions(ctx);
36+
assertVersions(ctx, "2-SNAPSHOT", "4.0-SNAPSHOT");
37+
}
38+
39+
@Test
40+
void testDeriveChildFilter() {
41+
ReleaseVersionFilter filter = new ReleaseVersionFilter();
42+
assertSame(filter, derive(filter, "g:a:1"));
43+
}
44+
45+
@SuppressWarnings("EqualsWithItself")
46+
@Test
47+
void testEquals() {
48+
ReleaseVersionFilter filter = new ReleaseVersionFilter();
49+
assertNotEquals(null, filter);
50+
assertEquals(filter, filter);
51+
assertEquals(filter, new ReleaseVersionFilter());
52+
}
53+
}

0 commit comments

Comments
 (0)