Skip to content

Commit

Permalink
Merge "Revert "Keep old PriorityQueue.offer behavior for < U."" into …
Browse files Browse the repository at this point in the history
…udc-dev am: 486239a

Original change: https://googleplex-android-review.googlesource.com/c/platform/libcore/+/24194550

Change-Id: I68bbcf8ecafb9e05ecd35d4e3affa9c0c7bac746
Signed-off-by: Automerger Merge Worker <[email protected]>
  • Loading branch information
RD Babiera authored and android-build-merge-worker-robot committed Jul 25, 2023
2 parents d3db5ec + 486239a commit a4627e3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 67 deletions.
34 changes: 1 addition & 33 deletions ojluni/src/main/java/java/util/PriorityQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@

package java.util;

import android.compat.Compatibility;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;

import java.util.function.Consumer;
import java.util.function.Predicate;
import jdk.internal.misc.SharedSecrets;
Expand Down Expand Up @@ -334,18 +327,7 @@ public boolean offer(E e) {
int i = size;
if (i >= queue.length)
grow(i + 1);
if (i == 0) {
// Android-changed: Keep old behavior on Android 13 or below. http://b/289878283
boolean usePreAndroidUBehavior = VMRuntime.getSdkVersion() < VersionCodes.UPSIDE_DOWN_CAKE
|| !Compatibility.isChangeEnabled(PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT);
if (usePreAndroidUBehavior) {
queue[0] = e;
} else {
siftUp(i, e);
}
} else {
siftUp(i, e);
}
siftUp(i, e);
size = i + 1;
return true;
}
Expand Down Expand Up @@ -988,18 +970,4 @@ public void forEach(Consumer<? super E> action) {
if (expectedModCount != modCount)
throw new ConcurrentModificationException();
}

// Android-added: Backward-compatible flag for offer() API.
/**
* Since Android 14, {@link PriorityQueue#offer(E)} requires all elements to be comparable if
* there was no comparator. Previously, the first element being added did not need to be
* comparable.
*
* This flag is enabled for apps targeting Android 14+.
*
* @hide
*/
@ChangeId
@EnabledSince(targetSdkVersion = VersionCodes.UPSIDE_DOWN_CAKE)
public static final long PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT = 289878283L;
}
27 changes: 5 additions & 22 deletions ojluni/src/test/java/util/PriorityQueue/AddNonComparable.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import android.compat.Compatibility;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;

public class AddNonComparable {

static <E> void test(Queue<E> queue, Supplier<E> supplier,
Expand All @@ -63,23 +58,11 @@ static <E> void test(Queue<E> queue, Supplier<E> supplier,

@Test
public void queues() {
// Android-added: test old behavior in < U.
boolean testPreAndroidUBehavior = VMRuntime.getSdkVersion() < VersionCodes.UPSIDE_DOWN_CAKE
|| !Compatibility.isChangeEnabled(PriorityQueue.PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT);

if (testPreAndroidUBehavior) {
test(new PriorityQueue<>(), NonComparable::new,
(q, e) -> {
assertEquals(q.size(), 1);
assertTrue(e == null);
});
} else {
test(new PriorityQueue<>(), NonComparable::new,
(q, e) -> {
assertEquals(q.size(), 0);
assertTrue(e instanceof ClassCastException);
});
}
test(new PriorityQueue<>(), NonComparable::new,
(q, e) -> {
assertEquals(q.size(), 0);
assertTrue(e instanceof ClassCastException);
});
test(new PriorityQueue<>(), AComparable::new,
(q, e) -> {
assertEquals(q.size(), 1);
Expand Down
13 changes: 1 addition & 12 deletions ojluni/src/test/java/util/concurrent/tck/PriorityQueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
import junit.framework.Test;
import junit.framework.TestSuite;

import android.compat.Compatibility;

import dalvik.annotation.compat.VersionCodes;
import dalvik.system.VMRuntime;

public class PriorityQueueTest extends JSR166TestCase {
public static void main(String[] args) {
main(suite(), args);
Expand Down Expand Up @@ -231,16 +226,10 @@ public void testOffer() {
*/
public void testOfferNonComparable() {
PriorityQueue q = new PriorityQueue(1);
// Android-added: test old behavior in < U.
boolean preAndroidUBehavior = VMRuntime.getSdkVersion() < VersionCodes.UPSIDE_DOWN_CAKE
|| !Compatibility.isChangeEnabled(PriorityQueue.PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT);
try {
q.offer(new Object());
if (!preAndroidUBehavior) {
shouldThrow();
}
shouldThrow();
} catch (ClassCastException success) {
assertFalse(preAndroidUBehavior);
assertTrue(q.isEmpty());
assertEquals(0, q.size());
assertNull(q.poll());
Expand Down

0 comments on commit a4627e3

Please sign in to comment.