Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.util;
     17 
     18 /**
     19  * A class with utility functions to help with dealing with <code>null</code>
     20  */
     21 public class NullUtil {
     22     /**
     23      * Counts <code>null</code> objects in the passed set
     24      */
     25     public static int countNulls(Object... objs) {
     26         int count = 0;
     27         for (Object obj : objs) {
     28             if (obj == null) count++;
     29         }
     30         return count;
     31     }
     32 
     33     /**
     34      * Counts non-<code>null</code> objects in the passed set
     35      */
     36     public static int countNonNulls(Object... objs) {
     37         int count = 0;
     38         for (Object obj : objs) {
     39             if (obj != null) count++;
     40         }
     41         return count;
     42     }
     43 
     44     /**
     45      * Checks if all objects are null.  Uses short-circuit logic, so may be more efficient than
     46      * {@link #countNonNulls(Object...)} for sets of many objects.
     47      *
     48      * @return <code>false</code> if any passed objects are non-null.  <code>true</code> otherwise.
     49      *         In particular, returns true of no objects are passed.
     50      */
     51     public static boolean allNull(Object... objs) {
     52         for (Object obj : objs) {
     53             if (obj != null) return false;
     54         }
     55         return true;
     56     }
     57 
     58     /**
     59      * Checks if exactly one object is non-null.  Uses short-circuit logic, so may be more efficient
     60      * than {@link #countNonNulls(Object...)} for sets of many objects.
     61      *
     62      * @return <code>true</code> if there is exactly one non-<code>null</code> object in the list.
     63      *         <code>false</code> otherwise.
     64      */
     65     public static boolean singleNonNull(Object... objs) {
     66         int nonNullCount = 0;
     67         for (Object obj : objs) {
     68             if (obj != null) nonNullCount++;
     69             if (nonNullCount > 1) return false;
     70         }
     71         return nonNullCount == 1;
     72     }
     73 
     74     /**
     75      * Check if every object is null, or every object is non-null.  Uses short-circuit logic, so may
     76      * be more efficient than {@link #countNulls(Object...)} and {@link #countNonNulls(Object...)}
     77      * for sets of many objects.
     78      *
     79      * @return <code>true</code> if every object is <code>null</code> or if every object is
     80      *         non-<code>null</code>.  <code>false</code> otherwise.
     81      */
     82     public static boolean isHomogeneousSet(Object... objs) {
     83         if (objs.length == 0) return true;
     84 
     85         final boolean expectNull = (objs[0] == null);
     86         for (Object obj : objs) {
     87             if (expectNull ^ (obj == null)) {
     88                 // xor tells us when the parities differ, which is only when objs[0] was null and
     89                 // obj is non-null, or objs[0] was non-null and obj is null
     90                 return false;
     91             }
     92         }
     93         return true;
     94     }
     95 }
     96