Skip to content

Commit

Permalink
[MDEP-832] - Remove commons-collections-4
Browse files Browse the repository at this point in the history
  • Loading branch information
khmarbaise committed Oct 20, 2022
1 parent 078f6aa commit d94f9bb
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 17 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
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;
Expand Down Expand Up @@ -151,15 +150,9 @@ private void createMessage( Set<String> duplicateDependencies, StringBuilder sb,

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.toList() );
return new LinkedHashSet<>(
CollectionUtils.disjunction( modelDependencies2, new LinkedHashSet<>( modelDependencies2 ) ) );
// @formatter:on
Util.symmetricDifference( modelDependencies2, new LinkedHashSet<>( modelDependencies2 ) ) );
}
}
Original file line number Diff line number Diff line change
@@ -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 ) );
}

}
Original file line number Diff line number Diff line change
@@ -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" );
}
}

0 comments on commit d94f9bb

Please sign in to comment.