Skip to content

Commit 51b6942

Browse files
authored
Merge pull request eugenp#10443 from dstr89/feature/BAEL-4535-overriding-compareTo
Feature/bael 4535 overriding compare to
2 parents 79420a9 + cba9eed commit 51b6942

9 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.compareto;
2+
3+
public class BankAccount implements Comparable<BankAccount> {
4+
5+
private final int balance;
6+
7+
public BankAccount(int balance) {
8+
this.balance = balance;
9+
}
10+
11+
@Override
12+
public int compareTo(BankAccount anotherAccount) {
13+
return this.balance - anotherAccount.balance;
14+
}
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.compareto;
2+
3+
public class BankAccountFix implements Comparable<BankAccountFix> {
4+
5+
private final int balance;
6+
7+
public BankAccountFix(int balance) {
8+
this.balance = balance;
9+
}
10+
11+
@Override
12+
public int compareTo(BankAccountFix anotherAccount) {
13+
return Integer.compare(this.balance, anotherAccount.balance);
14+
}
15+
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.compareto;
2+
3+
public class FootballPlayer implements Comparable<FootballPlayer> {
4+
5+
private final String name;
6+
private final int goalsScored;
7+
8+
public FootballPlayer(String name, int goalsScored) {
9+
this.name = name;
10+
this.goalsScored = goalsScored;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
@Override
18+
public int compareTo(FootballPlayer anotherPlayer) {
19+
return Integer.compare(this.goalsScored, anotherPlayer.goalsScored);
20+
}
21+
22+
@Override
23+
public boolean equals(Object object) {
24+
if (this == object)
25+
return true;
26+
if (object == null || getClass() != object.getClass())
27+
return false;
28+
FootballPlayer player = (FootballPlayer) object;
29+
return name.equals(player.name);
30+
}
31+
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.compareto;
2+
3+
public class HandballPlayer {
4+
5+
private final String name;
6+
private final int height;
7+
8+
public HandballPlayer(String name, int height) {
9+
this.name = name;
10+
this.height = height;
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.compareto;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class ArraysSortingUnitTest {
10+
11+
@Test
12+
public void givenArrayOfNumbers_whenSortingArray_thenNumbersAreSortedAscending() {
13+
int[] numbers = new int[] {5, 3, 9, 11, 1, 7};
14+
Arrays.sort(numbers);
15+
assertThat(numbers).containsExactly(1, 3, 5, 7, 9, 11);
16+
}
17+
18+
@Test
19+
public void givenArrayOfStrings_whenSortingArray_thenStringsAreSortedAlphabetically() {
20+
String[] players = new String[] {"ronaldo", "modric", "ramos", "messi"};
21+
Arrays.sort(players);
22+
assertThat(players).containsExactly("messi", "modric", "ramos", "ronaldo");
23+
}
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.compareto;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class BankAccountFixUnitTest {
8+
9+
@Test
10+
public void givenComparisonBasedImpl_whenUsingSmallIntegers_thenComparisonWorks() {
11+
BankAccountFix accountOne = new BankAccountFix(5000);
12+
BankAccountFix accountTwo = new BankAccountFix(1000);
13+
int comparison = accountOne.compareTo(accountTwo);
14+
assertThat(comparison).isPositive();
15+
}
16+
17+
@Test
18+
public void givenComparisonBasedImpl_whenUsingLargeIntegers_thenComparisonWorks() {
19+
BankAccountFix accountOne = new BankAccountFix(1900000000);
20+
BankAccountFix accountTwo = new BankAccountFix(-2000000000);
21+
int comparison = accountOne.compareTo(accountTwo);
22+
assertThat(comparison).isPositive();
23+
}
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.compareto;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
public class BankAccountUnitTest {
8+
9+
@Test
10+
public void givenSubtractionBasedImpl_whenUsingSmallIntegers_thenComparisonWorks() {
11+
BankAccount accountOne = new BankAccount(5000);
12+
BankAccount accountTwo = new BankAccount(1000);
13+
int comparison = accountOne.compareTo(accountTwo);
14+
assertThat(comparison).isPositive();
15+
}
16+
17+
@Test
18+
public void givenSubtractionBasedImpl_whenUsingLargeIntegers_thenComparisonBreaks() {
19+
BankAccount accountOne = new BankAccount(1900000000);
20+
BankAccount accountTwo = new BankAccount(-2000000000);
21+
int comparison = accountOne.compareTo(accountTwo);
22+
assertThat(comparison).isNegative();
23+
}
24+
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.compareto;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.TreeMap;
11+
import java.util.TreeSet;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class FootballPlayerUnitTest {
16+
17+
@Test
18+
public void givenInconsistentCompareToAndEqualsImpl_whenUsingSortedSet_thenSomeElementsAreNotAdded() {
19+
FootballPlayer messi = new FootballPlayer("Messi", 800);
20+
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 800);
21+
22+
TreeSet<FootballPlayer> set = new TreeSet<>();
23+
set.add(messi);
24+
set.add(ronaldo);
25+
26+
assertThat(set).hasSize(1);
27+
assertThat(set).doesNotContain(ronaldo);
28+
}
29+
30+
@Test
31+
public void givenCompareToImpl_whenUsingCustomComparator_thenComparatorLogicIsApplied() {
32+
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 900);
33+
FootballPlayer messi = new FootballPlayer("Messi", 800);
34+
FootballPlayer modric = new FootballPlayer("Modric", 100);
35+
36+
List<FootballPlayer> players = Arrays.asList(ronaldo, messi, modric);
37+
Comparator<FootballPlayer> nameComparator = Comparator.comparing(FootballPlayer::getName);
38+
Collections.sort(players, nameComparator);
39+
40+
assertThat(players).containsExactly(messi, modric, ronaldo);
41+
}
42+
43+
@Test
44+
public void givenCompareToImpl_whenSavingElementsInTreeMap_thenKeysAreSortedUsingCompareTo() {
45+
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 900);
46+
FootballPlayer messi = new FootballPlayer("Messi", 800);
47+
FootballPlayer modric = new FootballPlayer("Modric", 100);
48+
49+
Map<FootballPlayer, String> players = new TreeMap<>();
50+
players.put(ronaldo, "forward");
51+
players.put(messi, "forward");
52+
players.put(modric, "midfielder");
53+
54+
assertThat(players.keySet()).containsExactly(modric, messi, ronaldo);
55+
}
56+
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.compareto;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
7+
8+
public class HandballPlayerUnitTest {
9+
10+
@Test
11+
public void givenComparableIsNotImplemented_whenSortingArray_thenExceptionIsThrown() {
12+
HandballPlayer duvnjak = new HandballPlayer("Duvnjak", 197);
13+
HandballPlayer hansen = new HandballPlayer("Hansen", 196);
14+
15+
HandballPlayer[] players = new HandballPlayer[] {duvnjak, hansen};
16+
17+
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> Arrays.sort(players));
18+
}
19+
20+
}

0 commit comments

Comments
 (0)