-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MDEP-832] - Remove commons-collections-4
- Loading branch information
1 parent
078f6aa
commit d94f9bb
Showing
4 changed files
with
181 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/apache/maven/plugins/dependency/analyze/Util.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
src/test/java/org/apache/maven/plugins/dependency/analyze/UtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ); | ||
} | ||
} |