Resolves checkstyle errors for collection-pipeline, command, commander (#1061)
* Reduces checkstyle errors in collection-pipeline * Reduces checkstyle errors in command * Reduces checkstyle errors in commander
This commit is contained in:
committed by
Ilkka Seppälä
parent
31f27a720b
commit
2f49648047
collection-pipeline/src
main
java
com
test
java
com
iluwatar
collectionpipeline
command/src/main/java/com/iluwatar/command
App.javaCommand.javaGoblin.javaInvisibilitySpell.javaShrinkSpell.javaSize.javaTarget.javaVisibility.javaWizard.java
commander/src/main/java/com/iluwatar/commander
AppEmployeeDbFailCases.javaAppMessagingFailCases.javaAppPaymentFailCases.javaAppQueueFailCases.javaAppShippingFailCases.javaCommander.javaDatabase.javaOrder.javaRetry.javaService.java
employeehandle
exceptions
messagingservice
paymentservice
queue
shippingservice
@ -31,23 +31,20 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Imperative-style programming to iterate over the list and get the names of
|
||||
* cars made later than the year 2000. We then sort the models in ascending
|
||||
* order by year.
|
||||
*
|
||||
* Imperative-style programming to iterate over the list and get the names of cars made later than
|
||||
* the year 2000. We then sort the models in ascending order by year.
|
||||
*
|
||||
* <p>As you can see, there's a lot of looping in this code. First, the
|
||||
* getModelsAfter2000UsingFor method takes a list of cars as its parameter. It
|
||||
* extracts or filters out cars made after the year 2000, putting them into a
|
||||
* new list named carsSortedByYear. Next, it sorts that list in ascending order
|
||||
* by year-of-make. Finally, it loops through the list carsSortedByYear to get
|
||||
* the model names and returns them in a list.
|
||||
*
|
||||
* getModelsAfter2000UsingFor method takes a list of cars as its parameter. It extracts or filters
|
||||
* out cars made after the year 2000, putting them into a new list named carsSortedByYear. Next, it
|
||||
* sorts that list in ascending order by year-of-make. Finally, it loops through the list
|
||||
* carsSortedByYear to get the model names and returns them in a list.
|
||||
*
|
||||
* <p>This short example demonstrates what I call the effect of statements. While
|
||||
* functions and methods in general can be used as expressions, the {@link Collections}
|
||||
* sort method doesn't return a result. Because it is used as a statement, it
|
||||
* mutates the list given as argument. Both of the for loops also mutate lists
|
||||
* as they iterate. Being statements, that's just how these elements work. As a
|
||||
* result, the code contains unnecessary garbage variables
|
||||
* functions and methods in general can be used as expressions, the {@link Collections} sort method
|
||||
* doesn't return a result. Because it is used as a statement, it mutates the list given as
|
||||
* argument. Both of the for loops also mutate lists as they iterate. Being statements, that's just
|
||||
* how these elements work. As a result, the code contains unnecessary garbage variables
|
||||
*/
|
||||
public class ImperativeProgramming {
|
||||
private ImperativeProgramming() {
|
||||
@ -55,6 +52,7 @@ public class ImperativeProgramming {
|
||||
|
||||
/**
|
||||
* Method to return the car models built after year 2000 using for loops.
|
||||
*
|
||||
* @param cars {@link List} of {@link Car} to iterate over
|
||||
* @return {@link List} of {@link String} of car models built after year 2000
|
||||
*/
|
||||
@ -80,16 +78,16 @@ public class ImperativeProgramming {
|
||||
|
||||
return models;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method to group cars by category using for loops
|
||||
*
|
||||
* Method to group cars by category using for loops.
|
||||
*
|
||||
* @param cars {@link List} of {@link Car} to be used for grouping
|
||||
* @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) {
|
||||
for (Car car : cars) {
|
||||
if (groupingByCategory.containsKey(car.getCategory())) {
|
||||
groupingByCategory.get(car.getCategory()).add(car);
|
||||
} else {
|
||||
@ -100,33 +98,34 @@ public class ImperativeProgramming {
|
||||
}
|
||||
return groupingByCategory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method to get all Sedan cars belonging to a group of persons sorted by year of manufacture using for loops
|
||||
*
|
||||
* 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) {
|
||||
for (Person person : persons) {
|
||||
cars.addAll(person.getCars());
|
||||
}
|
||||
|
||||
|
||||
List<Car> sedanCars = new ArrayList<>();
|
||||
for (Car car: cars) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user