1/*
2 * Copyright (C) 2010 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
17package com.google.doclava.apicheck;
18
19import com.google.doclava.ClassInfo;
20import com.google.doclava.Errors;
21import com.google.doclava.PackageInfo;
22import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.Map;
25
26public class ApiInfo {
27
28  private HashMap<String, PackageInfo> mPackages
29      = new HashMap<String, PackageInfo>();
30  private HashMap<String, ClassInfo> mAllClasses
31      = new HashMap<String, ClassInfo>();
32  private Map<ClassInfo,String> mClassToSuper
33      = new HashMap<ClassInfo, String>();
34  private Map<ClassInfo, ArrayList<String>> mClassToInterface
35      = new HashMap<ClassInfo, ArrayList<String>>();
36
37
38  public ClassInfo findClass(String name) {
39    return mAllClasses.get(name);
40  }
41
42  protected void resolveInterfaces() {
43    for (ClassInfo cl : mAllClasses.values()) {
44      ArrayList<String> ifaces = mClassToInterface.get(cl);
45      if (ifaces == null) {
46        continue;
47      }
48      for (String iface : ifaces) {
49        ClassInfo ci = mAllClasses.get(iface);
50        if (ci == null) {
51          // Interface not provided by this codebase. Inject a stub.
52          ci = new ClassInfo(iface);
53        }
54        cl.addInterface(ci);
55      }
56    }
57  }
58
59  /**
60   * Checks to see if this api is consistent with a newer version.
61   */
62  public boolean isConsistent(ApiInfo otherApi) {
63    boolean consistent = true;
64    for (PackageInfo pInfo : mPackages.values()) {
65      if (otherApi.getPackages().containsKey(pInfo.name())) {
66        if (!pInfo.isConsistent(otherApi.getPackages().get(pInfo.name()))) {
67          consistent = false;
68        }
69      } else {
70        Errors.error(Errors.REMOVED_PACKAGE, pInfo.position(), "Removed package " + pInfo.name());
71        consistent = false;
72      }
73    }
74    for (PackageInfo pInfo : otherApi.mPackages.values()) {
75      if (!mPackages.containsKey(pInfo.name())) {
76        Errors.error(Errors.ADDED_PACKAGE, pInfo.position(), "Added package " + pInfo.name());
77        consistent = false;
78      }
79    }
80    return consistent;
81  }
82
83  public HashMap<String, PackageInfo> getPackages() {
84    return mPackages;
85  }
86
87  protected void mapClassToSuper(ClassInfo classInfo, String superclass) {
88    mClassToSuper.put(classInfo, superclass);
89  }
90
91  protected void mapClassToInterface(ClassInfo classInfo, String iface) {
92    if (!mClassToInterface.containsKey(classInfo)) {
93      mClassToInterface.put(classInfo, new ArrayList<String>());
94    }
95    mClassToInterface.get(classInfo).add(iface);
96  }
97
98  protected void addPackage(PackageInfo pInfo) {
99    // track the set of organized packages in the API
100    pInfo.setContainingApi(this);
101    mPackages.put(pInfo.name(), pInfo);
102
103    // accumulate a direct map of all the classes in the API
104    for (ClassInfo cl : pInfo.allClasses().values()) {
105      mAllClasses.put(cl.qualifiedName(), cl);
106    }
107  }
108
109  protected void resolveSuperclasses() {
110    for (ClassInfo cl : mAllClasses.values()) {
111      // java.lang.Object has no superclass
112      if (!cl.qualifiedName().equals("java.lang.Object")) {
113        String scName = mClassToSuper.get(cl);
114        if (scName == null) {
115          scName = "java.lang.Object";
116        }
117        ClassInfo superclass = mAllClasses.get(scName);
118        if (superclass == null) {
119          // Superclass not provided by this codebase. Inject a stub.
120          superclass = new ClassInfo(scName);
121        }
122        cl.setSuperClass(superclass);
123      }
124    }
125  }
126}
127