blob: 75d24a17a855f31454531aea2a2234f4e46364b1 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
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 */
Thomas Vachuska6b331262015-04-27 11:09:07 -070016package org.onlab.jdvue;
17
18import java.util.*;
19
Sho SHIMIZU43748e82015-08-25 10:05:08 -070020import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuska6b331262015-04-27 11:09:07 -070021
22/**
23 * Simple abstraction of a Java source file for the purpose of tracking
24 * dependencies and requirements.
25 *
26 * @author Thomas Vachuska
27 */
28public class JavaSource extends JavaEntity {
29
30 private String path;
31 private JavaPackage javaPackage;
32
33 private final Set<String> importNames = new HashSet<>();
34 private Set<JavaEntity> imports;
35
36 /**
37 * Creates a new Java source entity.
38 *
39 * @param name java source file name
Thomas Vachuska266b4432015-04-30 18:13:25 -070040 * @param path source file path
Thomas Vachuska6b331262015-04-27 11:09:07 -070041 */
42 JavaSource(String name, String path) {
43 super(name);
44 this.path = path;
45 }
46
47 /**
48 * Returns the Java package for this Java source.
49 *
50 * @return Java package
51 */
52 public JavaPackage getPackage() {
53 return javaPackage;
54 }
55
56 /**
57 * Sets the Java package for this Java source.
58 *
59 * @param javaPackage Java package
60 */
61 void setPackage(JavaPackage javaPackage) {
62 if (this.javaPackage == null) {
63 this.javaPackage = javaPackage;
64 }
65 }
66
67 /**
68 * Returns the set of resolved imports for this Java source
Thomas Vachuska266b4432015-04-30 18:13:25 -070069 *
Thomas Vachuska6b331262015-04-27 11:09:07 -070070 * @return set of imports
71 */
72 public Set<JavaEntity> getImports() {
73 return imports;
74 }
75
76 /**
77 * Sets the set of resolved imported Java entities for this source.
78 *
79 * @param imports set of resolved Java entities imported by this source
80 */
81 void setImports(Set<JavaEntity> imports) {
82 if (this.imports == null) {
83 this.imports = Collections.unmodifiableSet(new HashSet<>(imports));
84 }
85 }
86
87 /**
88 * Adds a name of an imported, but unresolved, Java entity name.
89 *
90 * @param name name of an imported Java entity
91 */
92 void addImportName(String name) {
93 importNames.add(name);
94 }
95
96 /**
97 * Returns the set of imported, but unresolved, Java entity names.
Thomas Vachuska266b4432015-04-30 18:13:25 -070098 *
Thomas Vachuska6b331262015-04-27 11:09:07 -070099 * @return set of imported Java entity names
100 */
101 Set<String> getImportNames() {
102 return importNames;
103 }
104
105 @Override
106 public String toString() {
107 return toStringHelper(this)
108 .add("name", name())
109 .add("javaPackage", (javaPackage != null ? javaPackage.name() : ""))
110 .add("importNames", importNames.size())
111 .add("imports", (imports != null ? imports.size() : 0))
112 .toString();
113 }
114
115}