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
17package com.android.cts.apicoverage;
18
19import java.util.Collection;
20import java.util.Collections;
21import java.util.HashMap;
22import java.util.Iterator;
23import java.util.Map;
24import java.util.Map.Entry;
25
26/** Representation of a package in the API containing classes. */
27class 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    public void removeEmptyAbstractClasses() {
76        Iterator<Entry<String, ApiClass>> it = mApiClassMap.entrySet().iterator();
77        while (it.hasNext()) {
78            Map.Entry<String, ApiClass> entry = it.next();
79            ApiClass cls = entry.getValue();
80            if (cls.isAbstract() && (cls.getTotalMethods() == 0)) {
81                // this is essentially interface
82                it.remove();
83            }
84        }
85    }
86}
87