1/*
2 * Copyright (C) 2008 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 android.test;
18
19import com.google.android.collect.Sets;
20
21import java.util.Collections;
22import java.util.Set;
23
24/**
25 * The Package object doesn't allow you to iterate over the contained
26 * classes and subpackages of that package.  This is a version that does.
27 *
28 * {@hide} Not needed for 1.0 SDK.
29 */
30public class ClassPathPackageInfo {
31
32    private final ClassPathPackageInfoSource source;
33    private final String packageName;
34    private final Set<String> subpackageNames;
35    private final Set<Class<?>> topLevelClasses;
36
37    ClassPathPackageInfo(ClassPathPackageInfoSource source, String packageName,
38            Set<String> subpackageNames, Set<Class<?>> topLevelClasses) {
39        this.source = source;
40        this.packageName = packageName;
41        this.subpackageNames = Collections.unmodifiableSet(subpackageNames);
42        this.topLevelClasses = Collections.unmodifiableSet(topLevelClasses);
43    }
44
45    public Set<ClassPathPackageInfo> getSubpackages() {
46        Set<ClassPathPackageInfo> info = Sets.newHashSet();
47        for (String name : subpackageNames) {
48            info.add(source.getPackageInfo(name));
49        }
50        return info;
51    }
52
53    public Set<Class<?>> getTopLevelClassesRecursive() {
54        Set<Class<?>> set = Sets.newHashSet();
55        addTopLevelClassesTo(set);
56        return set;
57    }
58
59    private void addTopLevelClassesTo(Set<Class<?>> set) {
60        set.addAll(topLevelClasses);
61        for (ClassPathPackageInfo info : getSubpackages()) {
62            info.addTopLevelClassesTo(set);
63        }
64    }
65
66    @Override
67    public boolean equals(Object obj) {
68        if (obj instanceof ClassPathPackageInfo) {
69            ClassPathPackageInfo that = (ClassPathPackageInfo) obj;
70            return (this.packageName).equals(that.packageName);
71        }
72        return false;
73    }
74
75    @Override
76    public int hashCode() {
77        return packageName.hashCode();
78    }
79}
80