Legacy desugared library JDK 11 file for release

New support on top of JDK8 include:
    - Flow
    - new APIs in time
    - new APIs in stream


Change-Id: I95d130e7fbd259d9434914d40236ba8b93a979b5
diff --git a/src/library_desugar/jdk11/desugar_jdk_libs.json b/src/library_desugar/jdk11/desugar_jdk_libs.json
index 4f42aac..58c8caa 100644
--- a/src/library_desugar/jdk11/desugar_jdk_libs.json
+++ b/src/library_desugar/jdk11/desugar_jdk_libs.json
@@ -6,15 +6,10 @@
   "support_all_callbacks_from_library": true,
   "common_flags": [
     {
-      "api_level_below_or_equal": 10000,
-      "rewrite_prefix": {
-        "java.net.URLDecoder": "j$.net.URLDecoder",
-        "java.net.URLEncoder": "j$.net.URLEncoder"
-      }
-    },
-    {
       "api_level_below_or_equal": 32,
       "rewrite_prefix": {
+        "java.net.URLDecoder": "j$.net.URLDecoder",
+        "java.net.URLEncoder": "j$.net.URLEncoder",
         "java.io.DesugarInputStream": "j$.io.DesugarInputStream"
       },
       "retarget_method_with_emulated_dispatch": {
@@ -51,6 +46,12 @@
       }
     },
     {
+      "api_level_below_or_equal": 29,
+      "rewrite_prefix": {
+        "java.util.concurrent.Flow": "j$.util.concurrent.Flow"
+      }
+    },
+    {
       "api_level_below_or_equal": 23,
       "rewrite_prefix": {
         "java.util.DoubleSummaryStatistics": "j$.util.DoubleSummaryStatistics",
@@ -247,12 +248,6 @@
       }
     },
     {
-      "api_level_below_or_equal": 29,
-      "rewrite_prefix": {
-        "java.util.concurrent.Flow": "j$.util.concurrent.Flow"
-      }
-    },
-    {
       "api_level_below_or_equal": 25,
       "rewrite_prefix": {
         "java.io.DesugarFile": "j$.io.DesugarFile",
diff --git a/src/library_desugar/jdk11/desugar_jdk_libs_alternative_3.json b/src/library_desugar/jdk11/desugar_jdk_libs_alternative_3.json
index 7230b39..9b48471 100644
--- a/src/library_desugar/jdk11/desugar_jdk_libs_alternative_3.json
+++ b/src/library_desugar/jdk11/desugar_jdk_libs_alternative_3.json
@@ -6,15 +6,10 @@
   "support_all_callbacks_from_library": false,
   "common_flags": [
     {
-      "api_level_below_or_equal": 10000,
-      "rewrite_prefix": {
-        "java.net.URLDecoder": "j$.net.URLDecoder",
-        "java.net.URLEncoder": "j$.net.URLEncoder"
-      }
-    },
-    {
       "api_level_below_or_equal": 32,
       "rewrite_prefix": {
+        "java.net.URLDecoder": "j$.net.URLDecoder",
+        "java.net.URLEncoder": "j$.net.URLEncoder",
         "java.io.DesugarInputStream": "j$.io.DesugarInputStream"
       },
       "retarget_method_with_emulated_dispatch": {
@@ -51,6 +46,12 @@
       }
     },
     {
+      "api_level_below_or_equal": 29,
+      "rewrite_prefix": {
+        "java.util.concurrent.Flow": "j$.util.concurrent.Flow"
+      }
+    },
+    {
       "api_level_below_or_equal": 23,
       "rewrite_prefix": {
         "java.io.DesugarBufferedReader": "j$.io.DesugarBufferedReader",
@@ -251,12 +252,6 @@
       }
     },
     {
-      "api_level_below_or_equal": 29,
-      "rewrite_prefix": {
-        "java.util.concurrent.Flow": "j$.util.concurrent.Flow"
-      }
-    },
-    {
       "api_level_below_or_equal": 25,
       "rewrite_prefix": {
         "java.io.DesugarFile": "j$.io.DesugarFile",
diff --git a/src/library_desugar/jdk11/desugar_jdk_libs_legacy.json b/src/library_desugar/jdk11/desugar_jdk_libs_legacy.json
new file mode 100644
index 0000000..e4a138b
--- /dev/null
+++ b/src/library_desugar/jdk11/desugar_jdk_libs_legacy.json
@@ -0,0 +1,217 @@
+{
+  "configuration_format_version": 4,
+  "group_id" : "com.tools.android",
+  "artifact_id" : "desugar_jdk_libs",
+  "version": "1.2.0",
+  "required_compilation_api_level": 30,
+  "synthesized_library_classes_package_prefix": "j$.",
+  "support_all_callbacks_from_library": true,
+  "common_flags": [
+    {
+      "api_level_below_or_equal": 30,
+      "rewrite_prefix": {
+        "java.time.": "j$.time.",
+        "java.util.Desugar": "j$.util.Desugar"
+      },
+      "retarget_lib_member": {
+        "java.util.Date#toInstant": "java.util.DesugarDate",
+        "java.util.TimeZone#toZoneId": "java.util.DesugarTimeZone",
+        "java.util.GregorianCalendar#toZonedDateTime": "java.util.DesugarGregorianCalendar"
+      },
+      "wrapper_conversion": [
+        "java.time.Clock"
+      ],
+      "custom_conversion": {
+        "java.time.Duration": "java.time.TimeConversions",
+        "java.time.Instant": "java.time.TimeConversions",
+        "java.time.LocalDate": "java.time.TimeConversions",
+        "java.time.MonthDay": "java.time.TimeConversions",
+        "java.time.ZoneId": "java.time.TimeConversions",
+        "java.time.ZonedDateTime": "java.time.TimeConversions"
+      }
+    },
+    {
+      "api_level_below_or_equal": 29,
+      "rewrite_prefix": {
+        "java.util.concurrent.Flow": "j$.util.concurrent.Flow"
+      }
+    },
+    {
+      "api_level_below_or_equal": 23,
+      "rewrite_prefix": {
+        "java.util.DoubleSummaryStatistics": "j$.util.DoubleSummaryStatistics",
+        "java.util.IntSummaryStatistics": "j$.util.IntSummaryStatistics",
+        "java.util.LongSummaryStatistics": "j$.util.LongSummaryStatistics",
+        "java.util.Optional": "j$.util.Optional",
+        "java.util.PrimitiveIterator": "j$.util.PrimitiveIterator",
+        "java.util.Spliterator": "j$.util.Spliterator",
+        "java.util.StringJoiner": "j$.util.StringJoiner",
+        "java.util.concurrent.ConcurrentHashMap": "j$.util.concurrent.ConcurrentHashMap",
+        "java.util.concurrent.ThreadLocalRandom": "j$.util.concurrent.ThreadLocalRandom",
+        "java.util.concurrent.atomic.DesugarAtomic": "j$.util.concurrent.atomic.DesugarAtomic",
+        "java.util.function.": "j$.util.function.",
+        "java.util.stream.": "j$.util.stream."
+      },
+      "emulate_interface": {
+        "java.lang.Iterable": "j$.lang.Iterable",
+        "java.util.Collection": "j$.util.Collection",
+        "java.util.Comparator": "j$.util.Comparator",
+        "java.util.Iterator": "j$.util.Iterator",
+        "java.util.List": "j$.util.List",
+        "java.util.Map": "j$.util.Map",
+        "java.util.Map$Entry": "j$.util.Map$Entry",
+        "java.util.Set": "j$.util.Set",
+        "java.util.SortedSet": "j$.util.SortedSet",
+        "java.util.concurrent.ConcurrentMap": "j$.util.concurrent.ConcurrentMap"
+      },
+      "dont_rewrite": [
+        "java.util.Iterator#remove"
+      ],
+      "retarget_lib_member": {
+        "java.util.Arrays#spliterator": "java.util.DesugarArrays",
+        "java.util.Arrays#stream": "java.util.DesugarArrays",
+        "java.util.LinkedHashSet#spliterator": "java.util.DesugarLinkedHashSet"
+      },
+      "wrapper_conversion": [
+        "java.util.Spliterator",
+        "java.util.function.IntUnaryOperator",
+        "java.util.function.BiFunction",
+        "java.util.function.IntConsumer",
+        "java.util.function.IntBinaryOperator",
+        "java.util.function.UnaryOperator",
+        "java.util.function.DoubleConsumer",
+        "java.util.function.IntPredicate",
+        "java.util.Spliterator$OfLong",
+        "java.util.stream.Collector",
+        "java.util.function.LongPredicate",
+        "java.util.function.ToLongFunction",
+        "java.util.function.LongToDoubleFunction",
+        "java.util.PrimitiveIterator$OfInt",
+        "java.util.function.LongToIntFunction",
+        "java.util.function.Predicate",
+        "java.util.Spliterator$OfPrimitive",
+        "java.util.function.DoubleToIntFunction",
+        "java.util.function.ObjDoubleConsumer",
+        "java.util.function.BinaryOperator",
+        "java.util.stream.DoubleStream",
+        "java.util.Spliterator$OfInt",
+        "java.util.stream.Stream",
+        "java.util.function.ObjLongConsumer",
+        "java.util.function.ToDoubleFunction",
+        "java.util.stream.IntStream",
+        "java.util.function.LongBinaryOperator",
+        "java.util.Spliterator$OfDouble",
+        "java.util.function.DoubleFunction",
+        "java.util.function.ObjIntConsumer",
+        "java.util.function.Function",
+        "java.util.function.Supplier",
+        "java.util.function.DoubleUnaryOperator",
+        "java.util.function.BiPredicate",
+        "java.util.PrimitiveIterator$OfDouble",
+        "java.util.function.DoubleBinaryOperator",
+        "java.util.PrimitiveIterator$OfLong",
+        "java.util.function.BiConsumer",
+        "java.util.function.IntFunction",
+        "java.util.stream.LongStream",
+        "java.util.function.IntToDoubleFunction",
+        "java.util.function.LongFunction",
+        "java.util.function.ToIntFunction",
+        "java.util.function.LongConsumer",
+        "java.util.function.Consumer",
+        "java.util.function.IntToLongFunction",
+        "java.util.function.DoubleToLongFunction",
+        "java.util.function.LongUnaryOperator",
+        "java.util.stream.BaseStream",
+        "java.util.function.DoublePredicate"
+      ],
+      "custom_conversion": {
+        "java.util.DoubleSummaryStatistics": "java.util.DoubleSummaryStatisticsConversions",
+        "java.util.IntSummaryStatistics": "java.util.IntSummaryStatisticsConversions",
+        "java.util.LongSummaryStatistics": "java.util.LongSummaryStatisticsConversions",
+        "java.util.Optional": "java.util.OptionalConversions",
+        "java.util.OptionalDouble": "java.util.OptionalConversions",
+        "java.util.OptionalInt": "java.util.OptionalConversions",
+        "java.util.OptionalLong": "java.util.OptionalConversions"
+      }
+    }
+  ],
+  "program_flags": [
+    {
+      "api_level_below_or_equal": 30,
+      "retarget_lib_member": {
+        "java.util.TimeZone#getTimeZone": "java.util.DesugarTimeZone",
+        "java.util.Calendar#toInstant": "java.util.DesugarCalendar",
+        "java.util.Date#from": "java.util.DesugarDate",
+        "java.util.GregorianCalendar#from": "java.util.DesugarGregorianCalendar"
+      }
+    },
+    {
+      "api_level_below_or_equal": 23,
+      "retarget_lib_member": {
+        "java.util.concurrent.atomic.AtomicInteger#getAndUpdate": "java.util.concurrent.atomic.DesugarAtomicInteger",
+        "java.util.concurrent.atomic.AtomicInteger#updateAndGet": "java.util.concurrent.atomic.DesugarAtomicInteger",
+        "java.util.concurrent.atomic.AtomicInteger#getAndAccumulate": "java.util.concurrent.atomic.DesugarAtomicInteger",
+        "java.util.concurrent.atomic.AtomicInteger#accumulateAndGet": "java.util.concurrent.atomic.DesugarAtomicInteger",
+        "java.util.concurrent.atomic.AtomicLong#getAndUpdate": "java.util.concurrent.atomic.DesugarAtomicLong",
+        "java.util.concurrent.atomic.AtomicLong#updateAndGet": "java.util.concurrent.atomic.DesugarAtomicLong",
+        "java.util.concurrent.atomic.AtomicLong#getAndAccumulate": "java.util.concurrent.atomic.DesugarAtomicLong",
+        "java.util.concurrent.atomic.AtomicLong#accumulateAndGet": "java.util.concurrent.atomic.DesugarAtomicLong",
+        "java.util.concurrent.atomic.AtomicReference#getAndUpdate": "java.util.concurrent.atomic.DesugarAtomicReference",
+        "java.util.concurrent.atomic.AtomicReference#updateAndGet": "java.util.concurrent.atomic.DesugarAtomicReference",
+        "java.util.concurrent.atomic.AtomicReference#getAndAccumulate": "java.util.concurrent.atomic.DesugarAtomicReference",
+        "java.util.concurrent.atomic.AtomicReference#accumulateAndGet": "java.util.concurrent.atomic.DesugarAtomicReference",
+        "java.util.Collections#synchronizedMap": "java.util.DesugarCollections",
+        "java.util.Collections#synchronizedSortedMap": "java.util.DesugarCollections"
+      }
+    }
+  ],
+  "library_flags": [
+    {
+      "api_level_below_or_equal": 30,
+      "rewrite_prefix": {
+        "j$.time.": "java.time.",
+        "sun.misc.Desugar": "j$.sun.misc.Desugar",
+        "sun.security.action.": "j$.sun.security.action."
+      },
+      "backport": {
+        "java.lang.DesugarDouble": "java.lang.Double",
+        "java.lang.DesugarInteger": "java.lang.Integer",
+        "java.lang.DesugarLong": "java.lang.Long",
+        "java.lang.DesugarMath": "java.lang.Math"
+      }
+    },
+    {
+      "api_level_below_or_equal": 23,
+      "rewrite_prefix": {
+        "java.util.AbstractList": "j$.util.AbstractList",
+        "java.util.CollSer": "j$.util.CollSer",
+        "java.util.Comparators": "j$.util.Comparators",
+        "java.util.ImmutableCollections": "j$.util.ImmutableCollections",
+        "java.util.KeyValueHolder": "j$.util.KeyValueHolder",
+        "java.util.SortedSet$1": "j$.util.SortedSet$1",
+        "java.util.Tripwire": "j$.util.Tripwire",
+        "java.util.concurrent.Helpers": "j$.util.concurrent.Helpers",
+        "j$.util.DoubleSummaryStatistics": "java.util.DoubleSummaryStatistics",
+        "j$.util.IntSummaryStatistics": "java.util.IntSummaryStatistics",
+        "j$.util.LongSummaryStatistics": "java.util.LongSummaryStatistics",
+        "j$.util.Optional": "java.util.Optional"
+      }
+    }
+  ],
+  "shrinker_config": [
+    "-keepclassmembers class j$.util.concurrent.ConcurrentHashMap$TreeBin { int lockState; }",
+    "-keepclassmembers class j$.util.concurrent.ConcurrentHashMap { int sizeCtl; int transferIndex; long baseCount; int cellsBusy; }",
+    "-keepclassmembers class j$.util.concurrent.ConcurrentHashMap { private void readObject(java.io.ObjectInputStream); private void writeObject(java.io.ObjectOutputStream); private void readObjectNoData(); private static final java.io.ObjectStreamField[] serialPersistentFields; private static final long serialVersionUID;}",
+    "-keepclassmembers class j$.util.concurrent.ConcurrentHashMap$CounterCell { long value; }",
+    "-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); public static final !synthetic <fields>; }",
+    "-keeppackagenames j$.**",
+    "-keepclassmembers class j$.util.IntSummaryStatistics { long count; long sum; int min; int max; }",
+    "-keepclassmembers class j$.util.LongSummaryStatistics { long count; long sum; long min; long max; }",
+    "-keepclassmembers class j$.util.DoubleSummaryStatistics { long count; double sum; double min; double max; }",
+    "-keepattributes Signature",
+    "-keepattributes EnclosingMethod",
+    "-keepattributes InnerClasses",
+    "-dontwarn sun.misc.Unsafe",
+    "-dontwarn wrapper.**"
+  ]
+}
\ No newline at end of file
diff --git a/src/test/examplesJava9/flow/FlowExample.java b/src/test/examplesJava9/flow/FlowExample.java
new file mode 100644
index 0000000..8491863
--- /dev/null
+++ b/src/test/examplesJava9/flow/FlowExample.java
@@ -0,0 +1,107 @@
+// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package flow;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.Future;
+import java.util.function.Consumer;
+
+public class FlowExample {
+  public static void main(String[] args) {
+    OneShotPublisher oneShotPublisher = new OneShotPublisher();
+    SampleSubscriber<Object> subscriber = new SampleSubscriber<>(50L, System.out::println);
+    oneShotPublisher.subscribe(subscriber);
+  }
+
+  static class OneShotPublisher implements Publisher<Boolean> {
+
+    @SuppressWarnings("CommonForkJoinPool")
+    private final ExecutorService executor = new ForkJoinPool(2); // daemon-based
+
+    private boolean subscribed; // true after first subscribe
+
+    public synchronized void subscribe(Subscriber<? super Boolean> subscriber) {
+      if (subscribed) {
+        subscriber.onError(new IllegalStateException()); // only one allowed
+      } else {
+        subscribed = true;
+        subscriber.onSubscribe(new OneShotSubscription(subscriber, executor));
+      }
+    }
+
+    static class OneShotSubscription implements Subscription {
+
+      private final Subscriber<? super Boolean> subscriber;
+      private final ExecutorService executor;
+      private Future<?> future; // to allow cancellation
+      private boolean completed;
+
+      OneShotSubscription(Subscriber<? super Boolean> subscriber, ExecutorService executor) {
+        this.subscriber = subscriber;
+        this.executor = executor;
+      }
+
+      public synchronized void request(long n) {
+        if (n != 0 && !completed) {
+          completed = true;
+          if (n < 0) {
+            IllegalArgumentException ex = new IllegalArgumentException();
+            executor.execute(() -> subscriber.onError(ex));
+          } else {
+            future =
+                executor.submit(
+                    () -> {
+                      subscriber.onNext(Boolean.TRUE);
+                      subscriber.onComplete();
+                    });
+          }
+        }
+      }
+
+      public synchronized void cancel() {
+        completed = true;
+        if (future != null) {
+          future.cancel(false);
+        }
+      }
+    }
+  }
+
+  static class SampleSubscriber<T> implements Subscriber<T> {
+
+    final Consumer<? super T> consumer;
+    Subscription subscription;
+    final long bufferSize;
+    long count;
+
+    SampleSubscriber(long bufferSize, Consumer<? super T> consumer) {
+      this.bufferSize = bufferSize;
+      this.consumer = consumer;
+    }
+
+    public void onSubscribe(Subscription subscription) {
+      long initialRequestSize = bufferSize;
+      count = bufferSize - bufferSize / 2; // re-request when half consumed
+      (this.subscription = subscription).request(initialRequestSize);
+    }
+
+    public void onNext(T item) {
+      if (--count <= 0) {
+        subscription.request(count = bufferSize - bufferSize / 2);
+      }
+      consumer.accept(item);
+    }
+
+    public void onError(Throwable ex) {
+      ex.printStackTrace();
+    }
+
+    public void onComplete() {}
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FlowTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FlowTest.java
new file mode 100644
index 0000000..c24f00d
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FlowTest.java
@@ -0,0 +1,80 @@
+// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.desugar.desugaredlibrary.jdk11;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
+import com.android.tools.r8.utils.BooleanUtils;
+import com.android.tools.r8.utils.StringUtils;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import org.junit.Assume;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class FlowTest extends DesugaredLibraryTestBase {
+
+  private final TestParameters parameters;
+  private final boolean shrinkDesugaredLibrary;
+
+  private static final Path INPUT_JAR = Paths.get(ToolHelper.EXAMPLES_JAVA9_BUILD_DIR + "flow.jar");
+  private static final String EXPECTED_OUTPUT = StringUtils.lines("true");
+  private static final String MAIN_CLASS = "flow.FlowExample";
+
+  @Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
+  public static List<Object[]> data() {
+    return buildParameters(
+        BooleanUtils.values(), getTestParameters().withDexRuntimes().withAllApiLevels().build());
+  }
+
+  public FlowTest(boolean shrinkDesugaredLibrary, TestParameters parameters) {
+    this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testD8() throws Exception {
+    Assume.assumeTrue(isJDK11DesugaredLibrary());
+    KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
+    testForD8(parameters.getBackend())
+        .addLibraryFiles(getLibraryFile())
+        .addProgramFiles(INPUT_JAR)
+        .setMinApi(parameters.getApiLevel())
+        .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
+        .compile()
+        .addDesugaredCoreLibraryRunClassPath(
+            this::buildDesugaredLibrary,
+            parameters.getApiLevel(),
+            keepRuleConsumer.get(),
+            shrinkDesugaredLibrary)
+        .run(parameters.getRuntime(), MAIN_CLASS)
+        .assertSuccessWithOutput(EXPECTED_OUTPUT);
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    Assume.assumeTrue(isJDK11DesugaredLibrary());
+    KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
+    testForR8(parameters.getBackend())
+        .addLibraryFiles(getLibraryFile())
+        .addProgramFiles(INPUT_JAR)
+        .setMinApi(parameters.getApiLevel())
+        .addKeepMainRule(MAIN_CLASS)
+        .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
+        .compile()
+        .addDesugaredCoreLibraryRunClassPath(
+            this::buildDesugaredLibrary,
+            parameters.getApiLevel(),
+            keepRuleConsumer.get(),
+            shrinkDesugaredLibrary)
+        .run(parameters.getRuntime(), MAIN_CLASS)
+        .assertSuccessWithOutput(EXPECTED_OUTPUT);
+  }
+}