Skip to content

Commit 2f49648

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Resolves checkstyle errors for collection-pipeline, command, commander (iluwatar#1061)
* Reduces checkstyle errors in collection-pipeline * Reduces checkstyle errors in command * Reduces checkstyle errors in commander
1 parent 31f27a7 commit 2f49648

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+646
-574
lines changed

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,54 @@
2323

2424
package com.iluwatar.collectionpipeline;
2525

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2926
import java.util.List;
3027
import java.util.Map;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
3130

3231
/**
33-
* In imperative-style programming, it is common to use for and while loops for
34-
* most kinds of data processing. Function composition is a simple technique
35-
* that lets you sequence modular functions to create more complex operations.
36-
* When you run data through the sequence, you have a collection pipeline.
37-
* Together, the Function Composition and Collection Pipeline patterns enable
38-
* you to create sophisticated programs where data flow from upstream to
39-
* downstream and is passed through a series of transformations.
40-
*
32+
* In imperative-style programming, it is common to use for and while loops for most kinds of data
33+
* processing. Function composition is a simple technique that lets you sequence modular functions
34+
* to create more complex operations. When you run data through the sequence, you have a collection
35+
* pipeline. Together, the Function Composition and Collection Pipeline patterns enable you to
36+
* create sophisticated programs where data flow from upstream to downstream and is passed through a
37+
* series of transformations.
4138
*/
4239
public class App {
4340

4441
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
4542

4643
/**
4744
* Program entry point.
48-
*
49-
* @param args
50-
* command line args
45+
*
46+
* @param args command line args
5147
*/
5248
public static void main(String[] args) {
5349

5450
List<Car> cars = CarFactory.createCars();
55-
51+
5652
List<String> modelsImperative = ImperativeProgramming.getModelsAfter2000(cars);
5753
LOGGER.info(modelsImperative.toString());
5854

5955
List<String> modelsFunctional = FunctionalProgramming.getModelsAfter2000(cars);
6056
LOGGER.info(modelsFunctional.toString());
61-
62-
Map<Category, List<Car>> groupingByCategoryImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
57+
58+
Map<Category, List<Car>> groupingByCategoryImperative =
59+
ImperativeProgramming.getGroupingOfCarsByCategory(cars);
6360
LOGGER.info(groupingByCategoryImperative.toString());
6461

65-
Map<Category, List<Car>> groupingByCategoryFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
62+
Map<Category, List<Car>> groupingByCategoryFunctional =
63+
FunctionalProgramming.getGroupingOfCarsByCategory(cars);
6664
LOGGER.info(groupingByCategoryFunctional.toString());
67-
65+
6866
Person john = new Person(cars);
6967

70-
List<Car> sedansOwnedImperative = ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
68+
List<Car> sedansOwnedImperative =
69+
ImperativeProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
7170
LOGGER.info(sedansOwnedImperative.toString());
7271

73-
List<Car> sedansOwnedFunctional = FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
72+
List<Car> sedansOwnedFunctional =
73+
FunctionalProgramming.getSedanCarsOwnedSortedByDate(List.of(john));
7474
LOGGER.info(sedansOwnedFunctional.toString());
7575
}
7676
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public class Car {
3434

3535
/**
3636
* Constructor to create an instance of car.
37-
* @param make the make of the car
38-
* @param model the model of the car
37+
*
38+
* @param make the make of the car
39+
* @param model the model of the car
3940
* @param yearOfMake the year of built of the car
40-
* @param category the {@link Category} of the car
41+
* @param category the {@link Category} of the car
4142
*/
4243
public Car(String make, String model, int yearOfMake, Category category) {
4344
this.make = make;
@@ -103,7 +104,7 @@ public String getModel() {
103104
public int getYear() {
104105
return year;
105106
}
106-
107+
107108
public Category getCategory() {
108109
return category;
109110
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private CarFactory() {
3434

3535
/**
3636
* Factory method to create a {@link List} of {@link Car} instances.
37+
*
3738
* @return {@link List} of {@link Car}
3839
*/
3940
public static List<Car> createCars() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.iluwatar.collectionpipeline;
2525

2626
/**
27-
* Enum for the category of car
27+
* Enum for the category of car.
2828
*/
2929
public enum Category {
3030
JEEP, SEDAN, CONVERTIBLE

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,53 @@
3030

3131
/**
3232
* Iterating and sorting with a collection pipeline
33-
*
33+
*
3434
* <p>In functional programming, it's common to sequence complex operations through
35-
* a series of smaller modular functions or operations. The series is called a
36-
* composition of functions, or a function composition. When a collection of
37-
* data flows through a function composition, it becomes a collection pipeline.
38-
* Function Composition and Collection Pipeline are two design patterns
39-
* frequently used in functional-style programming.
40-
*
35+
* a series of smaller modular functions or operations. The series is called a composition of
36+
* functions, or a function composition. When a collection of data flows through a function
37+
* composition, it becomes a collection pipeline. Function Composition and Collection Pipeline are
38+
* two design patterns frequently used in functional-style programming.
39+
*
4140
* <p>Instead of passing a lambda expression to the map method, we passed the
42-
* method reference Car::getModel. Likewise, instead of passing the lambda
43-
* expression car -> car.getYear() to the comparing method, we passed the method
44-
* reference Car::getYear. Method references are short, concise, and expressive.
45-
* It is best to use them wherever possible.
46-
*
41+
* method reference Car::getModel. Likewise, instead of passing the lambda expression car ->
42+
* car.getYear() to the comparing method, we passed the method reference Car::getYear. Method
43+
* references are short, concise, and expressive. It is best to use them wherever possible.
4744
*/
4845
public class FunctionalProgramming {
4946
private FunctionalProgramming() {
5047
}
5148

5249
/**
5350
* Method to get models using for collection pipeline.
54-
*
51+
*
5552
* @param cars {@link List} of {@link Car} to be used for filtering
5653
* @return {@link List} of {@link String} representing models built after year 2000
5754
*/
5855
public static List<String> getModelsAfter2000(List<Car> cars) {
5956
return cars.stream().filter(car -> car.getYear() > 2000)
60-
.sorted(Comparator.comparing(Car::getYear))
61-
.map(Car::getModel).collect(Collectors.toList());
57+
.sorted(Comparator.comparing(Car::getYear))
58+
.map(Car::getModel).collect(Collectors.toList());
6259
}
63-
60+
6461
/**
65-
* Method to group cars by category using groupingBy
66-
*
62+
* Method to group cars by category using groupingBy.
63+
*
6764
* @param cars {@link List} of {@link Car} to be used for grouping
6865
* @return {@link Map} with category as key and cars belonging to that category as value
6966
*/
7067
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
7168
return cars.stream().collect(Collectors.groupingBy(Car::getCategory));
7269
}
73-
70+
7471
/**
75-
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture
76-
*
72+
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture.
73+
*
7774
* @param persons {@link List} of {@link Person} to be used
7875
* @return {@link List} of {@link Car} to belonging to the group
7976
*/
8077
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
8178
return persons.stream().map(Person::getCars).flatMap(List::stream)
82-
.filter(car -> Category.SEDAN.equals(car.getCategory()))
83-
.sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList());
79+
.filter(car -> Category.SEDAN.equals(car.getCategory()))
80+
.sorted(Comparator.comparing(Car::getYear)).collect(Collectors.toList());
8481
}
8582
}

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,28 @@
3131
import java.util.Map;
3232

3333
/**
34-
* Imperative-style programming to iterate over the list and get the names of
35-
* cars made later than the year 2000. We then sort the models in ascending
36-
* order by year.
37-
*
34+
* Imperative-style programming to iterate over the list and get the names of cars made later than
35+
* the year 2000. We then sort the models in ascending order by year.
36+
*
3837
* <p>As you can see, there's a lot of looping in this code. First, the
39-
* getModelsAfter2000UsingFor method takes a list of cars as its parameter. It
40-
* extracts or filters out cars made after the year 2000, putting them into a
41-
* new list named carsSortedByYear. Next, it sorts that list in ascending order
42-
* by year-of-make. Finally, it loops through the list carsSortedByYear to get
43-
* the model names and returns them in a list.
44-
*
38+
* getModelsAfter2000UsingFor method takes a list of cars as its parameter. It extracts or filters
39+
* out cars made after the year 2000, putting them into a new list named carsSortedByYear. Next, it
40+
* sorts that list in ascending order by year-of-make. Finally, it loops through the list
41+
* carsSortedByYear to get the model names and returns them in a list.
42+
*
4543
* <p>This short example demonstrates what I call the effect of statements. While
46-
* functions and methods in general can be used as expressions, the {@link Collections}
47-
* sort method doesn't return a result. Because it is used as a statement, it
48-
* mutates the list given as argument. Both of the for loops also mutate lists
49-
* as they iterate. Being statements, that's just how these elements work. As a
50-
* result, the code contains unnecessary garbage variables
44+
* functions and methods in general can be used as expressions, the {@link Collections} sort method
45+
* doesn't return a result. Because it is used as a statement, it mutates the list given as
46+
* argument. Both of the for loops also mutate lists as they iterate. Being statements, that's just
47+
* how these elements work. As a result, the code contains unnecessary garbage variables
5148
*/
5249
public class ImperativeProgramming {
5350
private ImperativeProgramming() {
5451
}
5552

5653
/**
5754
* Method to return the car models built after year 2000 using for loops.
55+
*
5856
* @param cars {@link List} of {@link Car} to iterate over
5957
* @return {@link List} of {@link String} of car models built after year 2000
6058
*/
@@ -80,16 +78,16 @@ public int compare(Car car1, Car car2) {
8078

8179
return models;
8280
}
83-
81+
8482
/**
85-
* Method to group cars by category using for loops
86-
*
83+
* Method to group cars by category using for loops.
84+
*
8785
* @param cars {@link List} of {@link Car} to be used for grouping
8886
* @return {@link Map} with category as key and cars belonging to that category as value
8987
*/
9088
public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> cars) {
9189
Map<Category, List<Car>> groupingByCategory = new HashMap<>();
92-
for (Car car: cars) {
90+
for (Car car : cars) {
9391
if (groupingByCategory.containsKey(car.getCategory())) {
9492
groupingByCategory.get(car.getCategory()).add(car);
9593
} else {
@@ -100,33 +98,34 @@ public static Map<Category, List<Car>> getGroupingOfCarsByCategory(List<Car> car
10098
}
10199
return groupingByCategory;
102100
}
103-
101+
104102
/**
105-
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture using for loops
106-
*
103+
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture
104+
* using for loops.
105+
*
107106
* @param persons {@link List} of {@link Person} to be used
108107
* @return {@link List} of {@link Car} to belonging to the group
109108
*/
110109
public static List<Car> getSedanCarsOwnedSortedByDate(List<Person> persons) {
111110
List<Car> cars = new ArrayList<>();
112-
for (Person person: persons) {
111+
for (Person person : persons) {
113112
cars.addAll(person.getCars());
114113
}
115-
114+
116115
List<Car> sedanCars = new ArrayList<>();
117-
for (Car car: cars) {
116+
for (Car car : cars) {
118117
if (Category.SEDAN.equals(car.getCategory())) {
119118
sedanCars.add(car);
120119
}
121120
}
122-
121+
123122
sedanCars.sort(new Comparator<Car>() {
124123
@Override
125124
public int compare(Car o1, Car o2) {
126125
return o1.getYear() - o2.getYear();
127126
}
128127
});
129-
128+
130129
return sedanCars;
131130
}
132131
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class Person {
3333

3434
/**
3535
* Constructor to create an instance of person.
36+
*
3637
* @param cars the list of cars owned
3738
*/
3839
public Person(List<Car> cars) {

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323

2424
package com.iluwatar.collectionpipeline;
2525

26-
import org.junit.jupiter.api.Test;
27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
2927

3028
import java.util.List;
3129
import java.util.Map;
32-
33-
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import org.junit.jupiter.api.Test;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3433

3534
/**
3635
* Tests that Collection Pipeline methods work as expected.
@@ -39,35 +38,35 @@ public class AppTest {
3938
private static final Logger LOGGER = LoggerFactory.getLogger(AppTest.class);
4039

4140
private List<Car> cars = CarFactory.createCars();
42-
41+
4342
@Test
4443
public void testGetModelsAfter2000UsingFor() {
4544
var models = ImperativeProgramming.getModelsAfter2000(cars);
4645
assertEquals(List.of("Avenger", "Wrangler", "Focus", "Cascada"), models);
4746
}
48-
47+
4948
@Test
5049
public void testGetModelsAfter2000UsingPipeline() {
5150
var models = FunctionalProgramming.getModelsAfter2000(cars);
5251
assertEquals(List.of("Avenger", "Wrangler", "Focus", "Cascada"), models);
5352
}
54-
53+
5554
@Test
5655
public void testGetGroupingOfCarsByCategory() {
5756
var modelsExpected = Map.of(
58-
Category.CONVERTIBLE, List.of(new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
59-
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)),
60-
Category.SEDAN, List.of(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
61-
new Car("Ford", "Focus", 2012, Category.SEDAN)),
62-
Category.JEEP, List.of(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
63-
new Car("Jeep", "Comanche", 1990, Category.JEEP)));
57+
Category.CONVERTIBLE, List.of(new Car("Buick", "Cascada", 2016, Category.CONVERTIBLE),
58+
new Car("Chevrolet", "Geo Metro", 1992, Category.CONVERTIBLE)),
59+
Category.SEDAN, List.of(new Car("Dodge", "Avenger", 2010, Category.SEDAN),
60+
new Car("Ford", "Focus", 2012, Category.SEDAN)),
61+
Category.JEEP, List.of(new Car("Jeep", "Wrangler", 2011, Category.JEEP),
62+
new Car("Jeep", "Comanche", 1990, Category.JEEP)));
6463
var modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars);
6564
var modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars);
6665
LOGGER.info("Category " + modelsFunctional);
6766
assertEquals(modelsExpected, modelsFunctional);
6867
assertEquals(modelsExpected, modelsImperative);
6968
}
70-
69+
7170
@Test
7271
public void testGetSedanCarsOwnedSortedByDate() {
7372
var john = new Person(cars);

0 commit comments

Comments
 (0)