Review Changes

This commit is contained in:
nikhilbarar 2018-08-26 23:12:33 +05:30
parent ce459e8f9f
commit cd44ef3c81
7 changed files with 137 additions and 22 deletions

View File

@ -26,4 +26,5 @@ Use the Collection Pipeline pattern when
## Credits
* [Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html)
* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
* Java8 Streams (https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)

View File

@ -22,7 +22,9 @@
*/
package com.iluwatar.collectionpipeline;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -49,12 +51,26 @@ public class App {
*/
public static void main(String[] args) {
List<Car> cars = Iterating.createCars();
List<Car> cars = CarFactory.createCars();
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000UsingFor(cars);
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
LOGGER.info(modelsImperative.toString());
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000UsingPipeline(cars);
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
LOGGER.info(modelsFunctional.toString());
Map<String, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
LOGGER.info(groupingByCategoryImperative.toString());
Map<String, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
LOGGER.info(groupingByCategoryFunctional.toString());
Person john = new Person(cars);
List<Car> sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
LOGGER.info(sedansOwnedImperative.toString());
List<Car> sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
LOGGER.info(sedansOwnedFunctional.toString());
}
}

View File

@ -29,17 +29,19 @@ public class Car {
private String make;
private String model;
private int year;
private String category;
/**
* Constructor to create an instance of car.
* @param theMake the make of the car
* @param theModel the model of the car
* @param make the make of the car
* @param model the model of the car
* @param yearOfMake the year of built of the car
*/
public Car(String theMake, String theModel, int yearOfMake) {
make = theMake;
model = theModel;
year = yearOfMake;
public Car(String make, String model, int yearOfMake, String category) {
this.make = make;
this.model = model;
this.year = yearOfMake;
this.category = category;
}
public String getMake() {
@ -53,4 +55,8 @@ public class Car {
public int getYear() {
return year;
}
public String getCategory() {
return category;
}
}

View File

@ -28,8 +28,8 @@ import java.util.List;
/**
* A factory class to create a collection of {@link Car} instances.
*/
public class Iterating {
private Iterating() {
public class CarFactory {
private CarFactory() {
}
/**
@ -37,10 +37,10 @@ public class Iterating {
* @return {@link List} of {@link Car}
*/
public static List<Car> createCars() {
return Arrays.asList(new Car("Jeep", "Wrangler", 2011), new Car("Jeep", "Comanche", 1990),
new Car("Dodge", "Avenger", 2010),
new Car("Buick", "Cascada", 2016),
new Car("Ford", "Focus", 2012),
new Car("Chevrolet", "Geo Metro", 1992));
return Arrays.asList(new Car("Jeep", "Wrangler", 2011, "Jeep"), new Car("Jeep", "Comanche", 1990, "Jeep"),
new Car("Dodge", "Avenger", 2010, "Sedan"),
new Car("Buick", "Cascada", 2016, "Convertible"),
new Car("Ford", "Focus", 2012, "Sedan"),
new Car("Chevrolet", "Geo Metro", 1992, "Convertible"));
}
}

View File

@ -24,6 +24,7 @@ package com.iluwatar.collectionpipeline;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@ -53,9 +54,32 @@ public class FunctionalProgramming {
* @param cars {@link List} of {@link Car} to be used for filtering
* @return {@link List} of {@link String} representing models built after year 2000
*/
public static List<String> getModelsAfter2000UsingPipeline(List<Car> cars) {
public static List<String> getModelsAfter2000(List<Car> cars) {
return cars.stream().filter(car -> car.getYear() > 2000)
.sorted(Comparator.comparing(Car::getYear))
.map(Car::getModel).collect(Collectors.toList());
}
/**
* Method to group cars by category using groupingBy
*
* @param cars {@link List} of {@link Car} to be used for grouping
* @return {@link Map} of {@link String} and {@link List} of {@link Car} with category
* as key and cars belonging to that category as value
*/
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
}
/**
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture
*
* @param persons {@link List} of {@link Person} to be used
* @return {@link List} of {@link Car} to belonging to the group
*/
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
return persons.stream().map(Person::getCars).flatMap(List::stream)
.filter(car -> "Sedan".equals(car.getCategory()))
.sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList());
}
}

View File

@ -25,7 +25,9 @@ package com.iluwatar.collectionpipeline;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Imperative-style programming to iterate over the list and get the names of
@ -55,7 +57,7 @@ public class ImperativeProgramming {
* @param cars {@link List} of {@link Car} to iterate over
* @return {@link List} of {@link String} of car models built after year 2000
*/
public static List<String> getModelsAfter2000UsingFor(List<Car> cars) {
public static List<String> getModelsAfter2000(List<Car> cars) {
List<Car> carsSortedByYear = new ArrayList<>();
for (Car car : cars) {
@ -77,4 +79,54 @@ public class ImperativeProgramming {
return models;
}
/**
* Method to group cars by category using for loops
*
* @param cars {@link List} of {@link Car} to be used for grouping
* @return {@link Map} of {@link String} and {@link List} of {@link Car} with
* category as key and cars belonging to that category as value
*/
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
Map<String, 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;
}
/**
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture using for loops
*
* @param persons {@link List} of {@link Person} to be used
* @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 ("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;
}
}

View File

@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
@ -34,17 +35,32 @@ import org.junit.jupiter.api.Test;
*/
public class AppTest {
private List<Car> cars = Iterating.createCars();
private List<Car> cars = CarFactory.createCars();
@Test
public void testGetModelsAfter2000UsingFor() {
List<String> models = ImperativeProgramming.getModelsAfter2000UsingFor(cars);
List<String> models = ImperativeProgramming.getModelsAfter2000(cars);
assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"));
}
@Test
public void testGetModelsAfter2000UsingPipeline() {
List<String> models = FunctionalProgramming.getModelsAfter2000UsingPipeline(cars);
List<String> models = FunctionalProgramming.getModelsAfter2000(cars);
assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"));
}
@Test
public void testGetGroupingOfCarsByCategory() {
Map<String, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
Map<String, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
assertEquals(modelsFunctional, modelsImperative);
}
@Test
public void testGetSedanCarsOwnedSortedByDate() {
Person john = new Person(cars);
List<Car> modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
List<Car> modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
assertEquals(modelsFunctional, modelsImperative);
}
}