* BAEL-7853: Removed unnecessary dependencies

* BAEL-7853: Simple PriorityQueue tests

* BAEL-7853: Additional example with Meetings
This commit is contained in:
Eugene Kovko 2024-04-27 23:08:02 +02:00 committed by GitHub
parent eca1d4920a
commit c4b34c1793
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 280 additions and 12 deletions

View File

@ -14,24 +14,12 @@
</parent>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.roaringbitmap</groupId>
<artifactId>RoaringBitmap</artifactId>

View File

@ -0,0 +1,26 @@
package com.baeldung.priorityqueuewithpair;
public class Book {
private final String author;
private final String title;
private final int publicationYear;
public Book(String author, String title, int publicationYear) {
this.author = author;
this.title = title;
this.publicationYear = publicationYear;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public int getPublicationYear() {
return publicationYear;
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.priorityqueuewithpair;
import java.time.LocalDateTime;
import java.util.Objects;
public class Meeting implements Comparable<Meeting> {
private final LocalDateTime startTime;
private final LocalDateTime endTime;
private final String title;
public Meeting(LocalDateTime startTime, LocalDateTime endTime, String title) {
this.startTime = startTime;
this.endTime = endTime;
this.title = title;
}
public LocalDateTime getStartTime() {
return startTime;
}
public LocalDateTime getEndTime() {
return endTime;
}
public String getTitle() {
return title;
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
Meeting meeting = (Meeting) object;
if (!Objects.equals(startTime, meeting.startTime)) {
return false;
}
if (!Objects.equals(endTime, meeting.endTime)) {
return false;
}
return Objects.equals(title, meeting.title);
}
@Override
public int hashCode() {
int result = startTime != null ? startTime.hashCode() : 0;
result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
result = 31 * result + (title != null ? title.hashCode() : 0);
return result;
}
@Override
public int compareTo(Meeting meeting) {
return this.startTime.compareTo(meeting.startTime);
}
}

View File

@ -0,0 +1,191 @@
package com.baeldung.priorityqueuewithpair;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.time.LocalDateTime;
import java.util.AbstractMap;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.stream.Stream;
import org.apache.commons.math3.util.Pair;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
class PriorityQueueWithComplexObjectUnitTest {
@Test
void givenMeetings_whenUseWithPriorityQueue_thenSortByStartDateTime() {
Meeting projectDiscussion = new Meeting(
LocalDateTime.parse("2025-11-10T19:00:00"),
LocalDateTime.parse("2025-11-10T20:00:00"),
"Project Discussion"
);
Meeting businessMeeting = new Meeting(
LocalDateTime.parse("2025-11-15T14:00:00"),
LocalDateTime.parse("2025-11-15T16:00:00"),
"Business Meeting"
);
PriorityQueue<Meeting> meetings = new PriorityQueue<>();
meetings.add(projectDiscussion);
meetings.add(businessMeeting);
assertThat(meetings.poll()).isEqualTo(projectDiscussion);
assertThat(meetings.poll()).isEqualTo(businessMeeting);
}
@ParameterizedTest
@MethodSource("pairProvider")
void givenPairs_whenUsePriorityQueue_thenSortThemBySecondElement(List<Pair<String, Integer>> pairs) {
PriorityQueue<Pair<String, Integer>> queue = new PriorityQueue<>(Comparator.comparingInt(Pair::getSecond));
queue.addAll(pairs);
Pair<String, Integer> previousEntry = queue.poll();
while (!queue.isEmpty()) {
Pair<String, Integer> currentEntry = queue.poll();
assertThat(previousEntry.getSecond()).isLessThanOrEqualTo(currentEntry.getSecond());
previousEntry = currentEntry;
}
}
@ParameterizedTest
@MethodSource("mapEntryProvider")
void givenMapEntries_whenUsePriorityQueue_thenSortThemBySecondElement(List<Map.Entry<String, Integer>> pairs) {
PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue));
queue.addAll(pairs);
Map.Entry<String, Integer> previousEntry = queue.poll();
while (!queue.isEmpty()) {
Map.Entry<String, Integer> currentEntry = queue.poll();
assertThat(previousEntry.getValue()).isLessThanOrEqualTo(currentEntry.getValue());
previousEntry = currentEntry;
}
}
@ParameterizedTest
@MethodSource("bookProvider")
void givenBooks_whenUsePriorityQueue_thenSortThemBySecondElement(List<Book> books) {
PriorityQueue<Book> queue = new PriorityQueue<>(Comparator.comparingInt(Book::getPublicationYear));
queue.addAll(books);
Book previousBook = queue.poll();
while (!queue.isEmpty()) {
Book currentBook = queue.poll();
assertThat(previousBook.getPublicationYear())
.isLessThanOrEqualTo(currentBook.getPublicationYear());
previousBook = currentBook;
}
}
@ParameterizedTest
@MethodSource("bookProvider")
void givenBooks_whenUsePriorityQueueWithoutComparator_thenThrowClassCastExcetption(List<Book> books) {
PriorityQueue<Book> queue = new PriorityQueue<>();
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> queue.addAll(books));
}
@ParameterizedTest
@MethodSource("bookProvider")
void givenBooks_whenUsePriorityQueueWithoutComparatorWithoutAddingElements_thenNoExcetption(List<Book> books) {
PriorityQueue<Book> queue = new PriorityQueue<>();
assertThat(queue).isNotNull();
}
static Stream<List> pairProvider() {
return Stream.of(
List.of(
new Pair<>("Dune", 1965),
new Pair<>("Foundation", 1951),
new Pair<>("2001: A Space Odyssey", 1968),
new Pair<>("Do Androids Dream of Electric Sheep?", 1968),
new Pair<>("Ender's Game", 1985),
new Pair<>("The Hitchhiker's Guide to the Galaxy", 1979),
new Pair<>("1984", 1949),
new Pair<>("Brave New World", 1932),
new Pair<>("Fahrenheit 451", 1953),
new Pair<>("Neuromancer", 1984),
new Pair<>("Stranger in a Strange Land", 1961),
new Pair<>("The Handmaid's Tale", 1985),
new Pair<>("Ubik", 1969),
new Pair<>("Solaris", 1961),
new Pair<>("Slaughterhouse-Five", 1969),
new Pair<>("The War of the Worlds", 1898),
new Pair<>("Childhood's End", 1953),
new Pair<>("Snow Crash", 1992),
new Pair<>("The Moon is a Harsh Mistress", 1966),
new Pair<>("Hyperion", 1989),
new Pair<>("The Left Hand of Darkness", 1969),
new Pair<>("The Gods Themselves", 1972),
new Pair<>("The Stars My Destination", 1956),
new Pair<>("Rendezvous with Rama", 1973),
new Pair<>("The Forever War", 1974)
)
);
}
static Stream<List> mapEntryProvider() {
return Stream.of(
List.of(
new AbstractMap.SimpleEntry<>("Dune", 1965),
new AbstractMap.SimpleEntry<>("Foundation", 1951),
new AbstractMap.SimpleEntry<>("2001: A Space Odyssey", 1968),
new AbstractMap.SimpleEntry<>("Do Androids Dream of Electric Sheep?", 1968),
new AbstractMap.SimpleEntry<>("Ender's Game", 1985),
new AbstractMap.SimpleEntry<>("The Hitchhiker's Guide to the Galaxy", 1979),
new AbstractMap.SimpleEntry<>("1984", 1949),
new AbstractMap.SimpleEntry<>("Brave New World", 1932),
new AbstractMap.SimpleEntry<>("Fahrenheit 451", 1953),
new AbstractMap.SimpleEntry<>("Neuromancer", 1984),
new AbstractMap.SimpleEntry<>("Stranger in a Strange Land", 1961),
new AbstractMap.SimpleEntry<>("The Handmaid's Tale", 1985),
new AbstractMap.SimpleEntry<>("Ubik", 1969),
new AbstractMap.SimpleEntry<>("Solaris", 1961),
new AbstractMap.SimpleEntry<>("Slaughterhouse-Five", 1969),
new AbstractMap.SimpleEntry<>("The War of the Worlds", 1898),
new AbstractMap.SimpleEntry<>("Childhood's End", 1953),
new AbstractMap.SimpleEntry<>("Snow Crash", 1992),
new AbstractMap.SimpleEntry<>("The Moon is a Harsh Mistress", 1966),
new AbstractMap.SimpleEntry<>("Hyperion", 1989),
new AbstractMap.SimpleEntry<>("The Left Hand of Darkness", 1969),
new AbstractMap.SimpleEntry<>("The Gods Themselves", 1972),
new AbstractMap.SimpleEntry<>("The Stars My Destination", 1956),
new AbstractMap.SimpleEntry<>("Rendezvous with Rama", 1973),
new AbstractMap.SimpleEntry<>("The Forever War", 1974)
)
);
}
static Stream<List<Book>> bookProvider() {
return Stream.of(
List.of(
new Book("Frank Herbert", "Dune", 1965),
new Book("Isaac Asimov", "Foundation", 1951),
new Book("Arthur C. Clarke", "2001: A Space Odyssey", 1968),
new Book("Philip K. Dick", "Do Androids Dream of Electric Sheep?", 1968),
new Book("Orson Scott Card", "Ender's Game", 1985),
new Book("Douglas Adams", "The Hitchhiker's Guide to the Galaxy", 1979),
new Book("George Orwell", "1984", 1949),
new Book("Aldous Huxley", "Brave New World", 1932),
new Book("Ray Bradbury", "Fahrenheit 451", 1953),
new Book("William Gibson", "Neuromancer", 1984),
new Book("Robert A. Heinlein", "Stranger in a Strange Land", 1961),
new Book("Margaret Atwood", "The Handmaid's Tale", 1985),
new Book("Philip K. Dick", "Ubik", 1969),
new Book("Stanislaw Lem", "Solaris", 1961),
new Book("Kurt Vonnegut", "Slaughterhouse-Five", 1969),
new Book("H.G. Wells", "The War of the Worlds", 1898),
new Book("Arthur C. Clarke", "Childhood's End", 1953),
new Book("Neal Stephenson", "Snow Crash", 1992),
new Book("Robert A. Heinlein", "The Moon is a Harsh Mistress", 1966),
new Book("Dan Simmons", "Hyperion", 1989),
new Book("Ursula K. Le Guin", "The Left Hand of Darkness", 1969),
new Book("Isaac Asimov", "The Gods Themselves", 1972),
new Book("Alfred Bester", "The Stars My Destination", 1956),
new Book("Arthur C. Clarke", "Rendezvous with Rama", 1973),
new Book("Joe Haldeman", "The Forever War", 1974)
)
);
}
}