001/*
002 * Copyright 2010-2018 The jdependency developers.
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 */
016package org.vafer.jdependency;
017
018import java.util.HashSet;
019import java.util.Set;
020
021public final class Clazz implements Comparable<Clazz> {
022
023    private final Set<Clazz> dependencies = new HashSet<Clazz>();
024    private final Set<Clazz> references = new HashSet<Clazz>();
025    private final Set<ClazzpathUnit> units = new HashSet<ClazzpathUnit>();
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<ClazzpathUnit> 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<Clazz> getDependencies() {
062        return dependencies;
063    }
064
065
066
067    public Set<Clazz> getReferences() {
068        return references;
069    }
070
071
072    public Set<Clazz> getTransitiveDependencies() {
073        final Set<Clazz> all = new HashSet<Clazz>();
074        findTransitiveDependencies(all);
075        return all;
076    }
077
078    void findTransitiveDependencies( final Set<? super Clazz> 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 Clazz pO ) {
101        return name.compareTo(((Clazz) pO).name);
102    }
103
104    public String toString() {
105        return name;
106    }
107
108}