1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-09-10 06:45:59 +00:00

Add PlayQueueItem equals and hashCode

This commit is contained in:
Isira Seneviratne
2025-06-11 06:19:52 +05:30
parent 840084d2c9
commit 690af88db9
5 changed files with 40 additions and 39 deletions

View File

@@ -852,8 +852,7 @@ public final class VideoDetailFragment
if (playQueue == null) { if (playQueue == null) {
playQueue = new SinglePlayQueue(result); playQueue = new SinglePlayQueue(result);
} }
if (stack.isEmpty() || !stack.peek().getPlayQueue() if (stack.isEmpty() || !stack.peek().getPlayQueue().equals(playQueue)) {
.equalStreams(playQueue)) {
stack.push(new StackItem(serviceId, url, title, playQueue)); stack.push(new StackItem(serviceId, url, title, playQueue));
} }
} }
@@ -1739,7 +1738,7 @@ public final class VideoDetailFragment
// deleted/added items inside Channel/Playlist queue and makes possible to have // deleted/added items inside Channel/Playlist queue and makes possible to have
// a history of played items // a history of played items
@Nullable final StackItem stackPeek = stack.peek(); @Nullable final StackItem stackPeek = stack.peek();
if (stackPeek != null && !stackPeek.getPlayQueue().equalStreams(queue)) { if (stackPeek != null && !stackPeek.getPlayQueue().equals(queue)) {
@Nullable final PlayQueueItem playQueueItem = queue.getItem(); @Nullable final PlayQueueItem playQueueItem = queue.getItem();
if (playQueueItem != null) { if (playQueueItem != null) {
stack.push(new StackItem(playQueueItem.getServiceId(), playQueueItem.getUrl(), stack.push(new StackItem(playQueueItem.getServiceId(), playQueueItem.getUrl(),
@@ -1803,7 +1802,7 @@ public final class VideoDetailFragment
// They are not equal when user watches something in popup while browsing in fragment and // They are not equal when user watches something in popup while browsing in fragment and
// then changes screen orientation. In that case the fragment will set itself as // then changes screen orientation. In that case the fragment will set itself as
// a service listener and will receive initial call to onMetadataUpdate() // a service listener and will receive initial call to onMetadataUpdate()
if (!queue.equalStreams(playQueue)) { if (!queue.equals(playQueue)) {
return; return;
} }
@@ -2073,9 +2072,10 @@ public final class VideoDetailFragment
private StackItem findQueueInStack(final PlayQueue queue) { private StackItem findQueueInStack(final PlayQueue queue) {
StackItem item = null; StackItem item = null;
final Iterator<StackItem> iterator = stack.descendingIterator(); final Iterator<StackItem> iterator = stack.descendingIterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
final StackItem next = iterator.next(); final StackItem next = iterator.next();
if (next.getPlayQueue().equalStreams(queue)) { if (next.getPlayQueue().equals(queue)) {
item = next; item = next;
break; break;
} }
@@ -2089,8 +2089,7 @@ public final class VideoDetailFragment
// Player will have STATE_IDLE when a user pressed back button // Player will have STATE_IDLE when a user pressed back button
if (isClearingQueueConfirmationRequired(activity) if (isClearingQueueConfirmationRequired(activity)
&& playerIsNotStopped() && playerIsNotStopped()
&& activeQueue != null && !Objects.equals(activeQueue, playQueue)) {
&& !activeQueue.equalStreams(playQueue)) {
showClearingQueueConfirmation(onAllow); showClearingQueueConfirmation(onAllow);
} else { } else {
onAllow.run(); onAllow.run();

View File

@@ -518,31 +518,18 @@ public abstract class PlayQueue implements Serializable {
* This method also gives a chance to track history of items in a queue in * This method also gives a chance to track history of items in a queue in
* VideoDetailFragment without duplicating items from two identical queues * VideoDetailFragment without duplicating items from two identical queues
*/ */
public boolean equalStreams(@Nullable final PlayQueue other) { @Override
if (other == null) { public boolean equals(final Object o) {
return false; return o instanceof PlayQueue playQueue && streams.equals(playQueue.streams);
} }
if (size() != other.size()) {
return false; @Override
} public int hashCode() {
for (int i = 0; i < size(); i++) { return streams.hashCode();
final PlayQueueItem stream = streams.get(i);
final PlayQueueItem otherStream = other.streams.get(i);
// Check is based on serviceId and URL
if (stream.getServiceId() != otherStream.getServiceId()
|| !stream.getUrl().equals(otherStream.getUrl())) {
return false;
}
}
return true;
} }
public boolean equalStreamsAndIndex(@Nullable final PlayQueue other) { public boolean equalStreamsAndIndex(@Nullable final PlayQueue other) {
if (equalStreams(other)) { return equals(other) && other.getIndex() == getIndex();
//noinspection ConstantConditions
return other.getIndex() == getIndex(); //NOSONAR: other is not null
}
return false;
} }
public boolean isDisposed() { public boolean isDisposed() {

View File

@@ -11,6 +11,7 @@ import org.schabi.newpipe.util.ExtractorHelper;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Objects;
import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.schedulers.Schedulers;
@@ -139,4 +140,16 @@ public class PlayQueueItem implements Serializable {
public void setAutoQueued(final boolean autoQueued) { public void setAutoQueued(final boolean autoQueued) {
isAutoQueued = autoQueued; isAutoQueued = autoQueued;
} }
@Override
public boolean equals(final Object o) {
return o instanceof PlayQueueItem item
&& serviceId == item.serviceId
&& url.equals(item.url);
}
@Override
public int hashCode() {
return Objects.hash(url, serviceId);
}
} }

View File

@@ -1,19 +1,17 @@
package org.schabi.newpipe.player.playqueue; package org.schabi.newpipe.player.playqueue;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import org.junit.Test;
public class PlayQueueItemTest { public class PlayQueueItemTest {
public static final String URL = "MY_URL"; public static final String URL = "MY_URL";
@Test @Test
public void equalsMustNotBeOverloaded() { public void equalsMustWork() {
final PlayQueueItem a = PlayQueueTest.makeItemWithUrl(URL); final PlayQueueItem a = PlayQueueTest.makeItemWithUrl(URL);
final PlayQueueItem b = PlayQueueTest.makeItemWithUrl(URL); final PlayQueueItem b = PlayQueueTest.makeItemWithUrl(URL);
assertEquals(a, a); assertEquals(a, b);
assertNotEquals(a, b); // they should compare different even if they have the same data
} }
} }

View File

@@ -3,6 +3,8 @@ package org.schabi.newpipe.player.playqueue;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.extractor.stream.StreamType;
@@ -13,12 +15,14 @@ import java.util.Objects;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@RunWith(Enclosed.class)
@SuppressWarnings("checkstyle:HideUtilityClassConstructor") @SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class PlayQueueTest { public class PlayQueueTest {
static PlayQueue makePlayQueue(final int index, final List<PlayQueueItem> streams) { static PlayQueue makePlayQueue(final int index, final List<PlayQueueItem> streams) {
@@ -168,7 +172,7 @@ public class PlayQueueTest {
final List<PlayQueueItem> streams = Collections.nCopies(5, item1); final List<PlayQueueItem> streams = Collections.nCopies(5, item1);
final PlayQueue queue1 = makePlayQueue(0, streams); final PlayQueue queue1 = makePlayQueue(0, streams);
final PlayQueue queue2 = makePlayQueue(0, streams); final PlayQueue queue2 = makePlayQueue(0, streams);
assertTrue(queue1.equalStreams(queue2)); assertEquals(queue1, queue2);
assertTrue(queue1.equalStreamsAndIndex(queue2)); assertTrue(queue1.equalStreamsAndIndex(queue2));
} }
@@ -177,7 +181,7 @@ public class PlayQueueTest {
final List<PlayQueueItem> streams = Collections.nCopies(5, item1); final List<PlayQueueItem> streams = Collections.nCopies(5, item1);
final PlayQueue queue1 = makePlayQueue(1, streams); final PlayQueue queue1 = makePlayQueue(1, streams);
final PlayQueue queue2 = makePlayQueue(4, streams); final PlayQueue queue2 = makePlayQueue(4, streams);
assertTrue(queue1.equalStreams(queue2)); assertEquals(queue1, queue2);
assertFalse(queue1.equalStreamsAndIndex(queue2)); assertFalse(queue1.equalStreamsAndIndex(queue2));
} }
@@ -187,7 +191,7 @@ public class PlayQueueTest {
final List<PlayQueueItem> streams2 = Collections.nCopies(5, item2); final List<PlayQueueItem> streams2 = Collections.nCopies(5, item2);
final PlayQueue queue1 = makePlayQueue(0, streams1); final PlayQueue queue1 = makePlayQueue(0, streams1);
final PlayQueue queue2 = makePlayQueue(0, streams2); final PlayQueue queue2 = makePlayQueue(0, streams2);
assertFalse(queue1.equalStreams(queue2)); assertNotEquals(queue1, queue2);
} }
@Test @Test
@@ -196,7 +200,7 @@ public class PlayQueueTest {
final List<PlayQueueItem> streams2 = Collections.nCopies(6, item2); final List<PlayQueueItem> streams2 = Collections.nCopies(6, item2);
final PlayQueue queue1 = makePlayQueue(0, streams1); final PlayQueue queue1 = makePlayQueue(0, streams1);
final PlayQueue queue2 = makePlayQueue(0, streams2); final PlayQueue queue2 = makePlayQueue(0, streams2);
assertFalse(queue1.equalStreams(queue2)); assertNotEquals(queue1, queue2);
} }
} }
} }