[go: nahoru, domu]

Add an iterator-based 'any' method for Lists.

Test: New Test
Change-Id: I642f05e1cd684b7f7930be98b501d9e286e5ab52
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/DoubleTapGestureDetector.kt b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/DoubleTapGestureDetector.kt
index 0ccd304..1fc73ac 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/DoubleTapGestureDetector.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/DoubleTapGestureDetector.kt
@@ -30,6 +30,7 @@
 import androidx.ui.temputils.delay
 import androidx.ui.unit.IntPxSize
 import androidx.ui.unit.PxPosition
+import androidx.ui.util.fastAny
 import kotlinx.coroutines.Job
 import kotlin.coroutines.CoroutineContext
 
@@ -102,7 +103,7 @@
             }
 
             if (pass == PointerEventPass.PostDown &&
-                changesToReturn.any { it.anyPositionChangeConsumed() }
+                changesToReturn.fastAny { it.anyPositionChangeConsumed() }
             ) {
                 state = State.Idle
             }
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/GestureUtils.kt b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/GestureUtils.kt
index 9581101..5273f7e 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/GestureUtils.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/GestureUtils.kt
@@ -20,6 +20,7 @@
 import androidx.ui.core.pointerinput.PointerInputFilter
 import androidx.ui.core.pointerinput.PointerInputModifier
 import androidx.ui.unit.IntPxSize
+import androidx.ui.util.fastAny
 
 /**
  * Utility method that determines if any pointers are currently in [bounds].
@@ -30,7 +31,7 @@
  * @return True if at least one pointer is in bounds.
  */
 fun List<PointerInputChange>.anyPointersInBounds(bounds: IntPxSize) =
