blob: 6902d22b49c9105e19846eb52895919b30866fee [file] [log] [blame]
Thomas Vachuska6b331262015-04-27 11:09:07 -07001package org.onlab.jdvue;
2
3import java.util.*;
4
5import static com.google.common.base.Objects.toStringHelper;
6
7/**
8 * Simple abstraction of a Java source file for the purpose of tracking
9 * dependencies and requirements.
10 *
11 * @author Thomas Vachuska
12 */
13public class JavaSource extends JavaEntity {
14
15 private String path;
16 private JavaPackage javaPackage;
17
18 private final Set<String> importNames = new HashSet<>();
19 private Set<JavaEntity> imports;
20
21 /**
22 * Creates a new Java source entity.
23 *
24 * @param name java source file name
Thomas Vachuska266b4432015-04-30 18:13:25 -070025 * @param path source file path
Thomas Vachuska6b331262015-04-27 11:09:07 -070026 */
27 JavaSource(String name, String path) {
28 super(name);
29 this.path = path;
30 }
31
32 /**
33 * Returns the Java package for this Java source.
34 *
35 * @return Java package
36 */
37 public JavaPackage getPackage() {
38 return javaPackage;
39 }
40
41 /**
42 * Sets the Java package for this Java source.
43 *
44 * @param javaPackage Java package
45 */
46 void setPackage(JavaPackage javaPackage) {
47 if (this.javaPackage == null) {
48 this.javaPackage = javaPackage;
49 }
50 }
51
52 /**
53 * Returns the set of resolved imports for this Java source
Thomas Vachuska266b4432015-04-30 18:13:25 -070054 *
Thomas Vachuska6b331262015-04-27 11:09:07 -070055 * @return set of imports
56 */
57 public Set<JavaEntity> getImports() {
58 return imports;
59 }
60
61 /**
62 * Sets the set of resolved imported Java entities for this source.
63 *
64 * @param imports set of resolved Java entities imported by this source
65 */
66 void setImports(Set<JavaEntity> imports) {
67 if (this.imports == null) {
68 this.imports = Collections.unmodifiableSet(new HashSet<>(imports));
69 }
70 }
71
72 /**
73 * Adds a name of an imported, but unresolved, Java entity name.
74 *
75 * @param name name of an imported Java entity
76 */
77 void addImportName(String name) {
78 importNames.add(name);
79 }
80
81 /**
82 * Returns the set of imported, but unresolved, Java entity names.
Thomas Vachuska266b4432015-04-30 18:13:25 -070083 *
Thomas Vachuska6b331262015-04-27 11:09:07 -070084 * @return set of imported Java entity names
85 */
86 Set<String> getImportNames() {
87 return importNames;
88 }
89
90 @Override
91 public String toString() {
92 return toStringHelper(this)
93 .add("name", name())
94 .add("javaPackage", (javaPackage != null ? javaPackage.name() : ""))
95 .add("importNames", importNames.size())
96 .add("imports", (imports != null ? imports.size() : 0))
97 .toString();
98 }
99
100}