blob: a15cb4d33a6a0c594e376a7f9b2fee07ecf7aeda [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 Vachuska6b331262015-04-27 11:09:07 -070016package org.onlab.jdvue;
17
18import java.util.Collections;
19import java.util.HashSet;
20import java.util.Set;
21
Sho SHIMIZU43748e82015-08-25 10:05:08 -070022import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuska6b331262015-04-27 11:09:07 -070023
24/**
25 * Simple abstraction of a Java package for the purpose of tracking
26 * dependencies and requirements.
27 *
28 * @author Thomas Vachuska
29 */
30public class JavaPackage extends JavaEntity {
31
32 private final Set<JavaSource> sources = new HashSet<>();
33 private Set<JavaPackage> dependencies;
34
35 /**
36 * Creates a new Java package.
37 *
38 * @param name java package file name
39 */
40 JavaPackage(String name) {
41 super(name);
42 }
43
44 /**
45 * Returns the set of sources contained in this Java package.
46 *
47 * @return set of Java sources
48 */
49 public Set<JavaSource> getSources() {
50 return Collections.unmodifiableSet(sources);
51 }
52
53 /**
54 * Adds the specified Java source to the package. Only possible if the
55 * Java package of the source is the same as this Java package.
56 *
57 * @param source Java source to be added
58 */
59 void addSource(JavaSource source) {
60 if (source.getPackage().equals(this)) {
61 sources.add(source);
62 }
63 }
64
65 /**
66 * Returns the set of packages directly required by this package.
67 *
68 * @return set of Java package dependencies
69 */
70 Set<JavaPackage> getDependencies() {
71 return dependencies;
72 }
73
74 /**
75 * Sets the set of resolved Java packages on which this package dependens.
76 *
77 * @param dependencies set of resolved Java packages
78 */
79 void setDependencies(Set<JavaPackage> dependencies) {
80 if (this.dependencies == null) {
81 this.dependencies = Collections.unmodifiableSet(new HashSet<>(dependencies));
82 }
83 }
84
85 @Override
86 public String toString() {
87 return toStringHelper(this)
88 .add("name", name())
89 .add("sources", sources.size())
90 .add("dependencies", (dependencies != null ? dependencies.size() : 0))
91 .toString();
92 }
93
94}