Merge pull request #432 from PriyaKrishna/master
Core java enum examples
This commit is contained in:
commit
c8a8132d06
|
@ -0,0 +1,84 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Pizza {
|
||||
|
||||
private static EnumSet<PizzaStatusEnum> deliveredPizzaStatuses =
|
||||
EnumSet.of(PizzaStatusEnum.DELIVERED);
|
||||
|
||||
private PizzaStatusEnum status;
|
||||
|
||||
public enum PizzaStatusEnum {
|
||||
ORDERED (5){
|
||||
@Override
|
||||
public boolean isOrdered() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
READY (2){
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
DELIVERED (0){
|
||||
@Override
|
||||
public boolean isDelivered() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private int timeToDelivery;
|
||||
|
||||
public boolean isOrdered() {return false;}
|
||||
|
||||
public boolean isReady() {return false;}
|
||||
|
||||
public boolean isDelivered(){return false;}
|
||||
public int getTimeToDelivery() {
|
||||
return timeToDelivery;
|
||||
}
|
||||
|
||||
private PizzaStatusEnum (int timeToDelivery) {
|
||||
this.timeToDelivery = timeToDelivery;
|
||||
}
|
||||
}
|
||||
|
||||
public PizzaStatusEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(PizzaStatusEnum status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public boolean isDeliverable() {
|
||||
return this.status.isReady();
|
||||
}
|
||||
|
||||
public void printTimeToDeliver() {
|
||||
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
|
||||
}
|
||||
|
||||
public static List<Pizza> getAllUndeliveredPizzas(List<Pizza> input) {
|
||||
return input.stream().filter((s) -> !deliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pzList) {
|
||||
EnumMap<PizzaStatusEnum, List<Pizza>> map = pzList.stream().collect(
|
||||
Collectors.groupingBy(Pizza::getStatus,
|
||||
() -> new EnumMap<PizzaStatusEnum, List<Pizza>>(PizzaStatusEnum.class), Collectors.toList()));
|
||||
return map;
|
||||
}
|
||||
|
||||
public void deliver() {
|
||||
if (isDeliverable()) {
|
||||
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
|
||||
this.setStatus(PizzaStatusEnum.DELIVERED);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
public enum PizzaDeliveryStrategy {
|
||||
EXPRESS {
|
||||
@Override
|
||||
public void deliver(Pizza pz) {
|
||||
System.out.println("Pizza will be delivered in express mode");
|
||||
}
|
||||
},
|
||||
NORMAL {
|
||||
@Override
|
||||
public void deliver(Pizza pz) {
|
||||
System.out.println("Pizza will be delivered in normal mode");
|
||||
}
|
||||
};
|
||||
|
||||
public abstract void deliver(Pizza pz);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
|
||||
public enum PizzaDeliverySystemConfiguration {
|
||||
INSTANCE ;
|
||||
private PizzaDeliverySystemConfiguration() {
|
||||
//Do the configuration initialization which
|
||||
// involves overriding defaults like delivery strategy
|
||||
}
|
||||
|
||||
private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL;
|
||||
|
||||
public static PizzaDeliverySystemConfiguration getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public PizzaDeliveryStrategy getDeliveryStrategy() {
|
||||
return deliveryStrategy;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
public class PizzaTest {
|
||||
@Test
|
||||
public void givenPizaOrder_whenReady_thenDeliverable() {
|
||||
Pizza testPz = new Pizza();
|
||||
testPz.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
assertTrue(testPz.isDeliverable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() {
|
||||
List<Pizza> pzList = new ArrayList<Pizza>();
|
||||
Pizza pz1 = new Pizza();
|
||||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED);
|
||||
|
||||
Pizza pz2 = new Pizza();
|
||||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz3 = new Pizza();
|
||||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz4 = new Pizza();
|
||||
pz4.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
|
||||
pzList.add(pz1);
|
||||
pzList.add(pz2);
|
||||
pzList.add(pz3);
|
||||
pzList.add(pz4);
|
||||
|
||||
List<Pizza> undeliveredPzs = Pizza.getAllUndeliveredPizzas(pzList);
|
||||
assertTrue(undeliveredPzs.size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() {
|
||||
|
||||
List<Pizza> pzList = new ArrayList<Pizza>();
|
||||
Pizza pz1 = new Pizza();
|
||||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED);
|
||||
|
||||
Pizza pz2 = new Pizza();
|
||||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz3 = new Pizza();
|
||||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz4 = new Pizza();
|
||||
pz4.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
|
||||
pzList.add(pz1);
|
||||
pzList.add(pz2);
|
||||
pzList.add(pz3);
|
||||
pzList.add(pz4);
|
||||
|
||||
EnumMap<Pizza.PizzaStatusEnum, List<Pizza>> map = Pizza.groupPizzaByStatus(pzList);
|
||||
assertTrue(map.get(Pizza.PizzaStatusEnum.DELIVERED).size() == 1);
|
||||
assertTrue(map.get(Pizza.PizzaStatusEnum.ORDERED).size() == 2);
|
||||
assertTrue(map.get(Pizza.PizzaStatusEnum.READY).size() == 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() {
|
||||
Pizza pz = new Pizza();
|
||||
pz.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
pz.deliver();
|
||||
assertTrue(pz.getStatus() == Pizza.PizzaStatusEnum.DELIVERED);
|
||||
}
|
||||
}
|
|
@ -9,7 +9,11 @@
|
|||
<dependencies>
|
||||
|
||||
<!-- utils -->
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>4.01</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.collections15.CollectionUtils;
|
||||
import org.apache.commons.collections15.Predicate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
public class Pizza {
|
||||
|
||||
private static EnumSet<PizzaStatusEnum> undeliveredPizzaStatuses =
|
||||
EnumSet.of(PizzaStatusEnum.ORDERED, PizzaStatusEnum.READY);
|
||||
|
||||
private PizzaStatusEnum status;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||
public enum PizzaStatusEnum {
|
||||
ORDERED (5){
|
||||
@Override
|
||||
public boolean isOrdered() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
READY (2){
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
},
|
||||
DELIVERED (0){
|
||||
@Override
|
||||
public boolean isDelivered() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
private int timeToDelivery;
|
||||
|
||||
public boolean isOrdered() {return false;}
|
||||
|
||||
public boolean isReady() {return false;}
|
||||
|
||||
public boolean isDelivered(){return false;}
|
||||
@JsonProperty("timeToDelivery")
|
||||
public int getTimeToDelivery() {
|
||||
return timeToDelivery;
|
||||
}
|
||||
|
||||
private PizzaStatusEnum (int timeToDelivery) {
|
||||
this.timeToDelivery = timeToDelivery;
|
||||
}
|
||||
}
|
||||
|
||||
public PizzaStatusEnum getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(PizzaStatusEnum status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public boolean isDeliverable() {
|
||||
return this.status.isReady();
|
||||
}
|
||||
|
||||
public void printTimeToDeliver() {
|
||||
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
|
||||
}
|
||||
|
||||
public static List<Pizza> getAllUndeliveredPizza(List<Pizza> input) {
|
||||
List<Pizza> undelivered = input;
|
||||
CollectionUtils.filter(undelivered, thatAreNotDelivered());
|
||||
return undelivered;
|
||||
}
|
||||
|
||||
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pizzaList) {
|
||||
EnumMap<PizzaStatusEnum, List<Pizza>> pzByStatus = new EnumMap<PizzaStatusEnum, List<Pizza>>(PizzaStatusEnum.class);
|
||||
for (Pizza pz : pizzaList) {
|
||||
PizzaStatusEnum status = pz.getStatus();
|
||||
|
||||
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() {
|
||||
if (isDeliverable()) {
|
||||
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
|
||||
this.setStatus(PizzaStatusEnum.DELIVERED);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getJsonString(Pizza pz) throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pz);
|
||||
}
|
||||
|
||||
private static Predicate<Pizza> thatAreNotDelivered() {
|
||||
return new Predicate<Pizza>() {
|
||||
public boolean evaluate(Pizza entry) {
|
||||
return undeliveredPizzaStatuses.contains(entry.getStatus());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
public enum PizzaDeliveryStrategy {
|
||||
EXPRESS {
|
||||
@Override
|
||||
public void deliver(Pizza pz) {
|
||||
System.out.println("Pizza will be delivered in express mode");
|
||||
}
|
||||
},
|
||||
NORMAL {
|
||||
@Override
|
||||
public void deliver(Pizza pz) {
|
||||
System.out.println("Pizza will be delivered in normal mode");
|
||||
}
|
||||
};
|
||||
|
||||
public abstract void deliver(Pizza pz);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.enums;
|
||||
|
||||
public enum PizzaDeliverySystemConfiguration {
|
||||
INSTANCE ;
|
||||
private PizzaDeliverySystemConfiguration() {
|
||||
//Do the configuration initialization which
|
||||
// involves overriding defaults like delivery strategy
|
||||
}
|
||||
|
||||
private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL;
|
||||
|
||||
public static PizzaDeliverySystemConfiguration getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public PizzaDeliveryStrategy getDeliveryStrategy() {
|
||||
return deliveryStrategy;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package org.baeldung.java.enums;
|
||||
|
||||
|
||||
import com.baeldung.enums.Pizza;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
|
||||
public class PizzaTest {
|
||||
@Test
|
||||
public void givenPizaOrder_whenReady_thenDeliverable() {
|
||||
Pizza testPz = new Pizza();
|
||||
testPz.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
assertTrue(testPz.isDeliverable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrders_whenRetrievingUnDeliveredPzs_thenCorrectlyRetrieved() {
|
||||
List<Pizza> pzList = new ArrayList<Pizza>();
|
||||
Pizza pz1 = new Pizza();
|
||||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED);
|
||||
|
||||
Pizza pz2 = new Pizza();
|
||||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz3 = new Pizza();
|
||||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz4 = new Pizza();
|
||||
pz4.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
|
||||
pzList.add(pz1);
|
||||
pzList.add(pz2);
|
||||
pzList.add(pz3);
|
||||
pzList.add(pz4);
|
||||
|
||||
List<Pizza> undeliveredPzs = Pizza.getAllUndeliveredPizza(pzList);
|
||||
assertTrue(undeliveredPzs.size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrders_whenGroupByStatusCalled_thenCorrectlyGrouped() {
|
||||
|
||||
List<Pizza> pzList = new ArrayList<Pizza>();
|
||||
Pizza pz1 = new Pizza();
|
||||
pz1.setStatus(Pizza.PizzaStatusEnum.DELIVERED);
|
||||
|
||||
Pizza pz2 = new Pizza();
|
||||
pz2.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz3 = new Pizza();
|
||||
pz3.setStatus(Pizza.PizzaStatusEnum.ORDERED);
|
||||
|
||||
Pizza pz4 = new Pizza();
|
||||
pz4.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
|
||||
pzList.add(pz1);
|
||||
pzList.add(pz2);
|
||||
pzList.add(pz3);
|
||||
pzList.add(pz4);
|
||||
|
||||
EnumMap<Pizza.PizzaStatusEnum, List<Pizza>> map = Pizza.groupPizzaByStatus(pzList);
|
||||
assertTrue(map.get(Pizza.PizzaStatusEnum.DELIVERED).size() == 1);
|
||||
assertTrue(map.get(Pizza.PizzaStatusEnum.ORDERED).size() == 2);
|
||||
assertTrue(map.get(Pizza.PizzaStatusEnum.READY).size() == 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() {
|
||||
Pizza pz = new Pizza();
|
||||
pz.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
pz.deliver();
|
||||
assertTrue(pz.getStatus() == Pizza.PizzaStatusEnum.DELIVERED);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue