Never use null collections

Don't null out collections; clear() them instead.

Change-Id: I738fa4b2fe5cbb3a741ee85884eb02e13d3e85c5
diff --git a/src/com/android/telecomm/BinderDeallocator.java b/src/com/android/telecomm/BinderDeallocator.java
index ab7db25..ae62ec3 100644
--- a/src/com/android/telecomm/BinderDeallocator.java
+++ b/src/com/android/telecomm/BinderDeallocator.java
@@ -65,7 +65,7 @@
      * The set of all known binders, either in use or potentially about to be used.
      */
     @SuppressWarnings("rawtypes")
-    private Set<ServiceBinder> mBinders = Sets.newHashSet();
+    private final Set<ServiceBinder> mBinders = Sets.newHashSet();
 
     /**
      * Accounts for the action entering a critical section (i.e. potentially needing access to
diff --git a/src/com/android/telecomm/CallServiceRepository.java b/src/com/android/telecomm/CallServiceRepository.java
index 5567e51..c55fbf2 100644
--- a/src/com/android/telecomm/CallServiceRepository.java
+++ b/src/com/android/telecomm/CallServiceRepository.java
@@ -98,7 +98,7 @@
      * responses are received, the partial (potentially empty) set gets passed (to the switchboard)
      * instead. Entries are removed from this set as providers are processed.
      */
-    private Set<ComponentName> mOutstandingProviders;
+    private final Set<ComponentName> mOutstandingProviders = Sets.newHashSet();
 
     /**
      * The map of call-service wrappers keyed by their ComponentName.  Used to ensure at most one
@@ -107,7 +107,7 @@
      * include only active call services (ones that are associated with one or more active calls)
      * upon {@link #purgeInactiveCallServices()} invocations.
      */
