mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-09-04 20:07:56 +00:00
Add PlayQueueItem equals and hashCode
This commit is contained in:
@@ -852,8 +852,7 @@ public final class VideoDetailFragment
|
||||
if (playQueue == null) {
|
||||
playQueue = new SinglePlayQueue(result);
|
||||
}
|
||||
if (stack.isEmpty() || !stack.peek().getPlayQueue()
|
||||
.equalStreams(playQueue)) {
|
||||
if (stack.isEmpty() || !stack.peek().getPlayQueue().equals(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
|
||||
// a history of played items
|
||||
@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();
|
||||
if (playQueueItem != null) {
|
||||
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
|
||||
// then changes screen orientation. In that case the fragment will set itself as
|
||||
// a service listener and will receive initial call to onMetadataUpdate()
|
||||
if (!queue.equalStreams(playQueue)) {
|
||||
if (!queue.equals(playQueue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2073,9 +2072,10 @@ public final class VideoDetailFragment
|
||||
private StackItem findQueueInStack(final PlayQueue queue) {
|
||||
StackItem item = null;
|
||||
final Iterator<StackItem> iterator = stack.descendingIterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
final StackItem next = iterator.next();
|
||||
if (next.getPlayQueue().equalStreams(queue)) {
|
||||
if (next.getPlayQueue().equals(queue)) {
|
||||
item = next;
|
||||
break;
|
||||
}
|
||||
@@ -2089,8 +2089,7 @@ public final class VideoDetailFragment
|
||||
// Player will have STATE_IDLE when a user pressed back button
|
||||
if (isClearingQueueConfirmationRequired(activity)
|
||||
&& playerIsNotStopped()
|
||||
&& activeQueue != null
|
||||
&& !activeQueue.equalStreams(playQueue)) {
|
||||
&& !Objects.equals(activeQueue, playQueue)) {
|
||||
showClearingQueueConfirmation(onAllow);
|
||||
} else {
|
||||
onAllow.run();
|
||||
|
@@ -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
|
||||
* VideoDetailFragment without duplicating items from two identical queues
|
||||
*/
|
||||
public boolean equalStreams(@Nullable final PlayQueue other) {
|
||||
if (other == null) {
|
||||
return false;
|
||||
}
|
||||
if (size() != other.size()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < size(); i++) {
|
||||
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;
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
return o instanceof PlayQueue playQueue && streams.equals(playQueue.streams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return streams.hashCode();
|
||||
}
|
||||
|
||||
public boolean equalStreamsAndIndex(@Nullable final PlayQueue other) {
|
||||
if (equalStreams(other)) {
|
||||
//noinspection ConstantConditions
|
||||
return other.getIndex() == getIndex(); //NOSONAR: other is not null
|
||||
}
|
||||
return false;
|
||||
return equals(other) && other.getIndex() == getIndex();
|
||||
}
|
||||
|
||||
public boolean isDisposed() {
|
||||
|
@@ -11,6 +11,7 @@ import org.schabi.newpipe.util.ExtractorHelper;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import io.reactivex.rxjava3.core.Single;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
@@ -139,4 +140,16 @@ public class PlayQueueItem implements Serializable {
|
||||
public void setAutoQueued(final boolean 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);
|
||||
}
|
||||
}
|
||||
|
@@ -1,19 +1,17 @@
|
||||
package org.schabi.newpipe.player.playqueue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PlayQueueItemTest {
|
||||
|
||||
public static final String URL = "MY_URL";
|
||||
|
||||
@Test
|
||||
public void equalsMustNotBeOverloaded() {
|
||||
public void equalsMustWork() {
|
||||
final PlayQueueItem a = PlayQueueTest.makeItemWithUrl(URL);
|
||||
final PlayQueueItem b = PlayQueueTest.makeItemWithUrl(URL);
|
||||
assertEquals(a, a);
|
||||
assertNotEquals(a, b); // they should compare different even if they have the same data
|
||||
assertEquals(a, b);
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,8 @@ package org.schabi.newpipe.player.playqueue;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
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.StreamType;
|
||||
|
||||
@@ -13,12 +15,14 @@ import java.util.Objects;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
@RunWith(Enclosed.class)
|
||||
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
|
||||
public class PlayQueueTest {
|
||||
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 PlayQueue queue1 = makePlayQueue(0, streams);
|
||||
final PlayQueue queue2 = makePlayQueue(0, streams);
|
||||
assertTrue(queue1.equalStreams(queue2));
|
||||
assertEquals(queue1, queue2);
|
||||
assertTrue(queue1.equalStreamsAndIndex(queue2));
|
||||
}
|
||||
|
||||
@@ -177,7 +181,7 @@ public class PlayQueueTest {
|
||||
final List<PlayQueueItem> streams = Collections.nCopies(5, item1);
|
||||
final PlayQueue queue1 = makePlayQueue(1, streams);
|
||||
final PlayQueue queue2 = makePlayQueue(4, streams);
|
||||
assertTrue(queue1.equalStreams(queue2));
|
||||
assertEquals(queue1, queue2);
|
||||
assertFalse(queue1.equalStreamsAndIndex(queue2));
|
||||
}
|
||||
|
||||
@@ -187,7 +191,7 @@ public class PlayQueueTest {
|
||||
final List<PlayQueueItem> streams2 = Collections.nCopies(5, item2);
|
||||
final PlayQueue queue1 = makePlayQueue(0, streams1);
|
||||
final PlayQueue queue2 = makePlayQueue(0, streams2);
|
||||
assertFalse(queue1.equalStreams(queue2));
|
||||
assertNotEquals(queue1, queue2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -196,7 +200,7 @@ public class PlayQueueTest {
|
||||
final List<PlayQueueItem> streams2 = Collections.nCopies(6, item2);
|
||||
final PlayQueue queue1 = makePlayQueue(0, streams1);
|
||||
final PlayQueue queue2 = makePlayQueue(0, streams2);
|
||||
assertFalse(queue1.equalStreams(queue2));
|
||||
assertNotEquals(queue1, queue2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user