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.Collections; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Set; |
| 21 | |
Sho SHIMIZU | 43748e8 | 2015-08-25 10:05:08 -0700 | [diff] [blame] | 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | 6b33126 | 2015-04-27 11:09:07 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Simple abstraction of a Java package for the purpose of tracking |
| 26 | * dependencies and requirements. |
| 27 | * |
| 28 | * @author Thomas Vachuska |
| 29 | */ |
| 30 | public 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 | } |