Create a utility class for dealing with null values
Change-Id: I11cb97dbbd498ed573e65afe2b623cbbeb036bbd
diff --git a/src/com/android/tradefed/util/NullUtil.java b/src/com/android/tradefed/util/NullUtil.java
new file mode 100644
index 0000000..1fdd836
--- /dev/null
+++ b/src/com/android/tradefed/util/NullUtil.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2012 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.tradefed.util;
+
+/**
+ * A class with utility functions to help with dealing with <code>null</code>
+ */
+public class NullUtil {
+ /**
+ * Counts <code>null</code> objects in the passed set
+ */
+ public static int countNulls(Object... objs) {
+ int count = 0;
+ for (Object obj : objs) {
+ if (obj == null) count++;
+ }
+ return count;
+ }
+
+ /**
+ * Counts non-<code>null</code> objects in the passed set
+ */
+ public static int countNonNulls(Object... objs) {
+ int count = 0;
+ for (Object obj : objs) {
+ if (obj != null) count++;
+ }
+ return count;
+ }
+
+ /**
+ * Checks if all objects are null. Uses short-circuit logic, so may be more efficient than
+ * {@link #countNonNulls(Object...)} for sets of many objects.
+ *
+ * @return <code>false</code> if any passed objects are non-null. <code>true</code> otherwise.
+ * In particular, returns true of no objects are passed.
+ */
+ public static boolean allNull(Object... objs) {
+ for (Object obj : objs) {
+ if (obj != null) return false;
+ }
+ return true;
+ }
+
+ /**
+ * Checks if exactly one object is non-null. Uses short-circuit logic, so may be more efficient
+ * than {@link #countNonNulls(Object...)} for sets of many objects.
+ *
+ * @return <code>true</code> if there is exactly one non-<code>null</code> object in the list.
+ * <code>false</code> otherwise.
+ */
+ public static boolean singleNonNull(Object... objs) {
+ int nonNullCount = 0;
+ for (Object obj : objs) {
+ if (obj != null) nonNullCount++;
+ if (nonNullCount > 1) return false;
+ }
+ return nonNullCount == 1;
+ }
+
+ /**
+ * Check if every object is null, or every object is non-null. Uses short-circuit logic, so may
+ * be more efficient than {@link #countNulls(Object...)} and {@link #countNonNulls(Object...)}
+ * for sets of many objects.
+ *
+ * @return <code>true</code> if every object is <code>null</code> or if every object is
+ * non-<code>null</code>. <code>false</code> otherwise.
+ */
+ public static boolean isHomogeneousSet(Object... objs) {
+ if (objs.length == 0) return true;
+
+ final boolean expectNull = (objs[0] == null);
+ for (Object obj : objs) {
+ if (expectNull ^ (obj == null)) {
+ // xor tells us when the parities differ, which is only when objs[0] was null and
+ // obj is non-null, or objs[0] was non-null and obj is null
+ return false;
+ }
+ }
+ return true;
+ }
+}
diff --git a/tests/src/com/android/tradefed/UnitTests.java b/tests/src/com/android/tradefed/UnitTests.java
index 62d384d..c23ce4a 100644
--- a/tests/src/com/android/tradefed/UnitTests.java
+++ b/tests/src/com/android/tradefed/UnitTests.java
@@ -84,6 +84,7 @@
import com.android.tradefed.util.EmailTest;
import com.android.tradefed.util.FileUtilTest;
import com.android.tradefed.util.MultiMapTest;
+import com.android.tradefed.util.NullUtilTest;
import com.android.tradefed.util.QuotationAwareTokenizerTest;
import com.android.tradefed.util.RegexTrieTest;
import com.android.tradefed.util.RunUtilTest;
@@ -190,6 +191,7 @@
addTestSuite(EmailTest.class);
addTestSuite(FileUtilTest.class);
addTestSuite(MultiMapTest.class);
+ addTestSuite(NullUtilTest.class);
addTestSuite(QuotationAwareTokenizerTest.class);
addTestSuite(RegexTrieTest.class);
addTestSuite(RunUtilTest.class);
diff --git a/tests/src/com/android/tradefed/util/NullUtilTest.java b/tests/src/com/android/tradefed/util/NullUtilTest.java
new file mode 100644
index 0000000..358d150
--- /dev/null
+++ b/tests/src/com/android/tradefed/util/NullUtilTest.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2012 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.tradefed.util;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link NullUtil}
+ */
+public class NullUtilTest extends TestCase {
+ /**
+ * Make sure countNulls works as expected
+ */
+ public void testCountNulls() throws Exception {
+ assertEquals(0, NullUtil.countNulls());
+ assertEquals(0, NullUtil.countNulls("hi"));
+
+ assertEquals(1, NullUtil.countNulls((Object)null));
+ assertEquals(1, NullUtil.countNulls(null, "hi"));
+ assertEquals(1, NullUtil.countNulls("hi", null));
+
+ assertEquals(2, NullUtil.countNulls(null, null));
+ }
+
+ /**
+ * Make sure countNonNulls works as expected
+ */
+ public void testCountNonNulls() throws Exception {
+ assertEquals(0, NullUtil.countNonNulls());
+ assertEquals(0, NullUtil.countNonNulls((Object)null));
+
+ assertEquals(1, NullUtil.countNonNulls("hi"));
+ assertEquals(1, NullUtil.countNonNulls(null, "hi"));
+ assertEquals(1, NullUtil.countNonNulls("hi", null));
+
+ assertEquals(2, NullUtil.countNonNulls("hi", "hi"));
+ }
+
+ /**
+ * Make sure allNull works as expected
+ */
+ public void testAllNull() throws Exception {
+ assertTrue(NullUtil.allNull());
+ assertTrue(NullUtil.allNull((Object)null));
+ assertTrue(NullUtil.allNull(null, null));
+
+ assertFalse(NullUtil.allNull(1));
+ assertFalse(NullUtil.allNull(1, null, null));
+ assertFalse(NullUtil.allNull(null, null, 1));
+ }
+
+ /**
+ * Ensure that we return true only when exactly one passed value is non-null
+ */
+ public void testSingleNonNull() throws Exception {
+ assertFalse(NullUtil.singleNonNull());
+
+ assertFalse(NullUtil.singleNonNull(null, null, null));
+ assertTrue(NullUtil.singleNonNull(1, null, null));
+ assertTrue(NullUtil.singleNonNull(null, null, 1));
+ assertFalse(NullUtil.singleNonNull(1, null, 1));
+ assertFalse(NullUtil.singleNonNull(1, 1, 1));
+ }
+
+ /**
+ * Ensure that we return true only when it is not the case that a null object is simultaneously
+ * present with a non-null object
+ */
+ public void testHomogeneousSet() throws Exception {
+ assertTrue(NullUtil.isHomogeneousSet());
+
+ assertTrue(NullUtil.isHomogeneousSet(1));
+ assertTrue(NullUtil.isHomogeneousSet((Object)null));
+
+ assertTrue(NullUtil.isHomogeneousSet(1, 1));
+ assertTrue(NullUtil.isHomogeneousSet(null, null));
+
+ assertFalse(NullUtil.isHomogeneousSet(1, null));
+ assertFalse(NullUtil.isHomogeneousSet(null, 1));
+ }
+}
+