001    /*
002     * Copyright 2010 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.vafer.jdependency;
017    
018    import java.util.HashSet;
019    import java.util.Set;
020    
021    public final class Clazz implements Comparable {
022    
023        private final Set<Clazz> dependencies = new HashSet();
024        private final Set<Clazz> references = new HashSet();
025        private final Set<ClazzpathUnit> units = new HashSet();
026    
027        private final String name;  
028    
029        public Clazz( final String pName ) {
030            name = pName;
031        }
032    
033        public String getName() {
034            return name;
035        }
036        
037        
038        public void addClazzpathUnit( final ClazzpathUnit pUnit ) {
039            units.add(pUnit);
040        }
041    
042        public void removeClazzpathUnit( final ClazzpathUnit pUnit ) {
043            units.remove(pUnit);
044        }
045        
046        public Set getClazzpathUnits() {
047            return units;
048        }
049    
050    
051        public void addDependency( final Clazz pClazz ) {
052            pClazz.references.add(this);
053            dependencies.add(pClazz);
054        }
055    
056        public void removeDependency( final Clazz pClazz ) {
057            pClazz.references.remove(this);
058            dependencies.remove(pClazz);        
059        }
060        
061        public Set getDependencies() {
062            return dependencies;
063        }
064    
065    
066        
067        public Set getReferences() {
068            return references;
069        }
070    
071        
072        public Set getTransitiveDependencies() {
073            final Set all = new HashSet();
074            findTransitiveDependencies(all);
075            return all;
076        }
077    
078        void findTransitiveDependencies( final Set pAll ) {
079    
080            for (Clazz clazz : dependencies) {
081                if (!pAll.contains(clazz)) {
082                    pAll.add(clazz);
083                    clazz.findTransitiveDependencies(pAll);
084                }
085            }
086        }
087    
088        public boolean equals( final Object pO ) {
089            if (pO.getClass() != Clazz.class) {
090                return false;
091            }
092            final Clazz c = (Clazz) pO;
093            return name.equals(c.name);
094        }
095    
096        public int hashCode() {
097            return name.hashCode();
098        }
099    
100        public int compareTo( final Object pO ) {
101            return name.compareTo(((Clazz) pO).name);
102        }
103    
104        public String toString() {
105            return name;
106        }
107    
108    }