-    any {
+    fastAny {
         it.current.down &&
                 it.current.position!!.x.value >= 0 &&
                 it.current.position!!.x < bounds.width &&
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/LongPressGestureDetector.kt b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/LongPressGestureDetector.kt
index 0fa75d3..e0c33b3 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/LongPressGestureDetector.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/LongPressGestureDetector.kt
@@ -34,6 +34,7 @@
 import androidx.ui.temputils.delay
 import androidx.ui.unit.IntPxSize
 import androidx.ui.unit.PxPosition
+import androidx.ui.util.fastAny
 import kotlinx.coroutines.Job
 import kotlin.coroutines.CoroutineContext
 
@@ -123,7 +124,7 @@
             if (
                 pass == PointerEventPass.PostDown &&
                 state != State.Idle &&
-                changes.any { it.anyPositionChangeConsumed() }
+                changes.fastAny { it.anyPositionChangeConsumed() }
             ) {
                 // If we are anything but Idle and something consumed movement, reset.
                 resetToIdle()
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/PressIndicatorGestureDetector.kt b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/PressIndicatorGestureDetector.kt
index 80e0fa6..a6d4fd0 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/PressIndicatorGestureDetector.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/PressIndicatorGestureDetector.kt
@@ -29,6 +29,7 @@
 import androidx.ui.geometry.Offset
 import androidx.ui.unit.IntPxSize
 import androidx.ui.unit.PxPosition
+import androidx.ui.util.fastAny
 
 /**
  * This gesture detector has callbacks for when a press gesture starts and ends for the purposes of
@@ -176,7 +177,7 @@
             if (
                 pass == PointerEventPass.PostDown &&
                 state == State.Started &&
-                internalChanges.any { it.anyPositionChangeConsumed() }
+                internalChanges.fastAny { it.anyPositionChangeConsumed() }
             ) {
                 // On the final pass, if we have started and any of the changes had consumed
                 // position changes, we cancel.
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/RawDragGestureDetector.kt b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/RawDragGestureDetector.kt
index d8271fd..1cbb595 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/RawDragGestureDetector.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/RawDragGestureDetector.kt
@@ -34,6 +34,7 @@
 import androidx.ui.unit.IntPxSize
 import androidx.ui.unit.PxPosition
 import androidx.ui.unit.px
+import androidx.ui.util.fastAny
 
 interface DragObserver {
 
@@ -163,7 +164,7 @@
 
                 // Handle up changes, which includes removing individual pointer VelocityTrackers
                 // and potentially calling onStop().
-                if (changesToReturn.any { it.changedToUpIgnoreConsumed() }) {
+                if (changesToReturn.fastAny { it.changedToUpIgnoreConsumed() }) {
 
                     var velocityTracker: VelocityTracker? = null
 
@@ -203,7 +204,7 @@
                 }
 
                 // For each new pointer that has been added, start tracking information about it.
-                if (changesToReturn.any { it.changedToDownIgnoreConsumed() }) {
+                if (changesToReturn.fastAny { it.changedToDownIgnoreConsumed() }) {
                     changesToReturn.forEach {
                         // If a pointer has changed to down, we should start tracking information
                         // about it.
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/TapGestureDetector.kt b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/TapGestureDetector.kt
index df6c3ad..a740b46 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/gesture/TapGestureDetector.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/gesture/TapGestureDetector.kt
@@ -27,6 +27,7 @@
 import androidx.ui.core.consumeDownChange
 import androidx.ui.core.pointerinput.PointerInputFilter
 import androidx.ui.unit.IntPxSize
+import androidx.ui.util.fastAny
 
 /**
  * This gesture detector fires a callback when a traditional press is being released.  This is
@@ -102,7 +103,7 @@
             }
 
             if (pass == PointerEventPass.PostDown && active &&
-                internalChanges.any { it.anyPositionChangeConsumed() }
+                internalChanges.fastAny { it.anyPositionChangeConsumed() }
             ) {
                 // On the final pass, if we have started and any of the changes had consumed
                 // position changes, we cancel.
diff --git a/ui/ui-platform/src/main/java/androidx/ui/core/LayoutNodeWrapper.kt b/ui/ui-platform/src/main/java/androidx/ui/core/LayoutNodeWrapper.kt
index 9a00e73..8b4743e 100644
--- a/ui/ui-platform/src/main/java/androidx/ui/core/LayoutNodeWrapper.kt
+++ b/ui/ui-platform/src/main/java/androidx/ui/core/LayoutNodeWrapper.kt
@@ -38,6 +38,7 @@
 import androidx.ui.unit.toPx
 import androidx.ui.unit.toPxPosition
 import androidx.ui.unit.toPxSize
+import androidx.ui.util.fastAny
 import androidx.ui.util.fastForEach
 
 private val Unmeasured = IntPxSize(IntPx.Zero, IntPx.Zero)
@@ -489,7 +490,7 @@
 
     override fun draw(canvas: Canvas, density: Density) {
         if (introducedLayer != null ||
-            (!layoutNode.hasLayer && layoutNode.layoutChildren.any { it.hasElevation })
+            (!layoutNode.hasLayer && layoutNode.layoutChildren.fastAny { it.hasElevation })
         ) {
             layoutNodeInvalidate = null
             // we need to introduce a layer
@@ -540,7 +541,7 @@
         if (isGlobalPointerInBounds(pointerPositionRelativeToScreen)) {
             // Any because as soon as true is returned, we know we have found a hit path and we must
             //  not add PointerInputFilters on different paths so we should not even go looking.
-            return layoutNode.children.reversed().any { child ->
+            return layoutNode.children.reversed().fastAny { child ->
                 callHitTest(child, pointerPositionRelativeToScreen, hitPointerInputFilters)
             }
         } else {
@@ -576,7 +577,7 @@
     } else {
         // Any because as soon as true is returned, we know we have found a hit path and we must
         // not add PointerInputFilters on different paths so we should not even go looking.
-        return node.children.reversed().any { child ->
+        return node.children.reversed().fastAny { child ->
             callHitTest(child, globalPoint, hitPointerInputFilters)
         }
     }
diff --git a/ui/ui-util/api/0.1.0-dev09.txt b/ui/ui-util/api/0.1.0-dev09.txt
index e1cdf07..e8faee5 100644
--- a/ui/ui-util/api/0.1.0-dev09.txt
+++ b/ui/ui-util/api/0.1.0-dev09.txt
@@ -1,10 +1,6 @@
 // Signature format: 3.0
 package androidx.ui.util {
 
-  public final class FastForEachKt {
-    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
-  }
-
   public final class InlineClassHelperKt {
     method public static inline long packFloats(float val1, float val2);
     method public static inline long packInts(int val1, int val2);
@@ -14,6 +10,11 @@
     method public static inline int unpackInt2(long value);
   }
 
+  public final class ListUtilsKt {
+    method public static inline <T> boolean fastAny(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,java.lang.Boolean> predicate);
+    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
+  }
+
   public final class MathHelpersKt {
     method public static float lerp(float start, float stop, float fraction);
     method public static int lerp(int start, int stop, float fraction);
diff --git a/ui/ui-util/api/current.txt b/ui/ui-util/api/current.txt
index e1cdf07..e8faee5 100644
--- a/ui/ui-util/api/current.txt
+++ b/ui/ui-util/api/current.txt
@@ -1,10 +1,6 @@
 // Signature format: 3.0
 package androidx.ui.util {
 
-  public final class FastForEachKt {
-    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
-  }
-
   public final class InlineClassHelperKt {
     method public static inline long packFloats(float val1, float val2);
     method public static inline long packInts(int val1, int val2);
@@ -14,6 +10,11 @@
     method public static inline int unpackInt2(long value);
   }
 
+  public final class ListUtilsKt {
+    method public static inline <T> boolean fastAny(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,java.lang.Boolean> predicate);
+    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
+  }
+
   public final class MathHelpersKt {
     method public static float lerp(float start, float stop, float fraction);
     method public static int lerp(int start, int stop, float fraction);
diff --git a/ui/ui-util/api/public_plus_experimental_0.1.0-dev09.txt b/ui/ui-util/api/public_plus_experimental_0.1.0-dev09.txt
index e1cdf07..e8faee5 100644
--- a/ui/ui-util/api/public_plus_experimental_0.1.0-dev09.txt
+++ b/ui/ui-util/api/public_plus_experimental_0.1.0-dev09.txt
@@ -1,10 +1,6 @@
 // Signature format: 3.0
 package androidx.ui.util {
 
-  public final class FastForEachKt {
-    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
-  }
-
   public final class InlineClassHelperKt {
     method public static inline long packFloats(float val1, float val2);
     method public static inline long packInts(int val1, int val2);
@@ -14,6 +10,11 @@
     method public static inline int unpackInt2(long value);
   }
 
+  public final class ListUtilsKt {
+    method public static inline <T> boolean fastAny(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,java.lang.Boolean> predicate);
+    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
+  }
+
   public final class MathHelpersKt {
     method public static float lerp(float start, float stop, float fraction);
     method public static int lerp(int start, int stop, float fraction);
diff --git a/ui/ui-util/api/public_plus_experimental_current.txt b/ui/ui-util/api/public_plus_experimental_current.txt
index e1cdf07..e8faee5 100644
--- a/ui/ui-util/api/public_plus_experimental_current.txt
+++ b/ui/ui-util/api/public_plus_experimental_current.txt
@@ -1,10 +1,6 @@
 // Signature format: 3.0
 package androidx.ui.util {
 
-  public final class FastForEachKt {
-    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
-  }
-
   public final class InlineClassHelperKt {
     method public static inline long packFloats(float val1, float val2);
     method public static inline long packInts(int val1, int val2);
@@ -14,6 +10,11 @@
     method public static inline int unpackInt2(long value);
   }
 
+  public final class ListUtilsKt {
+    method public static inline <T> boolean fastAny(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,java.lang.Boolean> predicate);
+    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
+  }
+
   public final class MathHelpersKt {
     method public static float lerp(float start, float stop, float fraction);
     method public static int lerp(int start, int stop, float fraction);
diff --git a/ui/ui-util/api/restricted_0.1.0-dev09.txt b/ui/ui-util/api/restricted_0.1.0-dev09.txt
index e1cdf07..e8faee5 100644
--- a/ui/ui-util/api/restricted_0.1.0-dev09.txt
+++ b/ui/ui-util/api/restricted_0.1.0-dev09.txt
@@ -1,10 +1,6 @@
 // Signature format: 3.0
 package androidx.ui.util {
 
-  public final class FastForEachKt {
-    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
-  }
-
   public final class InlineClassHelperKt {
     method public static inline long packFloats(float val1, float val2);
     method public static inline long packInts(int val1, int val2);
@@ -14,6 +10,11 @@
     method public static inline int unpackInt2(long value);
   }
 
+  public final class ListUtilsKt {
+    method public static inline <T> boolean fastAny(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,java.lang.Boolean> predicate);
+    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
+  }
+
   public final class MathHelpersKt {
     method public static float lerp(float start, float stop, float fraction);
     method public static int lerp(int start, int stop, float fraction);
diff --git a/ui/ui-util/api/restricted_current.txt b/ui/ui-util/api/restricted_current.txt
index e1cdf07..e8faee5 100644
--- a/ui/ui-util/api/restricted_current.txt
+++ b/ui/ui-util/api/restricted_current.txt
@@ -1,10 +1,6 @@
 // Signature format: 3.0
 package androidx.ui.util {
 
-  public final class FastForEachKt {
-    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
-  }
-
   public final class InlineClassHelperKt {
     method public static inline long packFloats(float val1, float val2);
     method public static inline long packInts(int val1, int val2);
@@ -14,6 +10,11 @@
     method public static inline int unpackInt2(long value);
   }
 
+  public final class ListUtilsKt {
+    method public static inline <T> boolean fastAny(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,java.lang.Boolean> predicate);
+    method public static inline <T> void fastForEach(java.util.List<? extends T>, kotlin.jvm.functions.Function1<? super T,kotlin.Unit> action);
+  }
+
   public final class MathHelpersKt {
     method public static float lerp(float start, float stop, float fraction);
     method public static int lerp(int start, int stop, float fraction);
diff --git a/ui/ui-util/src/main/java/androidx/ui/util/FastForEach.kt b/ui/ui-util/src/main/java/androidx/ui/util/ListUtils.kt
similarity index 80%
rename from ui/ui-util/src/main/java/androidx/ui/util/FastForEach.kt
rename to ui/ui-util/src/main/java/androidx/ui/util/ListUtils.kt
index 9829b6c..f032a4f 100644
--- a/ui/ui-util/src/main/java/androidx/ui/util/FastForEach.kt
+++ b/ui/ui-util/src/main/java/androidx/ui/util/ListUtils.kt
@@ -25,4 +25,12 @@
         val item = get(index)
         action(item)
     }
-}
\ No newline at end of file
+}
+
+/**
+ * Returns `true` if at least one element matches the given [predicate].
+ */
+inline fun <T> List<T>.fastAny(predicate: (T) -> Boolean): Boolean {
+    fastForEach { if (predicate(it)) return true }
+    return false
+}
diff --git a/ui/ui-util/src/test/java/androidx/ui/util/FastForEachTest.kt b/ui/ui-util/src/test/java/androidx/ui/util/ListUtilsTest.kt
similarity index 76%
rename from ui/ui-util/src/test/java/androidx/ui/util/FastForEachTest.kt
rename to ui/ui-util/src/test/java/androidx/ui/util/ListUtilsTest.kt
index c08a696..8d1ae60 100644
--- a/ui/ui-util/src/test/java/androidx/ui/util/FastForEachTest.kt
+++ b/ui/ui-util/src/test/java/androidx/ui/util/ListUtilsTest.kt
@@ -17,12 +17,14 @@
 package androidx.ui.util
 
 import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.junit.runners.JUnit4
 
 @RunWith(JUnit4::class)
-class FastForEachTest {
+class ListUtilsTest {
     @Test
     fun regularIteration() {
         val list = listOf(1, 5, 10)
@@ -49,4 +51,22 @@
         assertEquals(1, otherList[0])
         assertEquals(10, otherList[1])
     }
+
+    @Test
+    fun anyEmpty() {
+        val list = listOf<Int>()
+        assertFalse(list.fastAny { it > 0 })
+    }
+
+    @Test
+    fun anyNotFound() {
+        val list = listOf(0, -1, -500)
+        assertFalse(list.fastAny { it > 0 })
+    }
+
+    @Test
+    fun anyFound() {
+        val list = listOf(0, -1, -500, 1)
+        assertTrue(list.fastAny { it > 0 })
+    }
 }
\ No newline at end of file