Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | 6b33126 | 2015-04-27 11:09:07 -0700 | [diff] [blame] | 16 | package org.onlab.jdvue; |
| 17 | |
| 18 | import java.util.*; |
| 19 | |
Sho SHIMIZU | 43748e8 | 2015-08-25 10:05:08 -0700 | [diff] [blame] | 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | 6b33126 | 2015-04-27 11:09:07 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Simple abstraction of a Java source file for the purpose of tracking |
| 24 | * dependencies and requirements. |
| 25 | * |
| 26 | * @author Thomas Vachuska |
| 27 | */ |
| 28 | public 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 Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 40 | * @param path source file path |
Thomas Vachuska | 6b33126 | 2015-04-27 11:09:07 -0700 | [diff] [blame] | 41 | */ |
| 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 Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 69 | * |
Thomas Vachuska | 6b33126 | 2015-04-27 11:09:07 -0700 | [diff] [blame] | 70 | * @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 Vachuska | 266b443 | 2015-04-30 18:13:25 -0700 | [diff] [blame] | 98 | * |
Thomas Vachuska | 6b33126 | 2015-04-27 11:09:07 -0700 | [diff] [blame] | 99 | * @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 | } |