This is an automated email from the ASF dual-hosted git repository. khmarbaise pushed a commit to branch MDEP-832 in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit 9260e2a99b5f19d976df189ec468d119cd83af59 Author: Karl Heinz Marbaise <khmarba...@apache.org> AuthorDate: Thu Oct 20 13:52:08 2022 +0200 [MDEP-832] - Remove commons-collections-4 --- pom.xml | 12 +-- .../dependency/analyze/AnalyzeDuplicateMojo.java | 15 +-- .../maven/plugins/dependency/analyze/Util.java | 58 +++++++++++ .../maven/plugins/dependency/analyze/UtilTest.java | 113 +++++++++++++++++++++ 4 files changed, 182 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index b3de43a3..e84a19fb 100644 --- a/pom.xml +++ b/pom.xml @@ -222,12 +222,6 @@ under the License. <version>3.12.0</version> </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-collections4</artifactId> - <version>4.2</version> - </dependency> - <!-- dependencies to annotations --> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> @@ -273,6 +267,12 @@ under the License. <version>4.13.2</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>3.23.1</version> + <scope>test</scope> + </dependency> <dependency> <groupId>org.apache.maven.plugin-testing</groupId> <artifactId>maven-plugin-testing-harness</artifactId> diff --git a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java index 552f4981..a706743a 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/analyze/AnalyzeDuplicateMojo.java @@ -27,8 +27,8 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; -import org.apache.commons.collections4.CollectionUtils; import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; @@ -151,15 +151,10 @@ public class AnalyzeDuplicateMojo private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies ) { - List<String> modelDependencies2 = new ArrayList<>(); - for ( Dependency dep : modelDependencies ) - { - modelDependencies2.add( dep.getManagementKey() ); - } - - // @formatter:off + List<String> modelDependencies2 = + modelDependencies.stream().map( Dependency::getManagementKey ).collect( Collectors.toCollection( + ArrayList::new ) ); return new LinkedHashSet<>( - CollectionUtils.disjunction( modelDependencies2, new LinkedHashSet<>( modelDependencies2 ) ) ); - // @formatter:on + Util.symmetricDifference( modelDependencies2, new LinkedHashSet<>( modelDependencies2 ) ) ); } } diff --git a/src/main/java/org/apache/maven/plugins/dependency/analyze/Util.java b/src/main/java/org/apache/maven/plugins/dependency/analyze/Util.java new file mode 100644 index 00000000..84a1e0fc --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/dependency/analyze/Util.java @@ -0,0 +1,58 @@ +package org.apache.maven.plugins.dependency.analyze; + +/* + * 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. + */ + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.summingInt; +import static java.util.stream.Collectors.toList; + +class Util +{ + + static <O> Collection<O> symmetricDifference( Collection<O> elementsOne, + Collection<O> elementsTwo ) + { + Set<O> hashSet = new HashSet<>( elementsOne ); + hashSet.addAll( elementsTwo ); + + Map<O, Integer> cardinalityOne = + elementsOne.stream().collect( groupingBy( identity(), summingInt( e -> 1 ) ) ); + Map<O, Integer> cardinalityTwo = + elementsTwo.stream().collect( groupingBy( identity(), summingInt( e -> 1 ) ) ); + + return hashSet.stream().flatMap( item -> + { + int cardOne = cardinalityOne.getOrDefault( item, 0 ); + int cardTwo = cardinalityTwo.getOrDefault( item, 0 ); + int max = Math.max( cardOne, cardTwo ); + int min = Math.min( cardOne, cardTwo ); + return Collections.nCopies( max - min, item ).stream(); + } ).collect( collectingAndThen( toList(), Collections::unmodifiableList ) ); + } + +} diff --git a/src/test/java/org/apache/maven/plugins/dependency/analyze/UtilTest.java b/src/test/java/org/apache/maven/plugins/dependency/analyze/UtilTest.java new file mode 100644 index 00000000..c28956af --- /dev/null +++ b/src/test/java/org/apache/maven/plugins/dependency/analyze/UtilTest.java @@ -0,0 +1,113 @@ +package org.apache.maven.plugins.dependency.analyze; + +/* + * 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. + */ + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UtilTest +{ + + @Test + public void two_lists_one_with_duplicates() + { + List<String> s1 = new ArrayList<>(); + s1.add( "1" ); + s1.add( "2" ); + s1.add( "2" ); + s1.add( "3" ); + List<String> s2 = new ArrayList<>(); + s2.add( "3" ); + s2.add( "4" ); + Collection<String> result = Util.symmetricDifference( s1, s2 ); + + assertThat( result ).containsExactly( "1", "2", "2", "4" ); + } + + @Test + public void two_different_sets_with_commons_elements() + { + Set<String> s1 = new HashSet<>(); + s1.add( "1" ); + s1.add( "2" ); + s1.add( "3" ); + s1.add( "4" ); + s1.add( "5" ); + Set<String> s2 = new HashSet<>(); + s2.add( "2" ); + s2.add( "4" ); + s2.add( "6" ); + Collection<String> result = Util.symmetricDifference( s1, s2 ); + + assertThat( result ).containsExactly( "1", "3", "5", "6" ); + } + + @Test + public void second_set_only() + { + Set<String> s1 = new HashSet<>(); + s1.add( "1" ); + s1.add( "2" ); + s1.add( "3" ); + s1.add( "4" ); + s1.add( "5" ); + Set<String> s2 = new HashSet<>(); + s2.add( "3" ); + s2.add( "5" ); + Collection<String> result = Util.symmetricDifference( s1, s2 ); + + assertThat( result ).containsExactly( "1", "2", "4" ); + } + + @Test + public void list_with_duplicate() + { + List<String> s1 = new ArrayList<>(); + s1.add( "1" ); + s1.add( "2" ); + s1.add( "2" ); + + Collection<String> result = Util.symmetricDifference( s1, new LinkedHashSet<>( s1 ) ); + + assertThat( result ).containsExactly( "2" ); + } + + @Test + public void list_with_tripple_entries() + { + List<String> s1 = new ArrayList<>(); + s1.add( "1" ); + s1.add( "2" ); + s1.add( "2" ); + s1.add( "2" ); + + Collection<String> result = Util.symmetricDifference( s1, new LinkedHashSet<>( s1 ) ); + + assertThat( result ).containsExactly( "2", "2" ); + } +}