BASE-4535: Example for overriding compareTo
This commit is contained in:
parent
e459c3faa0
commit
268fe2dfe6
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
public class BankAccount implements Comparable<BankAccount> {
|
||||
|
||||
private final int balance;
|
||||
|
||||
public BankAccount(int balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BankAccount anotherAccount) {
|
||||
return this.balance - anotherAccount.balance;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
public class BankAccountFix implements Comparable<BankAccountFix> {
|
||||
|
||||
private final int balance;
|
||||
|
||||
public BankAccountFix(int balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BankAccountFix anotherAccount) {
|
||||
return Integer.compare(this.balance, anotherAccount.balance);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
public class FootballPlayer implements Comparable<FootballPlayer> {
|
||||
|
||||
private final String name;
|
||||
private final int goalsScored;
|
||||
|
||||
public FootballPlayer(String name, int goalsScored) {
|
||||
this.name = name;
|
||||
this.goalsScored = goalsScored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(FootballPlayer anotherPlayer) {
|
||||
return this.goalsScored - anotherPlayer.goalsScored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (this == object)
|
||||
return true;
|
||||
if (object == null || getClass() != object.getClass())
|
||||
return false;
|
||||
FootballPlayer player = (FootballPlayer) object;
|
||||
return name.equals(player.name);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
public class HandballPlayer {
|
||||
|
||||
private final String name;
|
||||
private final int height;
|
||||
|
||||
public HandballPlayer(String name, int height) {
|
||||
this.name = name;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArraysSortingUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenArrayOfNumbers_whenSortingArray_thenNumbersAreSortedAscending() {
|
||||
int[] numbers = new int[] {5, 3, 9, 11, 1, 7};
|
||||
Arrays.sort(numbers);
|
||||
assertThat(numbers).containsExactly(1, 3, 5, 7, 9, 11);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfStrings_whenSortingArray_thenStringsAreSortedAlphabetically() {
|
||||
String[] players = new String[] {"ronaldo", "modric", "ramos", "messi"};
|
||||
Arrays.sort(players);
|
||||
assertThat(players).containsExactly("messi", "modric", "ramos", "ronaldo");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BankAccountFixUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenComparisonBasedImpl_whenUsingSmallIntegers_thenComparisonWorks() {
|
||||
BankAccountFix accountOne = new BankAccountFix(5000);
|
||||
BankAccountFix accountTwo = new BankAccountFix(1000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComparisonBasedImpl_whenUsingLargeIntegers_thenComparisonWorks() {
|
||||
BankAccountFix accountOne = new BankAccountFix(1900000000);
|
||||
BankAccountFix accountTwo = new BankAccountFix(-2000000000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isPositive();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class BankAccountUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSubtractionBasedImpl_whenUsingSmallIntegers_thenComparisonWorks() {
|
||||
BankAccount accountOne = new BankAccount(5000);
|
||||
BankAccount accountTwo = new BankAccount(1000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubtractionBasedImpl_whenUsingLargeIntegers_thenComparisonBreaks() {
|
||||
BankAccount accountOne = new BankAccount(1900000000);
|
||||
BankAccount accountTwo = new BankAccount(-2000000000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isNegative();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FootballPlayerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInconsistentCompareToAndEqualsImpl_whenUsingSortedSet_thenElementsAreNotAdded() {
|
||||
FootballPlayer messi = new FootballPlayer("Messi", 800);
|
||||
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 800);
|
||||
|
||||
TreeSet<FootballPlayer> set = new TreeSet<>();
|
||||
set.add(messi);
|
||||
set.add(ronaldo);
|
||||
|
||||
assertThat(set).hasSize(1);
|
||||
assertThat(set).doesNotContain(ronaldo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompareToImpl_whenSavingElementsInTreeMap_thenKeysAreSortedUsingCompareTo() {
|
||||
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 900);
|
||||
FootballPlayer messi = new FootballPlayer("Messi", 800);
|
||||
FootballPlayer modric = new FootballPlayer("modric", 100);
|
||||
|
||||
Map<FootballPlayer, String> players = new TreeMap<>();
|
||||
players.put(ronaldo, "forward");
|
||||
players.put(messi, "forward");
|
||||
players.put(modric, "midfielder");
|
||||
|
||||
assertThat(players.keySet()).containsExactly(modric, messi, ronaldo);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
public class HandballPlayerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenComparableIsNotImplemented_whenSortingArray_thenExceptionIsThrown() {
|
||||
HandballPlayer duvnjak = new HandballPlayer("Duvnjak", 197);
|
||||
HandballPlayer hansen = new HandballPlayer("Hansen", 196);
|
||||
|
||||
HandballPlayer[] players = new HandballPlayer[] {duvnjak, hansen};
|
||||
|
||||
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> Arrays.sort(players));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue