Home | History | Annotate | Download | only in apicoverage
      1 /*
      2  * Copyright (C) 2010 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 
     17 package com.android.cts.apicoverage;
     18 
     19 import java.util.Collection;
     20 import java.util.Collections;
     21 import java.util.HashMap;
     22 import java.util.Iterator;
     23 import java.util.Map;
     24 import java.util.Map.Entry;
     25 
     26 /** Representation of a package in the API containing classes. */
     27 class ApiPackage implements HasCoverage {
     28 
     29     private final String mName;
     30 
     31     private final Map<String, ApiClass> mApiClassMap = new HashMap<String, ApiClass>();
     32 
     33     ApiPackage(String name) {
     34         mName = name;
     35     }
     36 
     37     @Override
     38     public String getName() {
     39         return mName;
     40     }
     41 
     42     public void addClass(ApiClass apiClass) {
     43         mApiClassMap.put(apiClass.getName(), apiClass);
     44     }
     45 
     46     public ApiClass getClass(String name) {
     47         return mApiClassMap.get(name);
     48     }
     49 
     50     public Collection<ApiClass> getClasses() {
     51         return Collections.unmodifiableCollection(mApiClassMap.values());
     52     }
     53 
     54     public int getNumCoveredMethods() {
     55         int covered = 0;
     56         for (ApiClass apiClass : mApiClassMap.values()) {
     57             covered += apiClass.getNumCoveredMethods();
     58         }
     59         return covered;
     60     }
     61 
     62     public int getTotalMethods() {
     63         int total = 0;
     64         for (ApiClass apiClass : mApiClassMap.values()) {
     65             total += apiClass.getTotalMethods();
     66         }
     67         return total;
     68     }
     69 
     70     @Override
     71     public float getCoveragePercentage() {
     72         return (float) getNumCoveredMethods() / getTotalMethods() * 100;
     73     }
     74 
     75     @Override
     76     public int getMemberSize() {
     77         return getTotalMethods();
     78     }
     79 
     80     /** Iterate through all classes and add superclass. */
     81     public void resolveSuperClasses(Map<String, ApiPackage> packageMap) {
     82         Iterator<Entry<String, ApiClass>> it = mApiClassMap.entrySet().iterator();
     83         while (it.hasNext()) {
     84             Map.Entry<String, ApiClass> entry = it.next();
     85             ApiClass apiClass = entry.getValue();
     86             if (apiClass.getSuperClassName() != null) {
     87                 String superClassName = apiClass.getSuperClassName();
     88                 // Split the fully qualified class name into package and class name.
     89                 String packageName = superClassName.substring(0, superClassName.lastIndexOf('.'));
     90                 String className = superClassName.substring(
     91                         superClassName.lastIndexOf('.') + 1, superClassName.length());
     92                 if (packageMap.containsKey(packageName)) {
     93                     ApiPackage apiPackage = packageMap.get(packageName);
     94                     ApiClass superClass = apiPackage.getClass(className);
     95                     if (superClass != null) {
     96                         // Add the super class
     97                         apiClass.setSuperClass(superClass);
     98                     }
     99                 }
    100             }
    101         }
    102     }
    103 }
    104