Skip to content

Commit cd44ef3

Browse files
committed
Review Changes
1 parent ce459e8 commit cd44ef3

File tree

7 files changed

+137
-22
lines changed

7 files changed

+137
-22
lines changed

collection-pipeline/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ Use the Collection Pipeline pattern when
2626
## Credits
2727

2828
* [Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html)
29-
* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
29+
* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
30+
* Java8 Streams (https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
*/
2323
package com.iluwatar.collectionpipeline;
2424

25+
import java.util.Arrays;
2526
import java.util.List;
27+
import java.util.Map;
2628

2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
@@ -49,12 +51,26 @@ public class App {
4951
*/
5052
public static void main(String[] args) {
5153

52-
List<Car> cars = Iterating.createCars();
54+
List<Car> cars = CarFactory.createCars();
5355

54-
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000UsingFor(cars);
56+
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
5557
LOGGER.info(modelsImperative.toString());
5658

57-
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000UsingPipeline(cars);
59+
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
5860
LOGGER.info(modelsFunctional.toString());
61+
62+
Map<String, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
63+
LOGGER.info(groupingByCategoryImperative.toString());
64+
65+
Map<String, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
66+
LOGGER.info(groupingByCategoryFunctional.toString());
67+
68+
Person john = new Person(cars);
69+
70+
List<Car> sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
71+
LOGGER.info(sedansOwnedImperative.toString());
72+
73+
List<Car> sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
74+
LOGGER.info(sedansOwnedFunctional.toString());
5975
}
6076
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ public class Car {
2929
private String make;
3030
private String model;
3131
private int year;
32+
private String category;
3233

3334
/**
3435
* Constructor to create an instance of car.
35-
* @param theMake the make of the car
36-
* @param theModel the model of the car
36+
* @param make the make of the car
37+
* @param model the model of the car
3738
* @param yearOfMake the year of built of the car
3839
*/
39-
public Car(String theMake, String theModel, int yearOfMake) {
40-
make = theMake;
41-
model = theModel;
42-
year = yearOfMake;
40+
public Car(String make, String model, int yearOfMake, String category) {
41+
this.make = make;
42+
this.model = model;
43+
this.year = yearOfMake;
44+
this.category = category;
4345
}
4446

4547
public String getMake() {
@@ -53,4 +55,8 @@ public String getModel() {
5355
public int getYear() {
5456
return year;
5557
}
58+
59+
public String getCategory() {
60+
return category;
61+
}
5662
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Iterating.java renamed to collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/CarFactory.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828
/**
2929
* A factory class to create a collection of {@link Car} instances.
3030
*/
31-
public class Iterating {
32-
private Iterating() {
31+
public class CarFactory {
32+
private CarFactory() {
3333
}
3434

3535
/**
3636
* Factory method to create a {@link List} of {@link Car} instances.
3737
* @return {@link List} of {@link Car}
3838
*/
3939
public static List<Car> createCars() {
40-
return Arrays.asList(new Car("Jeep", "Wrangler", 2011), new Car("Jeep", "Comanche", 1990),
41-
new Car("Dodge", "Avenger", 2010),
42-
new Car("Buick", "Cascada", 2016),
43-
new Car("Ford", "Focus", 2012),
44-
new Car("Chevrolet", "Geo Metro", 1992));
40+
return Arrays.asList(new Car("Jeep", "Wrangler", 2011, "Jeep"), new Car("Jeep", "Comanche", 1990, "Jeep"),
41+
new Car("Dodge", "Avenger", 2010, "Sedan"),
42+
new Car("Buick", "Cascada", 2016, "Convertible"),
43+
new Car("Ford", "Focus", 2012, "Sedan"),
44+
new Car("Chevrolet", "Geo Metro", 1992, "Convertible"));
4545
}
4646
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.Comparator;
2626
import java.util.List;
27+
import java.util.Map;
2728
import java.util.stream.Collectors;
2829

2930
/**
@@ -53,9 +54,32 @@ private FunctionalProgramming() {
5354
* @param cars {@link List} of {@link Car} to be used for filtering
5455
* @return {@link List} of {@link String} representing models built after year 2000
5556
*/
56-
public static List<String> getModelsAfter2000UsingPipeline(List<Car> cars) {
57+
public static List<String> getModelsAfter2000(List<Car> cars) {
5758
return cars.stream().filter(car -> car.getYear() > 2000)
5859
.sorted(Comparator.comparing(Car::getYear))
5960
.map(Car::getModel).collect(Collectors.toList());
6061
}
62+
63+
/**
64+
* Method to group cars by category using groupingBy
65+
*
66+
* @param cars {@link List} of {@link Car} to be used for grouping
67+
* @return {@link Map} of {@link String} and {@link List} of {@link Car} with category
68+
* as key and cars belonging to that category as value
69+
*/
70+
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
71+
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
72+
}
73+
74+
/**
75+
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture
76+
*
77+
* @param persons {@link List} of {@link Person} to be used
78+
* @return {@link List} of {@link Car} to belonging to the group
79+
*/
80+
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
81+
return persons.stream().map(Person::getCars).flatMap(List::stream)
82+
.filter(car -> "Sedan".equals(car.getCategory()))
83+
.sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList());
84+
}
6185
}

collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import java.util.ArrayList;
2626
import java.util.Collections;
2727
import java.util.Comparator;
28+
import java.util.HashMap;
2829
import java.util.List;
30+
import java.util.Map;
2931

3032
/**
3133
* Imperative-style programming to iterate over the list and get the names of
@@ -55,7 +57,7 @@ private ImperativeProgramming() {
5557
* @param cars {@link List} of {@link Car} to iterate over
5658
* @return {@link List} of {@link String} of car models built after year 2000
5759
*/
58-
public static List<String> getModelsAfter2000UsingFor(List<Car> cars) {
60+
public static List<String> getModelsAfter2000(List<Car> cars) {
5961
List<Car> carsSortedByYear = new ArrayList<>();
6062

6163
for (Car car : cars) {
@@ -77,4 +79,54 @@ public int compare(Car car1, Car car2) {
7779

7880
return models;
7981
}
82+
83+
/**
84+
* Method to group cars by category using for loops
85+
*
86+
* @param cars {@link List} of {@link Car} to be used for grouping
87+
* @return {@link Map} of {@link String} and {@link List} of {@link Car} with
88+
* category as key and cars belonging to that category as value
89+
*/
90+
public static Map<String, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
91+
Map<String, List<Car>> groupingByCategory = new HashMap<>();
92+
for (Car car: cars) {
93+
if (groupingByCategory.containsKey(car.getCategory())) {
94+
groupingByCategory.get(car.getCategory()).add(car);
95+
} else {
96+
List<Car> categoryCars = new ArrayList<>();
97+
categoryCars.add(car);
98+
groupingByCategory.put(car.getCategory(), categoryCars);
99+
}
100+
}
101+
return groupingByCategory;
102+
}
103+
104+
/**
105+
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture using for loops
106+
*
107+
* @param persons {@link List} of {@link Person} to be used
108+
* @return {@link List} of {@link Car} to belonging to the group
109+
*/
110+
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
111+
List<Car> cars = new ArrayList<>();
112+
for (Person person: persons) {
113+
cars.addAll(person.getCars());
114+
}
115+
116+
List<Car> sedanCars = new ArrayList<>();
117+
for (Car car: cars) {
118+
if ("Sedan".equals(car.getCategory())) {
119+
sedanCars.add(car);
120+
}
121+
}
122+
123+
sedanCars.sort(new Comparator<Car>() {
124+
@Override
125+
public int compare(Car o1, Car o2) {
126+
return o1.getYear() - o2.getYear();
127+
}
128+
});
129+
130+
return sedanCars;
131+
}
80132
}

collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.Arrays;
2828
import java.util.List;
29+
import java.util.Map;
2930

3031
import org.junit.jupiter.api.Test;
3132

@@ -34,17 +35,32 @@
3435
*/
3536
public class AppTest {
3637

37-
private List<Car> cars = Iterating.createCars();
38+
private List<Car> cars = CarFactory.createCars();
3839

3940
@Test
4041
public void testGetModelsAfter2000UsingFor() {
41-
List<String> models = ImperativeProgramming.getModelsAfter2000UsingFor(cars);
42+
List<String> models = ImperativeProgramming.getModelsAfter2000(cars);
4243
assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"));
4344
}
4445

4546
@Test
4647
public void testGetModelsAfter2000UsingPipeline() {
47-
List<String> models = FunctionalProgramming.getModelsAfter2000UsingPipeline(cars);
48+
List<String> models = FunctionalProgramming.getModelsAfter2000(cars);
4849
assertEquals(models, Arrays.asList("Avenger", "Wrangler", "Focus", "Cascada"));
4950
}
51+
52+
@Test
53+
public void testGetGroupingOfCarsByCategory() {
54+
Map<String, List<Car>> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
55+
Map<String, List<Car>> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
56+
assertEquals(modelsFunctional, modelsImperative);
57+
}
58+
59+
@Test
60+
public void testGetSedanCarsOwnedSortedByDate() {
61+
Person john = new Person(cars);
62+
List<Car> modelsFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
63+
List<Car> modelsImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(Arrays.asList(john));
64+
assertEquals(modelsFunctional, modelsImperative);
65+
}
5066
}

0 commit comments

Comments
 (0)