View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.tool.classdepend;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  /**
25   * Effectively Immutable parameter class for ClassDepend.  
26   * When River transitions to Java 5, this will
27   * allow easy concurrent programming using the new concurrent utils packages.
28   * 
29   * This class cannot be instantiated directly, you must use a CDPBuilder to
30   * return a ClassDependParamters object instance.
31   * 
32   * @author Peter Firmstone
33   * @see ClassDepend
34   * @see CDPBuilder
35   */
36  public class ClassDependParameters {
37      /* outsidePackagesOrClasses excluded from search ,excludes the names of classes,
38       * or package patterns, that should be excluded from the computation */
39  
40      private final String[] outsidePackagesOrClasses;
41      private final String[] insidePackages; // package scope to search for dependencies in.
42      private final String[] showPackages; //Show only the dependencies found in these Packages.
43      private final String[] hidePackages; //Hide these packages from output, the dependencies are still calculated.
44      private final boolean ignoreOuterParentClass; // For internal classes to ignore their parent class.
45      private final boolean excludePlatformClasses;
46      private final boolean edges;
47  
48      private ClassDependParameters(CDPBuilder builder) {
49  
50          outsidePackagesOrClasses = (String[]) builder.outsidePackagesOrClasses.toArray(
51                  new String[builder.outsidePackagesOrClasses.size()]);
52          insidePackages = (String[]) builder.insidePackages.toArray(
53                  new String[builder.insidePackages.size()]);
54          showPackages = (String[]) builder.showPackages.toArray(
55                  new String[builder.showPackages.size()]);
56          hidePackages = (String[]) builder.hidePackages.toArray(
57                  new String[builder.hidePackages.size()]);
58          ignoreOuterParentClass = builder.ignoreOuterParentClass;
59          excludePlatformClasses = builder.excludePlatformClasses;
60          edges = builder.edges;
61  
62      }
63  
64      private List cloneArraytoList(String[] array) {
65          /* We can get away with cloning the Array since Strings are immutable.
66           * the copy, a cloned array, has identical object references to String
67           * objects contained in the original.  The retrieved ArrayList can be modified
68           * without affecting the original array.
69           */
70          String[] ac = (String[]) array.clone();
71          return Arrays.asList(ac);
72  
73      }
74  
75      /**
76       * outsidePackagesOrClasses - excluded from search ,excludes the names 
77       * of classes, or package patterns, that should be excluded from the 
78       * dependency computation 
79       * @see ClassDepend
80       * @return outsidePackagesOrClasses
81       */
82      public List outsidePackagesOrClasses() {
83          return cloneArraytoList(outsidePackagesOrClasses);
84      }
85  
86      public List insidePackages() {
87          return cloneArraytoList(insidePackages);
88      }
89      
90      public List showPackages() {
91          return cloneArraytoList(showPackages);
92      }
93      
94      public List hidePackages() {
95          return cloneArraytoList(hidePackages);
96      }
97      
98      public boolean ignoreOuterParentClass() {
99          return ignoreOuterParentClass;
100     }
101 
102     public boolean excludePlatformClasses() {
103         return excludePlatformClasses;
104     }
105 
106     public boolean edges() {
107         return edges;
108     }
109 
110     /**
111      * CDPBuilder - to build an immutable ClassDependParameters object, much
112      * like the StringBuilder and String class relationship.
113      * 
114      * CDP Builder is not threadsafe.
115      * 
116      * Optional Parameters are set by methods that can be chained on the
117      * Builder object, which has a no argument constructor.  
118      * The <code>build()</code> method returns the new ClassDependParameters
119      * object, the builder can be used to build as many ClassDependParameter
120      * objects as desired.
121      */
122     public static class CDPBuilder {
123         /* Lists are good for building, they're dynamically resizable
124          * this builder is not threadsafe.
125          */
126 
127         private List outsidePackagesOrClasses = new ArrayList();
128         private List insidePackages = new ArrayList();
129         private List showPackages = new ArrayList();
130         private List hidePackages = new ArrayList();
131         private boolean ignoreOuterParentClass = false;
132         private boolean excludePlatformClasses = false;
133         private boolean edges = false;
134 
135         public CDPBuilder() {
136         }
137 
138         /**
139          * The package patterns or class names to be excluded from the dependency
140          * search results.
141          * @param outsidePackageOrClass Package pattern or Class to be excluded from
142          * dependency checking.
143          * A package pattern ending in .* excludes the packages in the package
144          * root directory, to decend recursively into and exclude subpackages, 
145          * the package pattern should end in .**
146          * 
147          * @see ClassDepend
148          * @see ClassDependParameters
149          * @return CDPBuilder so named optional parameters can be chained
150          */
151         public CDPBuilder addOutsidePackageOrClass(String outsidePackageOrClass) {
152             outsidePackagesOrClasses.add(outsidePackageOrClass);
153             return this;
154         }
155 
156         public CDPBuilder addOutsidePackagesOrClasses(String[] outsidePackagesOrClasses) {
157             int l = outsidePackagesOrClasses.length;
158             for (int i = 0; i < l; i++) {
159                 this.outsidePackagesOrClasses.add(outsidePackagesOrClasses[i]);
160             }
161             return this;
162         }
163 
164         public CDPBuilder addOutsidePackagesOrClasses(List excludes) {
165             outsidePackagesOrClasses.addAll(excludes);
166             return this;
167         }
168 
169 
170 
171         /**
172          * Inside packages limit the scope of the dependency search to
173          * Classes within these packages.
174          * @param insidePackage A String pattern including the fully qualified 
175          *                      package name, followed by .* to capture classes
176          *                      in the packages root directory or by .** to
177          *                      include subpackages recursively as well.
178          * @return CDPBuilder - enables optional parameter method chaining.
179          */
180         public CDPBuilder addInsidePackage(String insidePackage) {
181             insidePackages.add(insidePackage);
182             return this;
183         }
184         
185         /**
186          * Inside packages limit the scope of the dependency search to
187          * Classes within these packages.
188          * @param insidePackages
189          * @return CDPBuilder - enables optional parameter method chaining.
190          */
191         public CDPBuilder addInsidePackages(String[] insidePackages) {
192             for (int i = 0, l = insidePackages.length; i < l; i++) {
193                 this.insidePackages.add(insidePackages[i]);
194             }
195             return this;
196         }
197 
198         public CDPBuilder addInsidePackages(List inside) {
199             insidePackages.addAll(inside);
200             return this;
201         }
202         
203         public CDPBuilder addShowPackages(String [] showPackages){
204             for (int i = 0, l = showPackages.length; i < l; i++){
205                 this.showPackages.add(showPackages[i]);
206             }
207             return this;
208         }
209         
210         public CDPBuilder addShowPackages(List showPackages){
211             this.showPackages.addAll(showPackages);
212             return this;
213         }
214         
215         public CDPBuilder addShowPackage(String showPackage){
216             this.showPackages.add(showPackage);
217             return this;
218         }
219         
220         public CDPBuilder addHidePackages(String [] hidePackages){
221             for (int i = 0, l = hidePackages.length; i < l; i++){
222                 this.hidePackages.add(hidePackages[i]);
223             }
224             return this;
225         }
226         
227         public CDPBuilder addHidePackages(List hidePackages){
228             this.hidePackages.addAll(hidePackages);
229             return this;
230         }
231         
232         public CDPBuilder addHidePackage(String hidePackage){
233             this.hidePackages.add(hidePackage);
234             return this;
235         }
236         
237         public CDPBuilder ignoreOuterParentClass(boolean b) {
238             ignoreOuterParentClass = b;
239             return this;
240         }
241 
242         /**
243          * This optional parameter if true, excludes Java platform classes
244          * from the dependency search.
245          * If false the platform classes returned will depend on the Java
246          * platform and version the test is executing on.
247          * 
248          * @see ClassDepend
249          * @see ClassDependParameters
250          * @param b
251          * @return CDPBuilder - enables optional parameter method chaining.
252          */
253         public CDPBuilder excludePlatformClasses(boolean b) {
254             excludePlatformClasses = b;
255             return this;
256         }
257 
258         public CDPBuilder edges(boolean e){
259             edges = e;
260             return this;
261         }
262 
263         /**
264          * Builds ClassDependParameters immutable object from optional
265          * parameters, execute this method last, after setting all optional
266          * parameters.
267          * @see ClassDependParameters
268          * @return ClassDependParameter object
269          */
270         public ClassDependParameters build() {
271             return new ClassDependParameters(this);
272         }
273     }
274 }