View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.basepom.mojo.duplicatefinder.classpath;
15  
16  import java.util.List;
17  
18  import com.google.common.base.Joiner;
19  import com.google.common.collect.ImmutableList;
20  
21  import static com.google.common.base.Preconditions.checkArgument;
22  import static com.google.common.base.Preconditions.checkNotNull;
23  
24  /**
25   * Manages the package in which resources and classes reside. Keeps track of the current package when
26   * traversing local folders for classes and resources.
27   */
28  class PackageNameHolder {
29  
30      private final ImmutableList<String> packages;
31      private final String packageName;
32      private final String path;
33  
34      PackageNameHolder(final List<String> packages) {
35          this.packages = ImmutableList.copyOf(checkNotNull(packages, "packages is null"));
36          this.packageName = Joiner.on('.').join(packages);
37          this.path = Joiner.on('/').join(packages);
38      }
39  
40      PackageNameHolder() {
41          this.packages = ImmutableList.of();
42          this.packageName = "";
43          this.path = "";
44      }
45  
46      PackageNameHolder getChildPackage(final String packageName) {
47          checkNotNull(packageName, "packageName is null");
48          checkArgument(packageName.length() > 0, "package name must have at least one character");
49  
50          return new PackageNameHolder(ImmutableList.<String>builder().addAll(packages).add(packageName).build());
51      }
52  
53      String getQualifiedName(final String className) {
54          checkNotNull(className, "className is null");
55          return packages.isEmpty() ? className : packageName + "." + className;
56      }
57  
58      String getQualifiedPath(final String className) {
59          checkNotNull(className, "className is null");
60          return packages.isEmpty() ? className : path + "/" + className;
61      }
62  
63      @Override
64      public String toString() {
65          return getClass().getSimpleName() + "(" + packageName + ")";
66      }
67  }