Skip to content

Commit

Permalink
Review Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilbarar committed Aug 26, 2018
1 parent ce459e8 commit cd44ef3
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 22 deletions.
3 changes: 2 additions & 1 deletion collection-pipeline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -53,4 +55,8 @@ public String getModel() {
public int getYear() {
return year;
}

public String getCategory() {
return category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
/**
* A factory class to create a collection of {@link Car} instances.
*/
public class Iterating {
private Iterating() {
public class CarFactory {
private CarFactory() {
}

/**
* Factory method to create a {@link List} of {@link Car} instances.
* @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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -53,9 +54,32 @@ private 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
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
Expand Down Expand Up @@ -55,7 +57,7 @@ private 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) {
Expand All @@ -77,4 +79,54 @@ public int compare(Car car1, Car car2) {

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

Expand All @@ -34,17 +35,32 @@
*/
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);
}
}

0 comments on commit cd44ef3

Please sign in to comment.