-    private Map<ComponentName, CallServiceWrapper> mCallServices = Maps.newHashMap();
+    private final Map<ComponentName, CallServiceWrapper> mCallServices = Maps.newHashMap();
 
     /**
      * Persists the specified parameters.
@@ -149,7 +149,7 @@
         mLookupId = lookupId;
         mIsLookupInProgress = true;
 
-        mOutstandingProviders = Sets.newHashSet();
+        mOutstandingProviders.clear();
         for (ComponentName name : providerNames) {
             mOutstandingProviders.add(name);
             bindProvider(name);
@@ -347,7 +347,7 @@
      */
     private void terminateLookup() {
         mHandler.removeCallbacks(mLookupTerminator);
-        mOutstandingProviders = null;
+        mOutstandingProviders.clear();
 
         updateSwitchboard();
         mIsLookupInProgress = false;
diff --git a/src/com/android/telecomm/CallServiceSelectorRepository.java b/src/com/android/telecomm/CallServiceSelectorRepository.java
index 13249c5..b286a79 100644
--- a/src/com/android/telecomm/CallServiceSelectorRepository.java
+++ b/src/com/android/telecomm/CallServiceSelectorRepository.java
@@ -91,7 +91,7 @@
      * The set of bound call-service selectors.  Only populated via initiateLookup scenarios.
      * Selectors should only be removed upon unbinding.
      */
-    private Set<ICallServiceSelector> mSelectorRegistry = Sets.newHashSet();
+    private final Set<ICallServiceSelector> mSelectorRegistry = Sets.newHashSet();
 
     /**
      * Stores the names of the selectors to bind to in one lookup cycle.  The set size represents
@@ -103,7 +103,7 @@
      * selectors do not require finding and hence are excluded from this set.  Also note that
      * selectors are removed from this set as they register.
      */
-    private Set<ComponentName> mUnregisteredSelectors;
+    private final Set<ComponentName> mUnregisteredSelectors = Sets.newHashSet();
 
     /**
      * Persists the specified parameters and initializes the new instance.
@@ -136,7 +136,7 @@
 
         mLookupId = lookupId;
         mIsLookupInProgress = true;
-        mUnregisteredSelectors = Sets.newHashSet();
+        mUnregisteredSelectors.clear();
 
         for (ComponentName name : selectorNames) {
             if (!mSelectorRegistry.contains(name)) {
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index 7381bdc..7102cf4 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -79,9 +79,9 @@
 
     private VoicemailManager mVoicemailManager;
 
-    private List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
+    private final List<OutgoingCallValidator> mOutgoingCallValidators = Lists.newArrayList();
 
-    private List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
+    private final List<IncomingCallValidator> mIncomingCallValidators = Lists.newArrayList();
 
     /**
      * Initializes the required Telecomm components.
diff --git a/src/com/android/telecomm/OutgoingCallProcessor.java b/src/com/android/telecomm/OutgoingCallProcessor.java
index a0875c9..aa0a0bb 100644
--- a/src/com/android/telecomm/OutgoingCallProcessor.java
+++ b/src/com/android/telecomm/OutgoingCallProcessor.java
@@ -269,7 +269,7 @@
             return;
         }
 
-        if (mCallServiceDescriptorIterator.hasNext()) {
+        if (mCallServiceDescriptorIterator != null && mCallServiceDescriptorIterator.hasNext()) {
             CallServiceDescriptor descriptor = mCallServiceDescriptorIterator.next();
             final CallServiceWrapper callService =
                     mCallServicesById.get(descriptor.getCallServiceId());
diff --git a/src/com/android/telecomm/Switchboard.java b/src/com/android/telecomm/Switchboard.java
index a9889c5..2ef58e3 100644
--- a/src/com/android/telecomm/Switchboard.java
+++ b/src/com/android/telecomm/Switchboard.java
@@ -94,14 +94,14 @@
      * {@link CallServiceRepository}.  Populated during call-service lookup cycles as part of the
      * {@link #placeOutgoingCall} flow and cleared upon zero-remaining new/pending outgoing calls.
      */
-    private Set<CallServiceWrapper> mCallServices;
+    private final Set<CallServiceWrapper> mCallServices = Sets.newHashSet();
 
     /**
      * The set of currently available call-service-selector implementations,
      * see {@link CallServiceSelectorRepository}.
-     * TODO(gilad): Null out once the active-call count goes to zero.
+     * TODO(gilad): Clear once the active-call count goes to zero.
      */
-    private Set<ICallServiceSelector> mSelectors;
+    private final Set<ICallServiceSelector> mSelectors = Sets.newHashSet();
 
     /**
      * The current lookup-cycle ID used with the repositories. Incremented with each invocation
@@ -139,8 +139,8 @@
         // reset, (5) the selector lookup completes but the call-services are missing.  This should
         // be okay since the call-service lookup completed. Specifically the already-available call
         // services are cached and will be provided in response to the second lookup cycle.
-        mCallServices = null;
-        mSelectors = null;
+        mCallServices.clear();
+        mSelectors.clear();
 
         mNewOutgoingCalls.add(call);
 
@@ -180,7 +180,8 @@
     void setCallServices(Set<CallServiceWrapper> callServices) {
         mBinderDeallocator.updateBinders(mCallServices);
 
-        mCallServices = callServices;
+        mCallServices.clear();
+        mCallServices.addAll(callServices);
         processNewOutgoingCalls();
     }
 
@@ -202,7 +203,8 @@
         // emergency calls) and order the entire set prior to the assignment below. If the
         // built-in selectors can be implemented in a manner that does not require binding,
         // that's probably preferred.  May want to use a LinkedHashSet for the sorted set.
-        mSelectors = selectors;
+        mSelectors.clear();
+        mSelectors.addAll(selectors);
         processNewOutgoingCalls();
     }
 
@@ -283,7 +285,7 @@
      * Attempts to process the next new outgoing calls that have not yet been expired.
      */
     private void processNewOutgoingCalls() {
-        if (isNullOrEmpty(mCallServices) || isNullOrEmpty(mSelectors)) {
+        if (mCallServices.isEmpty() || mSelectors.isEmpty()) {
             // At least one call service and one selector are required to process outgoing calls.
             return;
         }
@@ -305,9 +307,6 @@
      * @param call The call to place.
      */
     private void processNewOutgoingCall(Call call) {
-        Preconditions.checkNotNull(mCallServices);
-        Preconditions.checkNotNull(mSelectors);
-
         // Convert to (duplicate-free) list to aid index-based iteration, see the comment under
         // setSelectors regarding using LinkedHashSet instead.
         List<ICallServiceSelector> selectors = Lists.newArrayList();
@@ -365,15 +364,4 @@
             }
         }
     }
-
-    /**
-     * Determines whether or not the specified collection is either null or empty.
-     *
-     * @param collection Either null or the collection object to be evaluated.
-     * @return True if the collection is null or empty.
-     */
-    @SuppressWarnings("rawtypes")
-    private boolean isNullOrEmpty(Collection collection) {
-        return collection == null || collection.isEmpty();
-    }
 }