Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ private ChainedVersionFilter(VersionFilter[] filters) {
this.filters = filters;
}

@Override
public void filterVersions(VersionFilterContext context) throws RepositoryException {
for (int i = 0, n = filters.length; i < n && context.getCount() > 0; i++) {
filters[i].filterVersions(context);
}
}

@Override
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
VersionFilter[] children = null;
int removed = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ private boolean isEnabled(RepositorySystemSession session) {
return ConfigUtils.getBoolean(session, false, CONFIG_PROP_ENABLE);
}

@Override
public void filterVersions(VersionFilterContext context) {
if (isEnabled(context.getSession())) {
filter.filterVersions(context);
}
}

@Override
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
if (!isEnabled(context.getSession())) {
Artifact artifact = context.getArtifact();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class HighestVersionFilter implements VersionFilter {
*/
public HighestVersionFilter() {}

@Override
public void filterVersions(VersionFilterContext context) {
Iterator<Version> it = context.iterator();
for (boolean hasNext = it.hasNext(); hasNext; ) {
Expand All @@ -45,6 +46,7 @@ public void filterVersions(VersionFilterContext context) {
}
}

@Override
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.util.graph.version;

import java.util.Iterator;

import org.eclipse.aether.collection.DependencyCollectionContext;
import org.eclipse.aether.collection.VersionFilter;
import org.eclipse.aether.version.Version;

/**
* A version filter that excludes any version except the lowest one.
*
* @since 2.0.0
*/
public final class LowestVersionFilter implements VersionFilter {

/**
* Creates a new instance of this version filter.
*/
public LowestVersionFilter() {}

@Override
public void filterVersions(VersionFilterContext context) {
Iterator<Version> it = context.iterator();
if (it.hasNext()) {
it.next();
while (it.hasNext()) {
it.next();
it.remove();
}
}
}

@Override
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
return this;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (null == obj || !getClass().equals(obj.getClass())) {
return false;
}
return true;
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.util.graph.version;

import java.util.Iterator;
import java.util.Objects;
import java.util.function.Predicate;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.collection.DependencyCollectionContext;
import org.eclipse.aether.collection.VersionFilter;
import org.eclipse.aether.version.Version;

import static java.util.Objects.requireNonNull;

/**
* A version filter that excludes any version that is blacklisted.
*
* @since 2.0.0
*/
public final class PredicateVersionFilter implements VersionFilter {
private final Predicate<Artifact> artifactPredicate;

/**
* Creates a new instance of this version filter.
*/
public PredicateVersionFilter(Predicate<Artifact> artifactPredicate) {
this.artifactPredicate = requireNonNull(artifactPredicate);
}

@Override
public void filterVersions(VersionFilterContext context) {
Artifact dependencyArtifact = context.getDependency().getArtifact();
Iterator<Version> it = context.iterator();
while (it.hasNext()) {
Version version = it.next();
if (!artifactPredicate.test(dependencyArtifact.setVersion(version.toString()))) {
it.remove();
}
}
}

@Override
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PredicateVersionFilter that = (PredicateVersionFilter) o;
return Objects.equals(artifactPredicate, that.artifactPredicate);
}

@Override
public int hashCode() {
return Objects.hash(artifactPredicate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class SnapshotVersionFilter implements VersionFilter {
*/
public SnapshotVersionFilter() {}

@Override
public void filterVersions(VersionFilterContext context) {
for (Iterator<Version> it = context.iterator(); it.hasNext(); ) {
String version = it.next().toString();
Expand All @@ -44,6 +45,7 @@ public void filterVersions(VersionFilterContext context) {
}
}

@Override
public VersionFilter deriveChildFilter(DependencyCollectionContext context) {
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.util.graph.versions;

import org.eclipse.aether.collection.VersionFilter.VersionFilterContext;
import org.eclipse.aether.util.graph.version.LowestVersionFilter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class LowestVersionFilterTest extends AbstractVersionFilterTest {

@Test
void testFilterVersions() {
LowestVersionFilter filter = new LowestVersionFilter();
VersionFilterContext ctx = newContext("g:a:[1,9]", "1", "2", "3", "4", "5", "6", "7", "8", "9");
filter.filterVersions(ctx);
assertVersions(ctx, "1");
}

@Test
void testDeriveChildFilter() {
LowestVersionFilter filter = new LowestVersionFilter();
assertSame(filter, derive(filter, "g:a:1"));
}

@SuppressWarnings("EqualsWithItself")
@Test
void testEquals() {
LowestVersionFilter filter = new LowestVersionFilter();
assertNotEquals(null, filter);
assertEquals(filter, filter);
assertEquals(filter, new LowestVersionFilter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.aether.util.graph.versions;

import java.util.function.Predicate;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.collection.VersionFilter.VersionFilterContext;
import org.eclipse.aether.util.graph.version.PredicateVersionFilter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class PredicateVersionFilterTest extends AbstractVersionFilterTest {

@Test
void testFilterVersions() {
Predicate<Artifact> oddVersions = a -> Integer.parseInt(a.getVersion()) % 2 != 0;
PredicateVersionFilter filter = new PredicateVersionFilter(oddVersions);
VersionFilterContext ctx = newContext("g:a:[1,9]", "1", "2", "3", "4", "5", "6", "7", "8", "9");
filter.filterVersions(ctx);
assertVersions(ctx, "1", "3", "5", "7", "9");
}

@Test
void testDeriveChildFilter() {
PredicateVersionFilter filter = new PredicateVersionFilter(a -> true);
assertSame(filter, derive(filter, "g:a:1"));
}

@Test
void testEquals() {
Predicate<Artifact> falsePredicate = a -> false;
Predicate<Artifact> truePredicate = a -> true;
PredicateVersionFilter filter1 = new PredicateVersionFilter(falsePredicate);
PredicateVersionFilter filter2 = new PredicateVersionFilter(falsePredicate);
PredicateVersionFilter filter3 = new PredicateVersionFilter(truePredicate);
assertNotEquals(null, filter1);
assertEquals(filter1, filter2);
assertNotEquals(filter2, filter3);
assertEquals(filter1, new PredicateVersionFilter(falsePredicate));
}
}