Merge pull request #448 from atheedom/master
Some minor changes to code logic and style
This commit is contained in:
commit
de4899e30b
@ -126,8 +126,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.7</source>
|
<source>1.8</source>
|
||||||
<target>1.7</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
@ -3,37 +3,36 @@ package com.baeldung.enums;
|
|||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.commons.collections15.CollectionUtils;
|
|
||||||
import org.apache.commons.collections15.Predicate;
|
import org.apache.commons.collections15.Predicate;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Pizza {
|
public class Pizza {
|
||||||
|
|
||||||
private static EnumSet<PizzaStatusEnum> undeliveredPizzaStatuses =
|
private static EnumSet<PizzaStatus> undeliveredPizzaStatuses =
|
||||||
EnumSet.of(PizzaStatusEnum.ORDERED, PizzaStatusEnum.READY);
|
EnumSet.of(PizzaStatus.ORDERED, PizzaStatus.READY);
|
||||||
|
|
||||||
private PizzaStatusEnum status;
|
private PizzaStatus status;
|
||||||
|
|
||||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||||
public enum PizzaStatusEnum {
|
public enum PizzaStatus {
|
||||||
ORDERED (5){
|
ORDERED(5) {
|
||||||
@Override
|
@Override
|
||||||
public boolean isOrdered() {
|
public boolean isOrdered() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
READY (2){
|
READY(2) {
|
||||||
@Override
|
@Override
|
||||||
public boolean isReady() {
|
public boolean isReady() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
DELIVERED (0){
|
DELIVERED(0) {
|
||||||
@Override
|
@Override
|
||||||
public boolean isDelivered() {
|
public boolean isDelivered() {
|
||||||
return true;
|
return true;
|
||||||
@ -42,26 +41,33 @@ public class Pizza {
|
|||||||
|
|
||||||
private int timeToDelivery;
|
private int timeToDelivery;
|
||||||
|
|
||||||
public boolean isOrdered() {return false;}
|
public boolean isOrdered() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isReady() {return false;}
|
public boolean isReady() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDelivered() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isDelivered(){return false;}
|
|
||||||
@JsonProperty("timeToDelivery")
|
@JsonProperty("timeToDelivery")
|
||||||
public int getTimeToDelivery() {
|
public int getTimeToDelivery() {
|
||||||
return timeToDelivery;
|
return timeToDelivery;
|
||||||
}
|
}
|
||||||
|
|
||||||
private PizzaStatusEnum (int timeToDelivery) {
|
PizzaStatus(int timeToDelivery) {
|
||||||
this.timeToDelivery = timeToDelivery;
|
this.timeToDelivery = timeToDelivery;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PizzaStatusEnum getStatus() {
|
public PizzaStatus getStatus() {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStatus(PizzaStatusEnum status) {
|
public void setStatus(PizzaStatus status) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,32 +79,23 @@ public class Pizza {
|
|||||||
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
|
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Pizza> getAllUndeliveredPizza(List<Pizza> input) {
|
public static List<Pizza> getAllUndeliveredPizzas(List<Pizza> input) {
|
||||||
List<Pizza> undelivered = input;
|
return input.stream().filter(
|
||||||
CollectionUtils.filter(undelivered, thatAreNotDelivered());
|
(s) -> undeliveredPizzaStatuses.contains(s.getStatus()))
|
||||||
return undelivered;
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pizzaList) {
|
public static EnumMap<PizzaStatus, List<Pizza>>
|
||||||
EnumMap<PizzaStatusEnum, List<Pizza>> pzByStatus = new EnumMap<PizzaStatusEnum, List<Pizza>>(PizzaStatusEnum.class);
|
groupPizzaByStatus(List<Pizza> pzList) {
|
||||||
for (Pizza pz : pizzaList) {
|
return pzList.stream().collect(
|
||||||
PizzaStatusEnum status = pz.getStatus();
|
Collectors.groupingBy(Pizza::getStatus,
|
||||||
|
() -> new EnumMap<>(PizzaStatus.class), Collectors.toList()));
|
||||||
if (pzByStatus.containsKey(status)) {
|
|
||||||
pzByStatus.get(status).add(pz);
|
|
||||||
} else {
|
|
||||||
List<Pizza> newPzList = new ArrayList<Pizza>();
|
|
||||||
newPzList.add(pz);
|
|
||||||
pzByStatus.put(status, newPzList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return pzByStatus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deliver() {
|
public void deliver() {
|
||||||
if (isDeliverable()) {
|
if (isDeliverable()) {
|
||||||
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
|
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
|
||||||
this.setStatus(PizzaStatusEnum.DELIVERED);
|
this.setStatus(PizzaStatus.DELIVERED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,10 +105,6 @@ public class Pizza {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Predicate<Pizza> thatAreNotDelivered() {
|
private static Predicate<Pizza> thatAreNotDelivered() {
|
||||||
return new Predicate<Pizza>() {
|
return entry -> undeliveredPizzaStatuses.contains(entry.getStatus());
|
||||||
public boolean evaluate(Pizza entry) {
|
|
||||||
return undeliveredPizzaStatuses.contains(entry.getStatus());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,10 @@
|
|||||||
package com.baeldung.enums;
|
package com.baeldung.enums;
|
||||||
|
|
||||||
public enum PizzaDeliverySystemConfiguration {
|
public enum PizzaDeliverySystemConfiguration {
|
||||||
INSTANCE ;
|
INSTANCE;
|
||||||
private PizzaDeliverySystemConfiguration() {
|
|
||||||
//Do the configuration initialization which
|
PizzaDeliverySystemConfiguration() {
|
||||||
|
// Do the configuration initialization which
|
||||||
// involves overriding defaults like delivery strategy
|
// involves overriding defaults like delivery strategy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,66 +15,66 @@ public class PizzaTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenPizaOrder_whenReady_thenDeliverable() {
|
public void givenPizaOrder_whenReady_thenDeliverable() {
|
||||||
Pizza testPz = new Pizza();
|
Pizza testPz = new Pizza();
|
||||||
testPz.setStatus(Pizza.PizzaStatusEnum.READY);
|
testPz.setStatus(Pizza.PizzaStatus.READY);
|
||||||
assertTrue(testPz.isDeliverable());
|
assertTrue(testPz.isDeliverable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() {
|
public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() {
|
||||||
List<Pizza> pzList = new ArrayList<Pizza>();
|
List<Pizza> pzList = new ArrayList<>();
|
||||||
Pizza pz1 = new Pizza();
|
Pizza pz1 = new Pizza();
|
||||||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED);
|
pz1.setStatus(Pizza.PizzaStatus.DELIVERED);
|
||||||
|
|
||||||
Pizza pz2 = new Pizza();
|
Pizza pz2 = new Pizza();
|
||||||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
pz2.setStatus(Pizza.PizzaStatus.ORDERED);
|
||||||
|
|
||||||
Pizza pz3 = new Pizza();
|
Pizza pz3 = new Pizza();
|
||||||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
pz3.setStatus(Pizza.PizzaStatus.ORDERED);
|
||||||
|
|
||||||
Pizza pz4 = new Pizza();
|
Pizza pz4 = new Pizza();
|
||||||
pz4.setStatus(Pizza.PizzaStatusEnum.READY);
|
pz4.setStatus(Pizza.PizzaStatus.READY);
|
||||||
|
|
||||||
pzList.add(pz1);
|
pzList.add(pz1);
|
||||||
pzList.add(pz2);
|
pzList.add(pz2);
|
||||||
pzList.add(pz3);
|
pzList.add(pz3);
|
||||||
pzList.add(pz4);
|
pzList.add(pz4);
|
||||||
|
|
||||||
List<Pizza> undeliveredPzs = Pizza.getAllUndeliveredPizza(pzList);
|
List<Pizza> undeliveredPzs = Pizza.getAllUndeliveredPizzas(pzList);
|
||||||
assertTrue(undeliveredPzs.size() == 3);
|
assertTrue(undeliveredPzs.size() == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() {
|
public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() {
|
||||||
|
|
||||||
List<Pizza> pzList = new ArrayList<Pizza>();
|
List<Pizza> pzList = new ArrayList<>();
|
||||||
Pizza pz1 = new Pizza();
|
Pizza pz1 = new Pizza();
|
||||||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED);
|
pz1.setStatus(Pizza.PizzaStatus.DELIVERED);
|
||||||
|
|
||||||
Pizza pz2 = new Pizza();
|
Pizza pz2 = new Pizza();
|
||||||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
pz2.setStatus(Pizza.PizzaStatus.ORDERED);
|
||||||
|
|
||||||
Pizza pz3 = new Pizza();
|
Pizza pz3 = new Pizza();
|
||||||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
pz3.setStatus(Pizza.PizzaStatus.ORDERED);
|
||||||
|
|
||||||
Pizza pz4 = new Pizza();
|
Pizza pz4 = new Pizza();
|
||||||
pz4.setStatus(Pizza.PizzaStatusEnum.READY);
|
pz4.setStatus(Pizza.PizzaStatus.READY);
|
||||||
|
|
||||||
pzList.add(pz1);
|
pzList.add(pz1);
|
||||||
pzList.add(pz2);
|
pzList.add(pz2);
|
||||||
pzList.add(pz3);
|
pzList.add(pz3);
|
||||||
pzList.add(pz4);
|
pzList.add(pz4);
|
||||||
|
|
||||||
EnumMap<Pizza.PizzaStatusEnum, List<Pizza>> map = Pizza.groupPizzaByStatus(pzList);
|
EnumMap<Pizza.PizzaStatus, List<Pizza>> map = Pizza.groupPizzaByStatus(pzList);
|
||||||
assertTrue(map.get(Pizza.PizzaStatusEnum.DELIVERED).size() == 1);
|
assertTrue(map.get(Pizza.PizzaStatus.DELIVERED).size() == 1);
|
||||||
assertTrue(map.get(Pizza.PizzaStatusEnum.ORDERED).size() == 2);
|
assertTrue(map.get(Pizza.PizzaStatus.ORDERED).size() == 2);
|
||||||
assertTrue(map.get(Pizza.PizzaStatusEnum.READY).size() == 1);
|
assertTrue(map.get(Pizza.PizzaStatus.READY).size() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() {
|
public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() {
|
||||||
Pizza pz = new Pizza();
|
Pizza pz = new Pizza();
|
||||||
pz.setStatus(Pizza.PizzaStatusEnum.READY);
|
pz.setStatus(Pizza.PizzaStatus.READY);
|
||||||
pz.deliver();
|
pz.deliver();
|
||||||
assertTrue(pz.getStatus() == Pizza.PizzaStatusEnum.DELIVERED);
|
assertTrue(pz.getStatus() == Pizza.PizzaStatus.DELIVERED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user