From 57e490cf8cf85932c11208c64bbd2b35315ffbc8 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Thu, 9 Feb 2017 14:47:53 +0000 Subject: Partial Revert "Replace com.android.internal.util.Predicate with java.util.function.Predicate" This partially reverts commit 3e5fbca0c57742f28f5c7e6bda6c3500b9471b47. Bug: 35187548 Bug: 30188076 Bug: 35089332 Test: make checkbuild Change-Id: I7ab4279aab604e3d56003b0a59867948aebabd28 --- core/java/com/android/internal/util/Predicate.java | 32 ++++++ .../java/com/android/internal/util/Predicates.java | 125 ++++++++++++++++++++ .../com/android/internal/util/PredicatesTest.java | 74 ++++++++++++ .../src/com/android/internal/util/Predicate.java | 32 ------ .../src/com/android/internal/util/Predicates.java | 126 --------------------- .../com/android/internal/util/PredicatesTest.java | 74 ------------ 6 files changed, 231 insertions(+), 232 deletions(-) create mode 100644 core/java/com/android/internal/util/Predicate.java create mode 100644 core/java/com/android/internal/util/Predicates.java create mode 100644 core/tests/utiltests/src/com/android/internal/util/PredicatesTest.java delete mode 100644 test-runner/src/com/android/internal/util/Predicate.java delete mode 100644 test-runner/src/com/android/internal/util/Predicates.java delete mode 100644 test-runner/tests/src/com/android/internal/util/PredicatesTest.java diff --git a/core/java/com/android/internal/util/Predicate.java b/core/java/com/android/internal/util/Predicate.java new file mode 100644 index 000000000000..bc6d6b31dd28 --- /dev/null +++ b/core/java/com/android/internal/util/Predicate.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + +/** + * A Predicate can determine a true or false value for any input of its + * parameterized type. For example, a {@code RegexPredicate} might implement + * {@code Predicate}, and return true for any String that matches its + * given regular expression. + *

+ *

+ * Implementors of Predicate which may cause side effects upon evaluation are + * strongly encouraged to state this fact clearly in their API documentation. + */ +public interface Predicate { + + boolean apply(T t); +} diff --git a/core/java/com/android/internal/util/Predicates.java b/core/java/com/android/internal/util/Predicates.java new file mode 100644 index 000000000000..c006564f129a --- /dev/null +++ b/core/java/com/android/internal/util/Predicates.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + +import java.util.Arrays; + +/** + * Predicates contains static methods for creating the standard set of + * {@code Predicate} objects. + */ +public class Predicates { + + private Predicates() { + } + + /** + * Returns a Predicate that evaluates to true iff each of its components + * evaluates to true. The components are evaluated in order, and evaluation + * will be "short-circuited" as soon as the answer is determined. + */ + public static Predicate and(Predicate... components) { + return Predicates.and(Arrays.asList(components)); + } + + /** + * Returns a Predicate that evaluates to true iff each of its components + * evaluates to true. The components are evaluated in order, and evaluation + * will be "short-circuited" as soon as the answer is determined. Does not + * defensively copy the iterable passed in, so future changes to it will alter + * the behavior of this Predicate. If components is empty, the returned + * Predicate will always evaluate to true. + */ + public static Predicate and(Iterable> components) { + return new AndPredicate(components); + } + + /** + * Returns a Predicate that evaluates to true iff any one of its components + * evaluates to true. The components are evaluated in order, and evaluation + * will be "short-circuited" as soon as the answer is determined. + */ + public static Predicate or(Predicate... components) { + return Predicates.or(Arrays.asList(components)); + } + + /** + * Returns a Predicate that evaluates to true iff any one of its components + * evaluates to true. The components are evaluated in order, and evaluation + * will be "short-circuited" as soon as the answer is determined. Does not + * defensively copy the iterable passed in, so future changes to it will alter + * the behavior of this Predicate. If components is empty, the returned + * Predicate will always evaluate to false. + */ + public static Predicate or(Iterable> components) { + return new OrPredicate(components); + } + + /** + * Returns a Predicate that evaluates to true iff the given Predicate + * evaluates to false. + */ + public static Predicate not(Predicate predicate) { + return new NotPredicate(predicate); + } + + private static class AndPredicate implements Predicate { + private final Iterable> components; + + private AndPredicate(Iterable> components) { + this.components = components; + } + + public boolean apply(T t) { + for (Predicate predicate : components) { + if (!predicate.apply(t)) { + return false; + } + } + return true; + } + } + + private static class OrPredicate implements Predicate { + private final Iterable> components; + + private OrPredicate(Iterable> components) { + this.components = components; + } + + public boolean apply(T t) { + for (Predicate predicate : components) { + if (predicate.apply(t)) { + return true; + } + } + return false; + } + } + + private static class NotPredicate implements Predicate { + private final Predicate predicate; + + private NotPredicate(Predicate predicate) { + this.predicate = predicate; + } + + public boolean apply(T t) { + return !predicate.apply(t); + } + } +} diff --git a/core/tests/utiltests/src/com/android/internal/util/PredicatesTest.java b/core/tests/utiltests/src/com/android/internal/util/PredicatesTest.java new file mode 100644 index 000000000000..c46ff051dd33 --- /dev/null +++ b/core/tests/utiltests/src/com/android/internal/util/PredicatesTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.internal.util; + +import junit.framework.TestCase; + +import java.util.ArrayList; +import java.util.Collections; + +public class PredicatesTest extends TestCase { + + private static final Predicate TRUE = new Predicate() { + public boolean apply(Object o) { + return true; + } + }; + + private static final Predicate FALSE = new Predicate() { + public boolean apply(Object o) { + return false; + } + }; + + public void testAndPredicate_AllConditionsTrue() throws Exception { + assertTrue(Predicates.and(newArrayList(TRUE)).apply(null)); + assertTrue(Predicates.and(newArrayList(TRUE, TRUE)).apply(null)); + } + + public void testAndPredicate_AtLeastOneConditionIsFalse() throws Exception { + assertFalse(Predicates.and(newArrayList(FALSE, TRUE, TRUE)).apply(null)); + assertFalse(Predicates.and(newArrayList(TRUE, FALSE, TRUE)).apply(null)); + assertFalse(Predicates.and(newArrayList(TRUE, TRUE, FALSE)).apply(null)); + } + + public void testOrPredicate_AllConditionsTrue() throws Exception { + assertTrue(Predicates.or(newArrayList(TRUE, TRUE, TRUE)).apply(null)); + } + + public void testOrPredicate_AllConditionsFalse() throws Exception { + assertFalse(Predicates.or(newArrayList(FALSE, FALSE, FALSE)).apply(null)); + } + + public void testOrPredicate_AtLeastOneConditionIsTrue() throws Exception { + assertTrue(Predicates.or(newArrayList(TRUE, FALSE, FALSE)).apply(null)); + assertTrue(Predicates.or(newArrayList(FALSE, TRUE, FALSE)).apply(null)); + assertTrue(Predicates.or(newArrayList(FALSE, FALSE, TRUE)).apply(null)); + } + + public void testNotPredicate() throws Exception { + assertTrue(Predicates.not(FALSE).apply(null)); + assertFalse(Predicates.not(TRUE).apply(null)); + } + + private static ArrayList newArrayList(E... elements) { + ArrayList list = new ArrayList(); + Collections.addAll(list, elements); + return list; + } + +} diff --git a/test-runner/src/com/android/internal/util/Predicate.java b/test-runner/src/com/android/internal/util/Predicate.java deleted file mode 100644 index bc6d6b31dd28..000000000000 --- a/test-runner/src/com/android/internal/util/Predicate.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.util; - -/** - * A Predicate can determine a true or false value for any input of its - * parameterized type. For example, a {@code RegexPredicate} might implement - * {@code Predicate}, and return true for any String that matches its - * given regular expression. - *

- *

- * Implementors of Predicate which may cause side effects upon evaluation are - * strongly encouraged to state this fact clearly in their API documentation. - */ -public interface Predicate { - - boolean apply(T t); -} diff --git a/test-runner/src/com/android/internal/util/Predicates.java b/test-runner/src/com/android/internal/util/Predicates.java deleted file mode 100644 index 30b8dc6b6ee2..000000000000 --- a/test-runner/src/com/android/internal/util/Predicates.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.util; - -import java.util.Arrays; - -/** - * Predicates contains static methods for creating the standard set of - * {@code Predicate} objects. - * @hide - */ -public class Predicates { - - private Predicates() { - } - - /** - * Returns a Predicate that evaluates to true iff each of its components - * evaluates to true. The components are evaluated in order, and evaluation - * will be "short-circuited" as soon as the answer is determined. - */ - public static Predicate and(Predicate... components) { - return Predicates.and(Arrays.asList(components)); - } - - /** - * Returns a Predicate that evaluates to true iff each of its components - * evaluates to true. The components are evaluated in order, and evaluation - * will be "short-circuited" as soon as the answer is determined. Does not - * defensively copy the iterable passed in, so future changes to it will alter - * the behavior of this Predicate. If components is empty, the returned - * Predicate will always evaluate to true. - */ - public static Predicate and(Iterable> components) { - return new AndPredicate(components); - } - - /** - * Returns a Predicate that evaluates to true iff any one of its components - * evaluates to true. The components are evaluated in order, and evaluation - * will be "short-circuited" as soon as the answer is determined. - */ - public static Predicate or(Predicate... components) { - return Predicates.or(Arrays.asList(components)); - } - - /** - * Returns a Predicate that evaluates to true iff any one of its components - * evaluates to true. The components are evaluated in order, and evaluation - * will be "short-circuited" as soon as the answer is determined. Does not - * defensively copy the iterable passed in, so future changes to it will alter - * the behavior of this Predicate. If components is empty, the returned - * Predicate will always evaluate to false. - */ - public static Predicate or(Iterable> components) { - return new OrPredicate(components); - } - - /** - * Returns a Predicate that evaluates to true iff the given Predicate - * evaluates to false. - */ - public static Predicate not(Predicate predicate) { - return new NotPredicate(predicate); - } - - private static class AndPredicate implements Predicate { - private final Iterable> components; - - private AndPredicate(Iterable> components) { - this.components = components; - } - - public boolean apply(T t) { - for (Predicate predicate : components) { - if (!predicate.apply(t)) { - return false; - } - } - return true; - } - } - - private static class OrPredicate implements Predicate { - private final Iterable> components; - - private OrPredicate(Iterable> components) { - this.components = components; - } - - public boolean apply(T t) { - for (Predicate predicate : components) { - if (predicate.apply(t)) { - return true; - } - } - return false; - } - } - - private static class NotPredicate implements Predicate { - private final Predicate predicate; - - private NotPredicate(Predicate predicate) { - this.predicate = predicate; - } - - public boolean apply(T t) { - return !predicate.apply(t); - } - } -} diff --git a/test-runner/tests/src/com/android/internal/util/PredicatesTest.java b/test-runner/tests/src/com/android/internal/util/PredicatesTest.java deleted file mode 100644 index c46ff051dd33..000000000000 --- a/test-runner/tests/src/com/android/internal/util/PredicatesTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.internal.util; - -import junit.framework.TestCase; - -import java.util.ArrayList; -import java.util.Collections; - -public class PredicatesTest extends TestCase { - - private static final Predicate TRUE = new Predicate() { - public boolean apply(Object o) { - return true; - } - }; - - private static final Predicate FALSE = new Predicate() { - public boolean apply(Object o) { - return false; - } - }; - - public void testAndPredicate_AllConditionsTrue() throws Exception { - assertTrue(Predicates.and(newArrayList(TRUE)).apply(null)); - assertTrue(Predicates.and(newArrayList(TRUE, TRUE)).apply(null)); - } - - public void testAndPredicate_AtLeastOneConditionIsFalse() throws Exception { - assertFalse(Predicates.and(newArrayList(FALSE, TRUE, TRUE)).apply(null)); - assertFalse(Predicates.and(newArrayList(TRUE, FALSE, TRUE)).apply(null)); - assertFalse(Predicates.and(newArrayList(TRUE, TRUE, FALSE)).apply(null)); - } - - public void testOrPredicate_AllConditionsTrue() throws Exception { - assertTrue(Predicates.or(newArrayList(TRUE, TRUE, TRUE)).apply(null)); - } - - public void testOrPredicate_AllConditionsFalse() throws Exception { - assertFalse(Predicates.or(newArrayList(FALSE, FALSE, FALSE)).apply(null)); - } - - public void testOrPredicate_AtLeastOneConditionIsTrue() throws Exception { - assertTrue(Predicates.or(newArrayList(TRUE, FALSE, FALSE)).apply(null)); - assertTrue(Predicates.or(newArrayList(FALSE, TRUE, FALSE)).apply(null)); - assertTrue(Predicates.or(newArrayList(FALSE, FALSE, TRUE)).apply(null)); - } - - public void testNotPredicate() throws Exception { - assertTrue(Predicates.not(FALSE).apply(null)); - assertFalse(Predicates.not(TRUE).apply(null)); - } - - private static ArrayList newArrayList(E... elements) { - ArrayList list = new ArrayList(); - Collections.addAll(list, elements); - return list; - } - -} -- cgit v1.2.3-59-g8ed1b