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 vogar.target;
18
19import java.io.IOException;
20import java.util.Collections;
21import java.util.HashSet;
22import java.util.Set;
23import java.util.TreeSet;
24
25/**
26 * The class and subpackage contents of a package.
27 *
28 * <p>Adapted from android.test.ClassPathPackageInfo.
29 */
30class Package {
31
32    private final ClassPathScanner source;
33    private final Set<String> subpackageNames;
34    private final Set<Class<?>> topLevelClasses;
35
36    Package(ClassPathScanner source,
37            Set<String> subpackageNames, Set<Class<?>> topLevelClasses) {
38        this.source = source;
39        this.subpackageNames = Collections.unmodifiableSet(subpackageNames);
40        this.topLevelClasses = Collections.unmodifiableSet(topLevelClasses);
41    }
42
43    public Set<Class<?>> getTopLevelClassesRecursive() throws IOException {
44        Set<Class<?>> set = new TreeSet<Class<?>>(ClassPathScanner.ORDER_CLASS_BY_NAME);
45        addTopLevelClassesTo(set);
46        return set;
47    }
48
49    private Set<Package> getSubpackages() throws IOException {
50        Set<Package> info = new HashSet<Package>();
51        for (String name : subpackageNames) {
52            info.add(source.scan(name));
53        }
54        return info;
55    }
56
57    private void addTopLevelClassesTo(Set<Class<?>> set) throws IOException {
58        set.addAll(topLevelClasses);
59        for (Package info : getSubpackages()) {
60            info.addTopLevelClassesTo(set);
61        }
62    }
63}
64