Merge pull request #10443 from dstr89/feature/BAEL-4535-overriding-compareTo

Feature/bael 4535 overriding compare to
This commit is contained in:
Jonathan Cook 2021-01-31 16:42:48 +01:00 committed by GitHub
commit 9ea990660e
9 changed files with 228 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,32 @@
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;
}
public String getName() {
return name;
}
@Override
public int compareTo(FootballPlayer anotherPlayer) {
return Integer.compare(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);
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.compareto;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
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_thenSomeElementsAreNotAdded() {
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_whenUsingCustomComparator_thenComparatorLogicIsApplied() {
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 900);
FootballPlayer messi = new FootballPlayer("Messi", 800);
FootballPlayer modric = new FootballPlayer("Modric", 100);
List<FootballPlayer> players = Arrays.asList(ronaldo, messi, modric);
Comparator<FootballPlayer> nameComparator = Comparator.comparing(FootballPlayer::getName);
Collections.sort(players, nameComparator);
assertThat(players).containsExactly(messi, modric, 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);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.compareto;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
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));
}
}