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.apicommon;
18 
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentHashMap;
25 
26 
27 /** Representation of a method in the API with parameters (arguments) and a return value. */
28 public class ApiMethod implements Comparable<ApiMethod> {
29 
30     private final String mName;
31 
32     private final List<String> mParameterTypes;
33 
34     private final String mReturnType;
35 
36     private final boolean mDeprecated;
37 
38     private final String mVisibility;
39 
40     private final boolean mStaticMethod;
41 
42     private final boolean mFinalMethod;
43 
44     private final boolean mAbstractMethod;
45 
46     // A list of test APKs (aka CTS modules) that use this method.
47     private final Map<String, Boolean> mCoveredWith = new ConcurrentHashMap<>();
48 
49     // A list of CTS test methods that call this API method.
50     private final Map<String, Boolean> mCoveredTests = new ConcurrentHashMap<>();
51 
ApiMethod( String name, List<String> parameterTypes, String returnType, boolean deprecated, String visibility, boolean staticMethod, boolean finalMethod, boolean abstractMethod)52     public ApiMethod(
53             String name,
54             List<String> parameterTypes,
55             String returnType,
56             boolean deprecated,
57             String visibility,
58             boolean staticMethod,
59             boolean finalMethod,
60             boolean abstractMethod) {
61         mName = name;
62         mParameterTypes = new ArrayList<String>(parameterTypes);
63         mReturnType = returnType;
64         mDeprecated = deprecated;
65         mVisibility = visibility;
66         mStaticMethod = staticMethod;
67         mFinalMethod = finalMethod;
68         mAbstractMethod = abstractMethod;
69     }
70 
71     @Override
compareTo(ApiMethod another)72     public int compareTo(ApiMethod another) {
73         return mName.compareTo(another.mName);
74     }
75 
getName()76     public String getName() {
77         return mName;
78     }
79 
getParameterTypes()80     public List<String> getParameterTypes() {
81         return Collections.unmodifiableList(mParameterTypes);
82     }
83 
getReturnType()84     public String getReturnType() {
85         return mReturnType;
86     }
87 
isDeprecated()88     public boolean isDeprecated() {
89         return mDeprecated;
90     }
91 
isCovered()92     public boolean isCovered() {
93         return !mCoveredWith.isEmpty();
94     }
95 
getVisibility()96     public String getVisibility() {
97         return mVisibility;
98     }
99 
isAbstractMethod()100     public boolean isAbstractMethod() {
101         return mAbstractMethod;
102     }
103 
isStaticMethod()104     public boolean isStaticMethod() {
105         return mStaticMethod;
106     }
107 
isFinalMethod()108     public boolean isFinalMethod() {
109         return mFinalMethod;
110     }
111 
getCoveredWith()112     public Set<String> getCoveredWith() {
113         return mCoveredWith.keySet();
114     }
115 
getCoveredTests()116     public Set<String> getCoveredTests() {
117         return mCoveredTests.keySet();
118     }
119 
setCovered(String coveredWithModule)120     public void setCovered(String coveredWithModule) {
121         if (coveredWithModule.endsWith(".apk")) {
122             coveredWithModule = coveredWithModule.substring(0, coveredWithModule.length() - 4);
123         }
124 
125         mCoveredWith.put(coveredWithModule, true);
126     }
127 
128     /** Adds a test method that is calling this API. */
setCoveredTest(String coveredTest)129     public void setCoveredTest(String coveredTest) {
130         mCoveredTests.put(coveredTest, true);
131     }
132 }
133