001/* 002 * Copyright 2010-2021 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.HashMap; 019import java.util.HashSet; 020import java.util.Map; 021import java.util.Set; 022 023/** 024 * A `Clazz` represents the single class identifier inside a classpath. 025 * There is only one `Clazz` per classname. It has incoming and outgoing 026 * edges defining references and dependencies. If there are different 027 * versions found, it collects their sources as ClazzpathUnits. 028 */ 029public final class Clazz implements Comparable<Clazz> { 030 031 private final Set<Clazz> dependencies = new HashSet<>(); 032 private final Set<Clazz> references = new HashSet<>(); 033 private final Map<ClazzpathUnit, String> units = new HashMap<>(); 034 035 private final String name; 036 037 public Clazz( final String pName ) { 038 name = pName; 039 } 040 041 public String getName() { 042 return name; 043 } 044 045 public void addClazzpathUnit( final ClazzpathUnit pUnit, final String pDigest ) { 046 units.put(pUnit, pDigest); 047 } 048 049 public void removeClazzpathUnit( final ClazzpathUnit pUnit ) { 050 units.remove(pUnit); 051 } 052 053 public Set<ClazzpathUnit> getClazzpathUnits() { 054 return units.keySet(); 055 } 056 057 public Set<String> getVersions() { 058 // System.out.println("clazz:" + name + " units:" + units); 059 return new HashSet<>(units.values()); 060 } 061 062 063 public void addDependency( final Clazz pClazz ) { 064 pClazz.references.add(this); 065 dependencies.add(pClazz); 066 } 067 068 public void removeDependency( final Clazz pClazz ) { 069 pClazz.references.remove(this); 070 dependencies.remove(pClazz); 071 } 072 073 public Set<Clazz> getDependencies() { 074 return dependencies; 075 } 076 077 078 079 public Set<Clazz> getReferences() { 080 return references; 081 } 082 083 084 public Set<Clazz> getTransitiveDependencies() { 085 final Set<Clazz> all = new HashSet<>(); 086 findTransitiveDependencies(all); 087 return all; 088 } 089 090 091 void findTransitiveDependencies( final Set<? super Clazz> pAll ) { 092 093 for (Clazz clazz : dependencies) { 094 if (!pAll.contains(clazz)) { 095 pAll.add(clazz); 096 clazz.findTransitiveDependencies(pAll); 097 } 098 } 099 } 100 101 102 public boolean equals( final Object pO ) { 103 if (pO.getClass() != Clazz.class) { 104 return false; 105 } 106 final Clazz c = (Clazz) pO; 107 return name.equals(c.name); 108 } 109 110 public int hashCode() { 111 return name.hashCode(); 112 } 113 114 public int compareTo( final Clazz pO ) { 115 return name.compareTo(((Clazz) pO).name); 116 } 117 118 public String toString() { 119 return name; 120 } 121 122}