Home | History | Annotate | Download | only in providers
      1 /**
      2  * Copyright (c) 2011, Google Inc.
      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 
     17 package com.android.mail.providers;
     18 
     19 import com.google.common.collect.ImmutableSet;
     20 
     21 import java.lang.IllegalArgumentException;
     22 import java.lang.String;
     23 import java.util.Arrays;
     24 import java.util.Set;
     25 
     26 
     27 /**
     28  * A helper class to validate projections for the UIProvider queries.
     29  *
     30  * TODO(pwestbro): Consider creating an abstract ContentProvider that contains this
     31  * functionionality.
     32  */
     33 public class UIProviderValidator {
     34     /**
     35      * Validates and returns the projection that can be used for an account query.
     36      */
     37     public static String[] validateAccountProjection(String[] projection) {
     38         return getValidProjection(projection, UIProvider.ACCOUNTS_PROJECTION);
     39     }
     40 
     41     /**
     42      * Validates and returns the projection that can be used for a folder query.
     43      */
     44     public static String[] validateFolderProjection(String[] projection) {
     45         return getValidProjection(projection, UIProvider.FOLDERS_PROJECTION);
     46     }
     47 
     48     /**
     49      * Validates and returns the projection that can be used for a account cookie query.
     50      */
     51     public static String[] validateAccountCookieProjection(String[] projection) {
     52         return getValidProjection(projection, UIProvider.ACCOUNT_COOKIE_PROJECTION);
     53     }
     54 
     55     /**
     56      * Validates and returns the projection that can be used for a conversation query.
     57      */
     58     public static String[] validateConversationProjection(String[] projection) {
     59         return getValidProjection(projection, UIProvider.CONVERSATION_PROJECTION);
     60     }
     61 
     62     /**
     63      * Validates and returns the projection that can be used for a message query.
     64      */
     65     public static String[] validateMessageProjection(String[] projection) {
     66         return getValidProjection(projection, UIProvider.MESSAGE_PROJECTION);
     67     }
     68 
     69     /**
     70      * Validates and returns the projection that can be used for a attachment query.
     71      */
     72     public static String[] validateAttachmentProjection(String[] projection) {
     73         return getValidProjection(projection, UIProvider.ATTACHMENT_PROJECTION);
     74     }
     75 
     76 
     77     private static String[] getValidProjection(String[] requestedProjection,
     78             String[] allColumnProjection) {
     79         final String[] resultProjection;
     80         if (requestedProjection != null) {
     81             if (isValidProjection(requestedProjection, ImmutableSet.copyOf(allColumnProjection))) {
     82                 // The requested projection is valid, use it.
     83                 resultProjection = requestedProjection;
     84             } else {
     85                 throw new IllegalArgumentException(
     86                         "Invalid projection: " + Arrays.toString(requestedProjection));
     87             }
     88         } else {
     89             // If the caller specified a null projection, they want all columns
     90             resultProjection = allColumnProjection;
     91         }
     92         return resultProjection;
     93     }
     94 
     95     private static boolean isValidProjection(String[] projection, Set<String> validColumns) {
     96         for (String column : projection) {
     97             if (!validColumns.contains(column)) {
     98                 return false;
     99             }
    100         }
    101         return true;
    102     }
    103 }