[go: nahoru, domu]

Convert Consumer to Kotlin

By taking advantage of Kotlin's generics,
we ensure that the type that the Consumer is
created with (e.g., Consumer<String>) matches the
nullability of what the accept() method receives.

This means that a Consumer<String> is guaranteed
a non-null String while a Consumer<String?> may
receive a nullable String and this is now enforced
by the Kotlin compiler.

Rather than the generic name 't' for the parameter
to accept, the name 'value' was chosen to mirror
APIs in the Kotlin stdlib.

Test: ./gradlew checkApi
Relnote: "The `androidx.core.util.Consumer` class has
been rewritten in Kotlin and now enforce that the generic
type `T` now matches the nullability of what the
`accept()` method receives."

Change-Id: Ie21e0fdf7843c799a5cbcbcccde31e0770e6b3c1
diff --git a/activity/activity/src/androidTest/java/androidx/activity/ComponentActivityCallbacksTest.kt b/activity/activity/src/androidTest/java/androidx/activity/ComponentActivityCallbacksTest.kt
index d0bdd8d..1c67712 100644
--- a/activity/activity/src/androidTest/java/androidx/activity/ComponentActivityCallbacksTest.kt
+++ b/activity/activity/src/androidTest/java/androidx/activity/ComponentActivityCallbacksTest.kt
@@ -105,8 +105,8 @@
             val expectedFontScale = receivedFontScale * 2
 
             val listener = object : Consumer<Configuration> {
-                override fun accept(newConfig: Configuration) {
-                    receivedFontScale = newConfig.fontScale
+                override fun accept(value: Configuration) {
+                    receivedFontScale = value.fontScale
                     activity.removeOnConfigurationChangedListener(this)
                 }
             }
@@ -177,8 +177,8 @@
             var receivedLevel = -1
 
             val listener = object : Consumer<Int> {
-                override fun accept(level: Int) {
-                    receivedLevel = level
+                override fun accept(value: Int) {
+                    receivedLevel = value
                     activity.removeOnTrimMemoryListener(this)
                 }
             }
@@ -262,8 +262,8 @@
             val receivedIntents = mutableListOf<Intent>()
 
             val listener = object : Consumer<Intent> {
-                override fun accept(intent: Intent) {
-                    receivedIntents += intent
+                override fun accept(value: Intent) {
+                    receivedIntents += value
                     activity.removeOnNewIntentListener(this)
                 }
             }
@@ -366,8 +366,8 @@
             lateinit var receivedInfo: MultiWindowModeChangedInfo
 
             val listener = object : Consumer<MultiWindowModeChangedInfo> {
-                override fun accept(info: MultiWindowModeChangedInfo) {
-                    receivedInfo = info
+                override fun accept(value: MultiWindowModeChangedInfo) {
+                    receivedInfo = value
                     activity.removeOnMultiWindowModeChangedListener(this)
                 }
             }
@@ -461,8 +461,8 @@
             lateinit var receivedInfo: PictureInPictureModeChangedInfo
 
             val listener = object : Consumer<PictureInPictureModeChangedInfo> {
-                override fun accept(info: PictureInPictureModeChangedInfo) {
-                    receivedInfo = info
+                override fun accept(value: PictureInPictureModeChangedInfo) {
+                    receivedInfo = value
                     activity.removeOnPictureInPictureModeChangedListener(this)
                 }
             }
diff --git a/camera/camera-video/src/androidTest/java/androidx/camera/video/VideoRecordingTest.kt b/camera/camera-video/src/androidTest/java/androidx/camera/video/VideoRecordingTest.kt
index c514c78..2a86fcf 100644
--- a/camera/camera-video/src/androidTest/java/androidx/camera/video/VideoRecordingTest.kt
+++ b/camera/camera-video/src/androidTest/java/androidx/camera/video/VideoRecordingTest.kt
@@ -1291,8 +1291,8 @@
         }!!.await(timeoutMillis, TimeUnit.MILLISECONDS)).isTrue()
     }
 
-    override fun accept(event: VideoRecordEvent?) {
-        when (event) {
+    override fun accept(value: VideoRecordEvent) {
+        when (value) {
             is VideoRecordEvent.Status -> {
                 synchronized(this) {
                     countDown?.countDown()
diff --git a/core/core-ktx/src/main/java/androidx/core/util/Consumer.kt b/core/core-ktx/src/main/java/androidx/core/util/PlatformConsumer.kt
similarity index 98%
rename from core/core-ktx/src/main/java/androidx/core/util/Consumer.kt
rename to core/core-ktx/src/main/java/androidx/core/util/PlatformConsumer.kt
index c1d00aab..0c5d4b1 100644
--- a/core/core-ktx/src/main/java/androidx/core/util/Consumer.kt
+++ b/core/core-ktx/src/main/java/androidx/core/util/PlatformConsumer.kt
@@ -16,6 +16,7 @@
 
 // java.util.function.Consumer was added in API 24
 @file:RequiresApi(24)
+@file:JvmName("ConsumerKt")
 package androidx.core.util
 
 import androidx.annotation.RequiresApi
diff --git a/core/core-testing/src/main/java/androidx/core/testing/util/TestConsumer.kt b/core/core-testing/src/main/java/androidx/core/testing/util/TestConsumer.kt
index 47eaeb3..e15d2ad 100644
--- a/core/core-testing/src/main/java/androidx/core/testing/util/TestConsumer.kt
+++ b/core/core-testing/src/main/java/androidx/core/testing/util/TestConsumer.kt
@@ -37,6 +37,7 @@
      * Records the value in the order it was received.
      * @param t the input argument.
      */
+    @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") /* Avoid breaking named parameter compat */
     override fun accept(t: T) {
         lock.withLock {
             values.add(t)
diff --git a/core/core/api/current.txt b/core/core/api/current.txt
index d7001c1..4f2ec31 100644
--- a/core/core/api/current.txt
+++ b/core/core/api/current.txt
@@ -2349,8 +2349,8 @@
     method public java.io.FileOutputStream startWrite() throws java.io.IOException;
   }
 
-  public interface Consumer<T> {
-    method public void accept(T!);
+  public fun interface Consumer<T> {
+    method public void accept(T value);
   }
 
   @java.lang.FunctionalInterface public interface Function<T, R> {
diff --git a/core/core/api/restricted_current.txt b/core/core/api/restricted_current.txt
index b75e2bb..3abff46 100644
--- a/core/core/api/restricted_current.txt
+++ b/core/core/api/restricted_current.txt
@@ -2732,8 +2732,8 @@
     method public java.io.FileOutputStream startWrite() throws java.io.IOException;
   }
 
-  public interface Consumer<T> {
-    method public void accept(T!);
+  public fun interface Consumer<T> {
+    method public void accept(T value);
   }
 
   @RestrictTo(androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX) public class DebugUtils {
diff --git a/core/core/src/main/java/androidx/core/util/Consumer.kt b/core/core/src/main/java/androidx/core/util/Consumer.kt
index 28e0fb8..240f7e0 100644
--- a/core/core/src/main/java/androidx/core/util/Consumer.kt
+++ b/core/core/src/main/java/androidx/core/util/Consumer.kt
@@ -13,19 +13,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-package androidx.core.util;
+package androidx.core.util
 
 /**
- * Compat version of {@link java.util.function.Consumer}
- * @param <T> the type of the input to the operation
+ * Compat version of [java.util.function.Consumer]
+ * @param T the type of the input to the operation
  */
-public interface Consumer<T> {
-
+fun interface Consumer<T> {
     /**
      * Performs this operation on the given argument.
      *
-     * @param t the input argument
+     * @param value the input argument
      */
-    void accept(T t);
+    fun accept(value: T)
 }
diff --git a/transition/transition/src/androidTest/java/androidx/transition/SeekTransitionTest.kt b/transition/transition/src/androidTest/java/androidx/transition/SeekTransitionTest.kt
index 58fce87..728ffaf 100644
--- a/transition/transition/src/androidTest/java/androidx/transition/SeekTransitionTest.kt
+++ b/transition/transition/src/androidTest/java/androidx/transition/SeekTransitionTest.kt
@@ -1059,7 +1059,7 @@
         var animatedFraction = -1f
         var animatedMillis = -1L
         val removeListener = object : Consumer<TransitionSeekController> {
-            override fun accept(t: TransitionSeekController?) {
+            override fun accept(value: TransitionSeekController) {
                 seekController.removeOnProgressChangedListener(this)
             }
         }
diff --git a/window/integration-tests/configuration-change-tests/src/androidTest/java/androidx/window/integration/TestConsumer.kt b/window/integration-tests/configuration-change-tests/src/androidTest/java/androidx/window/integration/TestConsumer.kt
index 05a18ec..01f29fe 100644
--- a/window/integration-tests/configuration-change-tests/src/androidTest/java/androidx/window/integration/TestConsumer.kt
+++ b/window/integration-tests/configuration-change-tests/src/androidTest/java/androidx/window/integration/TestConsumer.kt
@@ -40,9 +40,9 @@
     /**
      * Appends the new value at the end of the mutable list values.
      */
-    override fun accept(numLayoutFeatures: T) {
+    override fun accept(value: T) {
         valueLock.withLock {
-            values.add(numLayoutFeatures)
+            values.add(value)
         }
         countDownLock.withLock {
             valueLatch.countDown()
diff --git a/window/window-java/src/androidTest/java/androidx/window/java/TestConsumer.kt b/window/window-java/src/androidTest/java/androidx/window/java/TestConsumer.kt
index cb53396..5fff062 100644
--- a/window/window-java/src/androidTest/java/androidx/window/java/TestConsumer.kt
+++ b/window/window-java/src/androidTest/java/androidx/window/java/TestConsumer.kt
@@ -22,8 +22,8 @@
 internal class TestConsumer<T> : Consumer<T> {
     private val values = mutableListOf<T>()
 
-    override fun accept(t: T) {
-        values.add(t)
+    override fun accept(value: T) {
+        values.add(value)
     }
 
     fun assertValueCount(count: Int) {
diff --git a/window/window/src/androidTest/java/androidx/window/layout/WindowInfoTrackerImplTest.kt b/window/window/src/androidTest/java/androidx/window/layout/WindowInfoTrackerImplTest.kt
index 69882ce..84da641 100644
--- a/window/window/src/androidTest/java/androidx/window/layout/WindowInfoTrackerImplTest.kt
+++ b/window/window/src/androidTest/java/androidx/window/layout/WindowInfoTrackerImplTest.kt
@@ -150,8 +150,8 @@
             val callback: Consumer<WindowLayoutInfo>
         ) : Consumer<WindowLayoutInfo> {
 
-            override fun accept(t: WindowLayoutInfo?) {
-                executor.execute { callback.accept(t) }
+            override fun accept(value: WindowLayoutInfo) {
+                executor.execute { callback.accept(value) }
             }
         }
 
diff --git a/window/window/src/main/java/androidx/window/layout/adapter/extensions/MulticastConsumer.kt b/window/window/src/main/java/androidx/window/layout/adapter/extensions/MulticastConsumer.kt
index b57f88f..a5d2cc1 100644
--- a/window/window/src/main/java/androidx/window/layout/adapter/extensions/MulticastConsumer.kt
+++ b/window/window/src/main/java/androidx/window/layout/adapter/extensions/MulticastConsumer.kt
@@ -39,8 +39,9 @@
 
     override fun accept(value: OEMWindowLayoutInfo) {
         multicastConsumerLock.withLock {
-            lastKnownValue = ExtensionsWindowLayoutInfoAdapter.translate(context, value)
-            registeredListeners.forEach { consumer -> consumer.accept(lastKnownValue) }
+            val newValue = ExtensionsWindowLayoutInfoAdapter.translate(context, value)
+            lastKnownValue = newValue
+            registeredListeners.forEach { consumer -> consumer.accept(newValue) }
         }
     }
 
diff --git a/window/window/src/testUtil/java/androidx/window/TestConsumer.kt b/window/window/src/testUtil/java/androidx/window/TestConsumer.kt
index 6fb559e..5c088e1 100644
--- a/window/window/src/testUtil/java/androidx/window/TestConsumer.kt
+++ b/window/window/src/testUtil/java/androidx/window/TestConsumer.kt
@@ -29,8 +29,8 @@
     /**
      * Add the value to the list of seen values.
      */
-    override fun accept(t: T) {
-        values.add(t)
+    override fun accept(value: T) {
+        values.add(value)
     }
 
     /**