33import com .fasterxml .jackson .annotation .JsonFormat ;
44import com .fasterxml .jackson .annotation .JsonProperty ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
6- import org .apache .commons .collections15 .CollectionUtils ;
76import org .apache .commons .collections15 .Predicate ;
87
98import java .io .IOException ;
10- import java .util .ArrayList ;
119import java .util .EnumMap ;
1210import java .util .EnumSet ;
1311import java .util .List ;
12+ import java .util .stream .Collectors ;
1413
1514public class Pizza {
1615
17- private static EnumSet <PizzaStatusEnum > undeliveredPizzaStatuses =
18- EnumSet .of (PizzaStatusEnum .ORDERED , PizzaStatusEnum .READY );
16+ private static EnumSet <PizzaStatus > undeliveredPizzaStatuses =
17+ EnumSet .of (PizzaStatus .ORDERED , PizzaStatus .READY );
1918
20- private PizzaStatusEnum status ;
19+ private PizzaStatus status ;
2120
2221 @ JsonFormat (shape = JsonFormat .Shape .OBJECT )
23- public enum PizzaStatusEnum {
24- ORDERED (5 ){
22+ public enum PizzaStatus {
23+ ORDERED (5 ) {
2524 @ Override
2625 public boolean isOrdered () {
2726 return true ;
2827 }
2928 },
30- READY (2 ){
29+ READY (2 ) {
3130 @ Override
3231 public boolean isReady () {
3332 return true ;
3433 }
3534 },
36- DELIVERED (0 ){
35+ DELIVERED (0 ) {
3736 @ Override
3837 public boolean isDelivered () {
3938 return true ;
@@ -42,26 +41,33 @@ public boolean isDelivered() {
4241
4342 private int timeToDelivery ;
4443
45- public boolean isOrdered () {return false ;}
44+ public boolean isOrdered () {
45+ return false ;
46+ }
47+
48+ public boolean isReady () {
49+ return false ;
50+ }
4651
47- public boolean isReady () {return false ;}
52+ public boolean isDelivered () {
53+ return false ;
54+ }
4855
49- public boolean isDelivered (){return false ;}
5056 @ JsonProperty ("timeToDelivery" )
5157 public int getTimeToDelivery () {
5258 return timeToDelivery ;
5359 }
5460
55- private PizzaStatusEnum (int timeToDelivery ) {
61+ PizzaStatus (int timeToDelivery ) {
5662 this .timeToDelivery = timeToDelivery ;
5763 }
5864 }
5965
60- public PizzaStatusEnum getStatus () {
66+ public PizzaStatus getStatus () {
6167 return status ;
6268 }
6369
64- public void setStatus (PizzaStatusEnum status ) {
70+ public void setStatus (PizzaStatus status ) {
6571 this .status = status ;
6672 }
6773
@@ -73,32 +79,23 @@ public void printTimeToDeliver() {
7379 System .out .println ("Time to delivery is " + this .getStatus ().getTimeToDelivery () + " days" );
7480 }
7581
76- public static List <Pizza > getAllUndeliveredPizza (List <Pizza > input ) {
77- List < Pizza > undelivered = input ;
78- CollectionUtils . filter ( undelivered , thatAreNotDelivered ());
79- return undelivered ;
82+ public static List <Pizza > getAllUndeliveredPizzas (List <Pizza > input ) {
83+ return input . stream (). filter (
84+ ( s ) -> undeliveredPizzaStatuses . contains ( s . getStatus ()))
85+ . collect ( Collectors . toList ()) ;
8086 }
8187
82- public static EnumMap <PizzaStatusEnum , List <Pizza >> groupPizzaByStatus (List <Pizza > pizzaList ) {
83- EnumMap <PizzaStatusEnum , List <Pizza >> pzByStatus = new EnumMap <PizzaStatusEnum , List <Pizza >>(PizzaStatusEnum .class );
84- for (Pizza pz : pizzaList ) {
85- PizzaStatusEnum status = pz .getStatus ();
86-
87- if (pzByStatus .containsKey (status )) {
88- pzByStatus .get (status ).add (pz );
89- } else {
90- List <Pizza > newPzList = new ArrayList <Pizza >();
91- newPzList .add (pz );
92- pzByStatus .put (status , newPzList );
93- }
94- }
95- return pzByStatus ;
88+ public static EnumMap <PizzaStatus , List <Pizza >>
89+ groupPizzaByStatus (List <Pizza > pzList ) {
90+ return pzList .stream ().collect (
91+ Collectors .groupingBy (Pizza ::getStatus ,
92+ () -> new EnumMap <>(PizzaStatus .class ), Collectors .toList ()));
9693 }
9794
9895 public void deliver () {
9996 if (isDeliverable ()) {
10097 PizzaDeliverySystemConfiguration .getInstance ().getDeliveryStrategy ().deliver (this );
101- this .setStatus (PizzaStatusEnum .DELIVERED );
98+ this .setStatus (PizzaStatus .DELIVERED );
10299 }
103100 }
104101
@@ -108,10 +105,6 @@ public static String getJsonString(Pizza pz) throws IOException {
108105 }
109106
110107 private static Predicate <Pizza > thatAreNotDelivered () {
111- return new Predicate <Pizza >() {
112- public boolean evaluate (Pizza entry ) {
113- return undeliveredPizzaStatuses .contains (entry .getStatus ());
114- }
115- };
108+ return entry -> undeliveredPizzaStatuses .contains (entry .getStatus ());
116109 }
117110}
0 commit comments