Merge branch 'master' into BAEL-4300
This commit is contained in:
@@ -11,5 +11,6 @@ This module contains articles about core features in the Java language
|
||||
- [Guide to the Java finally Keyword](https://www.baeldung.com/java-finally-keyword)
|
||||
- [The Java Headless Mode](https://www.baeldung.com/java-headless-mode)
|
||||
- [Comparing Long Values in Java](https://www.baeldung.com/java-compare-long-values)
|
||||
|
||||
- [Comparing Objects in Java](https://www.baeldung.com/java-comparing-objects)
|
||||
- [Casting int to Enum in Java](https://www.baeldung.com/java-cast-int-to-enum)
|
||||
[[ <-- Prev]](/core-java-modules/core-java-lang)[[Next --> ]](/core-java-modules/core-java-lang-3)
|
||||
|
||||
@@ -22,15 +22,15 @@ public class PersonWithEquals {
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String firstName() {
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String lastName() {
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate birthDate() {
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,18 @@ public class PersonWithEqualsAndComparable implements Comparable<PersonWithEqual
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
@@ -23,15 +23,15 @@ public class PersonWithEqualsAndComparableUsingComparator implements Comparable<
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String firstName() {
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String lastName() {
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate birthDate() {
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
@@ -52,9 +52,9 @@ public class PersonWithEqualsAndComparableUsingComparator implements Comparable<
|
||||
|
||||
@Override
|
||||
public int compareTo(PersonWithEqualsAndComparableUsingComparator o) {
|
||||
return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::lastName)
|
||||
.thenComparing(PersonWithEqualsAndComparableUsingComparator::firstName)
|
||||
.thenComparing(PersonWithEqualsAndComparableUsingComparator::birthDate, Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
return Comparator.comparing(PersonWithEqualsAndComparableUsingComparator::getLastName)
|
||||
.thenComparing(PersonWithEqualsAndComparableUsingComparator::getFirstName)
|
||||
.thenComparing(PersonWithEqualsAndComparableUsingComparator::getBirthDate, Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
.compare(this, o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,18 @@ public class PersonWithEqualsAndWrongComparable implements Comparable<PersonWith
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public LocalDate getBirthDate() {
|
||||
return birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
@@ -8,4 +8,12 @@ public class PersonWithoutEquals {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.inttoenum;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum PizzaStatus {
|
||||
ORDERED(5),
|
||||
READY(2),
|
||||
DELIVERED(0);
|
||||
|
||||
private int timeToDelivery;
|
||||
|
||||
PizzaStatus(int timeToDelivery) {
|
||||
this.timeToDelivery = timeToDelivery;
|
||||
}
|
||||
|
||||
public int getTimeToDelivery() {
|
||||
return timeToDelivery;
|
||||
}
|
||||
|
||||
private static Map<Integer, PizzaStatus> timeToDeliveryToEnumValuesMapping = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (PizzaStatus pizzaStatus : PizzaStatus.values()) {
|
||||
timeToDeliveryToEnumValuesMapping.put(
|
||||
pizzaStatus.getTimeToDelivery(),
|
||||
pizzaStatus
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static PizzaStatus castIntToEnum(int timeToDelivery) {
|
||||
return timeToDeliveryToEnumValuesMapping.get(timeToDelivery);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class ComparatorInterfaceUnitTest {
|
||||
Comparator<PersonWithEquals> compareByFirstNames = new Comparator<PersonWithEquals>() {
|
||||
@Override
|
||||
public int compare(PersonWithEquals o1, PersonWithEquals o2) {
|
||||
return o1.firstName().compareTo(o2.firstName());
|
||||
return o1.getFirstName().compareTo(o2.getFirstName());
|
||||
}
|
||||
};
|
||||
people.sort(compareByFirstNames);
|
||||
@@ -37,7 +37,7 @@ class ComparatorInterfaceUnitTest {
|
||||
people.add(joe);
|
||||
people.add(allan);
|
||||
|
||||
Comparator<PersonWithEquals> compareByFirstNames = Comparator.comparing(PersonWithEquals::firstName);
|
||||
Comparator<PersonWithEquals> compareByFirstNames = Comparator.comparing(PersonWithEquals::getFirstName);
|
||||
people.sort(compareByFirstNames);
|
||||
|
||||
assertThat(people).containsExactly(allan, joe);
|
||||
|
||||
@@ -63,8 +63,8 @@ class GuavaUnitTest {
|
||||
PersonWithEquals joe = new PersonWithEquals("Joe", "Portman");
|
||||
|
||||
int comparisonResult = ComparisonChain.start()
|
||||
.compare(natalie.lastName(), joe.lastName())
|
||||
.compare(natalie.firstName(), joe.firstName())
|
||||
.compare(natalie.getLastName(), joe.getLastName())
|
||||
.compare(natalie.getFirstName(), joe.getFirstName())
|
||||
.result();
|
||||
|
||||
assertThat(comparisonResult).isPositive();
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.inttoenum;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IntToEnumUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenIntToEnumUsingValuesMethod_thenReturnEnumObject() {
|
||||
int timeToDeliveryForOrderedPizzaStatus = 5;
|
||||
|
||||
PizzaStatus pizzaOrderedStatus = null;
|
||||
|
||||
for (PizzaStatus pizzaStatus : PizzaStatus.values()) {
|
||||
if (pizzaStatus.getTimeToDelivery() == timeToDeliveryForOrderedPizzaStatus) {
|
||||
pizzaOrderedStatus = pizzaStatus;
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(pizzaOrderedStatus).isEqualTo(PizzaStatus.ORDERED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIntToEnumUsingMap_thenReturnEnumObject() {
|
||||
int timeToDeliveryForOrderedPizzaStatus = 5;
|
||||
|
||||
assertThat(PizzaStatus.castIntToEnum(timeToDeliveryForOrderedPizzaStatus)).isEqualTo(PizzaStatus.ORDERED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIntToEnumUsingStream_thenReturnEnumObject() {
|
||||
int timeToDeliveryForOrderedPizzaStatus = 5;
|
||||
|
||||
Optional<PizzaStatus> pizzaStatus = Arrays.stream(PizzaStatus.values())
|
||||
.filter(p -> p.getTimeToDelivery() == timeToDeliveryForOrderedPizzaStatus)
|
||||
.findFirst();
|
||||
|
||||
assertThat(pizzaStatus).hasValue(PizzaStatus.ORDERED);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user