Java 11 migration: patterns (remaining b-c) (#1081)

* Moves business-delegate pattern  to java 11

* Moves bytecode pattern  to java 11

* Moves caching pattern  to java 11

* Moves callback pattern  to java 11

* Moves chain pattern  to java 11

* Moves circuit-breaker pattern  to java 11

* Moves collection-pipeline pattern  to java 11

* Moves command pattern  to java 11

* Moves commander pattern  to java 11

* Moves composite pattern  to java 11

* Corrects test cases
This commit is contained in:
Anurag Agarwal
2019-11-13 01:26:46 +05:30
committed by Ilkka Seppälä
parent 6ef840f3cf
commit 33ea7335b1
63 changed files with 798 additions and 979 deletions

View File

@ -24,7 +24,6 @@
package com.iluwatar.collectionpipeline;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -46,31 +45,26 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
var cars = CarFactory.createCars();
List<Car> cars = CarFactory.createCars();
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
var modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
LOGGER.info(modelsImperative.toString());
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
var modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
LOGGER.info(modelsFunctional.toString());
Map<Category, List<Car>> groupingByCategoryImperative =
ImperativeProgramming.getGroupingOfCarsByCategory(cars);
var groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
LOGGER.info(groupingByCategoryImperative.toString());
Map<Category, List<Car>> groupingByCategoryFunctional =
FunctionalProgramming.getGroupingOfCarsByCategory(cars);
var groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
LOGGER.info(groupingByCategoryFunctional.toString());
Person john = new Person(cars);
var john = new Person(cars);
List<Car> sedansOwnedImperative =
ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
var sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
LOGGER.info(sedansOwnedImperative.toString());
List<Car> sedansOwnedFunctional =
FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
var sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
LOGGER.info(sedansOwnedFunctional.toString());
}
}

View File

@ -23,12 +23,12 @@
package com.iluwatar.collectionpipeline;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Imperative-style programming to iterate over the list and get the names of cars made later than
@ -57,26 +57,11 @@ public class ImperativeProgramming {
* @return {@link List} of {@link String} of car models built after year 2000
*/
public static List<String> getModelsAfter2000(List<Car> cars) {
List<Car> carsSortedByYear = new ArrayList<>();
for (Car car : cars) {
if (car.getYear() > 2000) {
carsSortedByYear.add(car);
}
}
Collections.sort(carsSortedByYear, new Comparator<Car>() {
public int compare(Car car1, Car car2) {
return new Integer(car1.getYear()).compareTo(car2.getYear());
}
});
List<String> models = new ArrayList<>();
for (Car car : carsSortedByYear) {
models.add(car.getModel());
}
return models;
return cars.stream()
.filter(car -> car.getYear() > 2000)
.sorted(Comparator.comparingInt(Car::getYear))
.map(Car::getModel)
.collect(Collectors.toList());
}
/**
@ -86,17 +71,7 @@ public class ImperativeProgramming {
* @return {@link Map} with category as key and cars belonging to that category as value
*/
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
for (Car car : cars) {
if (groupingByCategory.containsKey(car.getCategory())) {
groupingByCategory.get(car.getCategory()).add(car);
} else {
List<Car> categoryCars = new ArrayList<>();
categoryCars.add(car);
groupingByCategory.put(car.getCategory(), categoryCars);
}
}
return groupingByCategory;
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
}
/**
@ -107,25 +82,11 @@ public class ImperativeProgramming {
* @return {@link List} of {@link Car} to belonging to the group
*/
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
List<Car> cars = new ArrayList<>();
for (Person person : persons) {
cars.addAll(person.getCars());
}
List<Car> sedanCars = new ArrayList<>();
for (Car car : cars) {
if (Category.SEDAN.equals(car.getCategory())) {
sedanCars.add(car);
}
}
sedanCars.sort(new Comparator<Car>() {
@Override
public int compare(Car o1, Car o2) {
return o1.getYear() - o2.getYear();
}
});
return sedanCars;
return persons.stream()
.map(Person::getCars)
.flatMap(Collection::stream)
.filter(car -> car.getCategory() == Category.SEDAN)
.sorted(Comparator.comparingInt(Car::getYear))
.collect(Collectors.toList());
}
}

View File

@ -54,12 +54,18 @@ public class AppTest {
@Test
public void testGetGroupingOfCarsByCategory() {
var modelsExpected = Map.of(
Category.CONVERTIBLE, List.of(new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)),
Category.SEDAN, List.of(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
new Car("Ford", "Focus", 2012, Category.SEDAN)),
Category.JEEP, List.of(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
new Car("Jeep", "Comanche", 1990, Category.JEEP)));
Category.CONVERTIBLE, List.of(
new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)
),
Category.SEDAN, List.of(
new Car("Dodge", "Avenger", 2010, Category.SEDAN),
new Car("Ford", "Focus", 2012, Category.SEDAN)
),
Category.JEEP, List.of(
new Car("Jeep", "Wrangler", 2011, Category.JEEP),
new Car("Jeep", "Comanche", 1990, Category.JEEP))
);
var modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
var modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
LOGGER.info("Category " + modelsFunctional);
@ -70,8 +76,10 @@ public class AppTest {
@Test
public void testGetSedanCarsOwnedSortedByDate() {
var john = new Person(cars);
var modelsExpected = List.of(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
new Car("Ford", "Focus", 2012, Category.SEDAN));
var modelsExpected = List.of(
new Car("Dodge", "Avenger", 2010, Category.SEDAN),
new Car("Ford", "Focus", 2012, Category.SEDAN)
);
var modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
var modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
assertEquals(modelsExpected, modelsFunctional);