1+ package com .baeldung .enums ;
2+
3+ import com .fasterxml .jackson .annotation .JsonFormat ;
4+ import com .fasterxml .jackson .annotation .JsonProperty ;
5+ import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import org .apache .commons .collections15 .CollectionUtils ;
7+ import org .apache .commons .collections15 .Predicate ;
8+
9+ import java .io .IOException ;
10+ import java .util .ArrayList ;
11+ import java .util .EnumMap ;
12+ import java .util .EnumSet ;
13+ import java .util .List ;
14+
15+ public class Pizza {
16+
17+ private static EnumSet <PizzaStatusEnum > undeliveredPizzaStatuses =
18+ EnumSet .of (PizzaStatusEnum .ORDERED , PizzaStatusEnum .READY );
19+
20+ private PizzaStatusEnum status ;
21+
22+ @ JsonFormat (shape = JsonFormat .Shape .OBJECT )
23+ public enum PizzaStatusEnum {
24+ ORDERED (5 ){
25+ @ Override
26+ public boolean isOrdered () {
27+ return true ;
28+ }
29+ },
30+ READY (2 ){
31+ @ Override
32+ public boolean isReady () {
33+ return true ;
34+ }
35+ },
36+ DELIVERED (0 ){
37+ @ Override
38+ public boolean isDelivered () {
39+ return true ;
40+ }
41+ };
42+
43+ private int timeToDelivery ;
44+
45+ public boolean isOrdered () {return false ;}
46+
47+ public boolean isReady () {return false ;}
48+
49+ public boolean isDelivered (){return false ;}
50+ @ JsonProperty ("timeToDelivery" )
51+ public int getTimeToDelivery () {
52+ return timeToDelivery ;
53+ }
54+
55+ private PizzaStatusEnum (int timeToDelivery ) {
56+ this .timeToDelivery = timeToDelivery ;
57+ }
58+ }
59+
60+ public PizzaStatusEnum getStatus () {
61+ return status ;
62+ }
63+
64+ public void setStatus (PizzaStatusEnum status ) {
65+ this .status = status ;
66+ }
67+
68+ public boolean isDeliverable () {
69+ return this .status .isReady ();
70+ }
71+
72+ public void printTimeToDeliver () {
73+ System .out .println ("Time to delivery is " + this .getStatus ().getTimeToDelivery () + " days" );
74+ }
75+
76+ public static List <Pizza > getAllUndeliveredPizza (List <Pizza > input ) {
77+ List <Pizza > undelivered = input ;
78+ CollectionUtils .filter (undelivered , thatAreNotDelivered ());
79+ return undelivered ;
80+ }
81+
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 ;
96+ }
97+
98+ public void deliver () {
99+ if (isDeliverable ()) {
100+ PizzaDeliverySystemConfiguration .getInstance ().getDeliveryStrategy ().deliver (this );
101+ this .setStatus (PizzaStatusEnum .DELIVERED );
102+ }
103+ }
104+
105+ public static String getJsonString (Pizza pz ) throws IOException {
106+ ObjectMapper mapper = new ObjectMapper ();
107+ return mapper .writerWithDefaultPrettyPrinter ().writeValueAsString (pz );
108+ }
109+
110+ private static Predicate <Pizza > thatAreNotDelivered () {
111+ return new Predicate <Pizza >() {
112+ public boolean evaluate (Pizza entry ) {
113+ return undeliveredPizzaStatuses .contains (entry .getStatus ());
114+ }
115+ };
116+ }
117+ }
0 commit comments