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.Collections;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.Set;
24  
25  /**
26   * A container to store class dependency related information for later analysis.
27   * @author Peter Firmstone
28   * @see ClassDepend
29   */
30  public class ClassDependencyRelationship {
31  
32      private final Set dependants;   // classes that depend upon this class.
33      private final Set providers;    // classes that this class depends upon.
34      private final String fullyQualifiedClassName;
35      private final int hash;
36      private final boolean rootClass;
37      
38      ClassDependencyRelationship (String fullyQualifiedClassName, boolean rootClass){
39          this.fullyQualifiedClassName = fullyQualifiedClassName;
40          hash = 59 * 7 + (this.fullyQualifiedClassName != null ? this.fullyQualifiedClassName.hashCode() : 0);
41          dependants = new HashSet();
42          providers = new HashSet();
43          this.rootClass = rootClass;
44      }
45      
46      ClassDependencyRelationship (String fullyQualifiedClassName){
47          this(fullyQualifiedClassName, false);    
48      }
49      
50      // This is private since we tend to recurse the dependency tree from the
51      // dependant end to the provider.
52      private void addDependant(ClassDependencyRelationship dependant) {
53          synchronized (dependants) {
54              dependants.add(dependant);
55          }
56      }
57  
58      /**
59       * Add a provider class to this dependant class.
60       * @param provider
61       */
62      public void addProvider(ClassDependencyRelationship provider) {
63          synchronized (providers){
64              providers.add(provider);
65          }
66          provider.addDependant(this);
67      }
68  
69      /**
70       * Get the classes dependant on this class.
71       * @return classes dependant on this
72       */
73      public Set getDependants() {
74          Set deps = new HashSet();
75          //defensive copy
76          synchronized (dependants){
77              deps.addAll(dependants);
78          }
79          return deps;
80      }
81  
82      /**
83       * Get the classes that this class needs to function.
84       * @return a Set of classes
85       */
86      public Set getProviders() {
87          Set prov = new HashSet();
88          //defensive copy
89          synchronized (providers){
90              prov.addAll(providers);
91          }
92          return prov;
93      }
94      
95      public String toString(){
96          return fullyQualifiedClassName;
97      }
98  
99      @Override
100     public int hashCode() {
101         return hash;
102     }
103     
104     public boolean equals(Object o){
105         if ( o == null ) return false;
106         if (!(o instanceof ClassDependencyRelationship)) return false;
107         if (fullyQualifiedClassName.equals(
108                 ((ClassDependencyRelationship)o).fullyQualifiedClassName))
109                 return true;
110         return false;
111     }
112 
113     /**
114      * If this a root dependant, the class was used to discover dependencies.
115      * @return true or false
116      */
117     public boolean isRootClass() {
118         return rootClass;
119     }
120 }