Skip to content

Commit fd27912

Browse files
committed
Merge pull request #1 from eugenp/master
Merging latest changes
2 parents b1eab75 + c8a8132 commit fd27912

60 files changed

Lines changed: 1979 additions & 168 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.enums;
2+
3+
import java.util.EnumMap;
4+
import java.util.EnumSet;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
public class Pizza {
9+
10+
private static EnumSet<PizzaStatusEnum> deliveredPizzaStatuses =
11+
EnumSet.of(PizzaStatusEnum.DELIVERED);
12+
13+
private PizzaStatusEnum status;
14+
15+
public enum PizzaStatusEnum {
16+
ORDERED (5){
17+
@Override
18+
public boolean isOrdered() {
19+
return true;
20+
}
21+
},
22+
READY (2){
23+
@Override
24+
public boolean isReady() {
25+
return true;
26+
}
27+
},
28+
DELIVERED (0){
29+
@Override
30+
public boolean isDelivered() {
31+
return true;
32+
}
33+
};
34+
35+
private int timeToDelivery;
36+
37+
public boolean isOrdered() {return false;}
38+
39+
public boolean isReady() {return false;}
40+
41+
public boolean isDelivered(){return false;}
42+
public int getTimeToDelivery() {
43+
return timeToDelivery;
44+
}
45+
46+
private PizzaStatusEnum (int timeToDelivery) {
47+
this.timeToDelivery = timeToDelivery;
48+
}
49+
}
50+
51+
public PizzaStatusEnum getStatus() {
52+
return status;
53+
}
54+
55+
public void setStatus(PizzaStatusEnum status) {
56+
this.status = status;
57+
}
58+
59+
public boolean isDeliverable() {
60+
return this.status.isReady();
61+
}
62+
63+
public void printTimeToDeliver() {
64+
System.out.println("Time to delivery is " + this.getStatus().getTimeToDelivery() + " days");
65+
}
66+
67+
public static List<Pizza> getAllUndeliveredPizzas(List<Pizza> input) {
68+
return input.stream().filter((s) -> !deliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList());
69+
}
70+
71+
public static EnumMap<PizzaStatusEnum, List<Pizza>> groupPizzaByStatus(List<Pizza> pzList) {
72+
EnumMap<PizzaStatusEnum, List<Pizza>> map = pzList.stream().collect(
73+
Collectors.groupingBy(Pizza::getStatus,
74+
() -> new EnumMap<PizzaStatusEnum, List<Pizza>>(PizzaStatusEnum.class), Collectors.toList()));
75+
return map;
76+
}
77+
78+
public void deliver() {
79+
if (isDeliverable()) {
80+
PizzaDeliverySystemConfiguration.getInstance().getDeliveryStrategy().deliver(this);
81+
this.setStatus(PizzaStatusEnum.DELIVERED);
82+
}
83+
}
84+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.enums;
2+
3+
public enum PizzaDeliveryStrategy {
4+
EXPRESS {
5+
@Override
6+
public void deliver(Pizza pz) {
7+
System.out.println("Pizza will be delivered in express mode");
8+
}
9+
},
10+
NORMAL {
11+
@Override
12+
public void deliver(Pizza pz) {
13+
System.out.println("Pizza will be delivered in normal mode");
14+
}
15+
};
16+
17+
public abstract void deliver(Pizza pz);
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.enums;
2+
3+
4+
public enum PizzaDeliverySystemConfiguration {
5+
INSTANCE ;
6+
private PizzaDeliverySystemConfiguration() {
7+
//Do the configuration initialization which
8+
// involves overriding defaults like delivery strategy
9+
}
10+
11+
private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL;
12+
13+
public static PizzaDeliverySystemConfiguration getInstance() {
14+
return INSTANCE;
15+
}
16+
17+
public PizzaDeliveryStrategy getDeliveryStrategy() {
18+
return deliveryStrategy;
19+
}
20+
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.java_8_features;
2+
3+
public class Address {
4+
5+
private String street;
6+
7+
public String getStreet() {
8+
return street;
9+
}
10+
11+
public void setStreet(String street) {
12+
this.street = street;
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.java_8_features;
2+
3+
public class CustomException extends RuntimeException {
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.java_8_features;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Detail {
7+
8+
private static final List<String> PARTS = Arrays.asList("turbine", "pump");
9+
10+
public List<String> getParts() {
11+
return PARTS;
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.java_8_features;
2+
3+
import java.util.Optional;
4+
5+
public class OptionalAddress {
6+
7+
private String street;
8+
9+
public Optional<String> getStreet() {
10+
return Optional.ofNullable(street);
11+
}
12+
13+
public void setStreet(String street) {
14+
this.street = street;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.java_8_features;
2+
3+
import java.util.Optional;
4+
5+
public class OptionalUser {
6+
7+
private OptionalAddress address;
8+
9+
public Optional<OptionalAddress> getAddress() {
10+
return Optional.of(address);
11+
}
12+
13+
public void setAddress(OptionalAddress address) {
14+
this.address = address;
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.java_8_features;
2+
3+
import java.util.Optional;
4+
5+
public class User {
6+
7+
private String name;
8+
9+
private Address address;
10+
11+
public Address getAddress() {
12+
return address;
13+
}
14+
15+
public void setAddress(Address address) {
16+
this.address = address;
17+
}
18+
19+
public User() {
20+
}
21+
22+
public User(String name) {
23+
this.name = name;
24+
}
25+
26+
public static boolean isRealUser(User user) {
27+
return true;
28+
}
29+
30+
public String getOrThrow() {
31+
String value = null;
32+
Optional<String> valueOpt = Optional.ofNullable(value);
33+
String result = valueOpt.orElseThrow(CustomException::new).toUpperCase();
34+
return result;
35+
}
36+
37+
public boolean isLegalName(String name) {
38+
return name.length() > 3 && name.length() < 16;
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.java_8_features;
2+
3+
public interface Vehicle {
4+
5+
void moveTo(long altitude, long longitude);
6+
7+
static String producer() {
8+
return "N&F Vehicles";
9+
}
10+
11+
default long[] startPosition() {
12+
return new long[]{23, 15};
13+
}
14+
15+
default String getOverview() {
16+
return "ATV made by " + producer();
17+
}
18+
}

0 commit comments

Comments
 (0)