* minor fixes and cleaning duties

This commit is contained in:
geroza 2018-07-25 17:36:17 -03:00 committed by José Carlos Valero Sánchez
parent 6b2fb615a7
commit 81027b7fbd
6 changed files with 10 additions and 35 deletions

View File

@ -18,7 +18,6 @@ public class CollectionFilterUnitTest {
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8); private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8);
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap(); private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap();
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() { private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() {
Map<Integer, List<Integer>> map = new HashMap<>(); Map<Integer, List<Integer>> map = new HashMap<>();
map.put(1, Arrays.asList(8)); map.put(1, Arrays.asList(8));
@ -30,7 +29,6 @@ public class CollectionFilterUnitTest {
} }
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap(); private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap();
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() { private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() {
Map<Integer, List<Integer>> map = new HashMap<>(); Map<Integer, List<Integer>> map = new HashMap<>();
map.put(1, Arrays.asList(8)); map.put(1, Arrays.asList(8));

View File

@ -39,12 +39,12 @@
<dependency> <dependency>
<groupId>org.eclipse.collections</groupId> <groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId> <artifactId>eclipse-collections-api</artifactId>
<version>9.2.0</version> <version>${eclipse.collections.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.collections</groupId> <groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId> <artifactId>eclipse-collections</artifactId>
<version>9.2.0</version> <version>${eclipse.collections.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
@ -67,5 +67,6 @@
<collections-generic.version>4.01</collections-generic.version> <collections-generic.version>4.01</collections-generic.version>
<avaitility.version>1.7.0</avaitility.version> <avaitility.version>1.7.0</avaitility.version>
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
<eclipse.collections.version>9.2.0</eclipse.collections.version>
</properties> </properties>
</project> </project>

View File

@ -8,13 +8,7 @@ import org.apache.commons.collections4.Predicate;
public class CollectionUtilsCollectionFilter { public class CollectionUtilsCollectionFilter {
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) { static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
Predicate<Integer> apacheEventNumberPredicate = new Predicate<Integer>() { Predicate<Integer> apacheEventNumberPredicate = item -> item % 2 == 0;
@Override
public boolean evaluate(Integer object) {
return object % 2 == 0;
}
};
CollectionUtils.filter(baseCollection, apacheEventNumberPredicate); CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
return baseCollection; return baseCollection;

View File

@ -8,13 +8,8 @@ import com.google.common.collect.Collections2;
public class GuavaCollectionFilter { public class GuavaCollectionFilter {
static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) { static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
Predicate<Integer> guavaPredicate = new Predicate<Integer>() { Predicate<Integer> guavaPredicate = item -> item % 2 == 0;
@Override
public boolean apply(Integer input) {
return input % 2 == 0;
}
};
Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate); Collection<Integer> filteredCollection = Collections2.filter(baseCollection, guavaPredicate);
return filteredCollection; return filteredCollection;
} }

View File

@ -16,22 +16,11 @@ public class StreamsCollectionFilter {
return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0); return filterCollectionHelperMethod(baseCollection, item -> item % 2 == 0);
} }
static public Collection<Integer> findEvenNumbersUsingLambda(Collection<Integer> baseCollection) { static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
return baseCollection.stream() Predicate<Integer> streamsPredicate = item -> item % 2 == 0;
.filter(item -> item % 2 == 0)
.collect(Collectors.toList());
}
static public Collection<Integer> findEvenNumbersUsingPredicate(Collection<Integer> baseCollection) {
Predicate<Integer> evenNumberPredicate = new Predicate<Integer>() {
@Override
public boolean test(Integer i) {
return i % 2 == 0;
}
};
return baseCollection.stream() return baseCollection.stream()
.filter(evenNumberPredicate) .filter(streamsPredicate)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }

View File

@ -27,15 +27,13 @@ public class CollectionFiltersUnitTest {
@Test @Test
public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() { public void givenAnIntegerCollection_whenFilteringEvenValues_thenObtainTheFilteredCollectionForAllCases() {
Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbersUsingLambda(BASE_INTEGER_COLLECTION); Collection<Integer> filteredWithStreams1 = StreamsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithStreams2 = StreamsCollectionFilter.findEvenNumbersUsingPredicate(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION)); Collection<Integer> filteredWithCollectionUtils = CollectionUtilsCollectionFilter.findEvenNumbers(new ArrayList<>(BASE_INTEGER_COLLECTION));
Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); Collection<Integer> filteredWithEclipseCollections = EclipseCollectionsCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION); Collection<Integer> filteredWithEclipseCollectionsUsingIterate = EclipseCollectionsCollectionFilter.findEvenNumbersUsingIterate(BASE_INTEGER_COLLECTION);
Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION); Collection<Integer> filteredWithGuava = GuavaCollectionFilter.findEvenNumbers(BASE_INTEGER_COLLECTION);
assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithStreams2) assertThat(filteredWithStreams1).hasSameElementsAs(filteredWithCollectionUtils)
.hasSameElementsAs(filteredWithCollectionUtils)
.hasSameElementsAs(filteredWithEclipseCollections) .hasSameElementsAs(filteredWithEclipseCollections)
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate) .hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)
.hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate) .hasSameElementsAs(filteredWithEclipseCollectionsUsingIterate)