Skip to content

Commit 195037f

Browse files
author
akravtsova
committed
if there is dependency from the same class
in fields, property, in difficult field and so on - distinct + equals and unique relations between classes in RelationshipCollection
1 parent d97e179 commit 195037f

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator/RelationshipGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public static string[] GenerateRelationships(RelationshipCollection relationship
1919
List<string> strings = new List<string>();
2020
strings.AddRange(relationshipCollection.Select(r => r.ToString()));
2121

22-
return strings.ToArray();
22+
return strings.Distinct().ToArray();
2323
}
2424
}

src/PlantUmlClassDiagramGenerator.Library/Relationship.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,25 @@ public override string ToString()
1313
{
1414
return $"{baseTypeName.Identifier}{baseLabel} {symbol}{subLabel} {subTypeName.Identifier}{centerLabel}";
1515
}
16+
17+
private bool Equals(Relationship other)
18+
{
19+
return Equals(baseTypeName, other.baseTypeName) && Equals(subTypeName, other.subTypeName);
20+
}
21+
22+
public override bool Equals(object obj)
23+
{
24+
if (ReferenceEquals(null, obj)) return false;
25+
if (ReferenceEquals(this, obj)) return true;
26+
if (obj.GetType() != this.GetType()) return false;
27+
return Equals((Relationship)obj);
28+
}
29+
30+
public override int GetHashCode()
31+
{
32+
unchecked
33+
{
34+
return ((baseTypeName != null ? baseTypeName.GetHashCode() : 0) * 397) ^ (subTypeName != null ? subTypeName.GetHashCode() : 0);
35+
}
36+
}
1637
}

src/PlantUmlClassDiagramGenerator.Library/RelationshipCollection.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,16 @@ private static TypeNameText GetLeafName(string attributeName, TypeSyntax typeSyn
142142
private void AddeRationship(PlantUmlAssociationAttribute attribute, TypeNameText leafName, TypeNameText rootName)
143143
{
144144
var symbol = string.IsNullOrEmpty(attribute.Association) ? "--" : attribute.Association;
145-
items.Add(new Relationship(rootName, leafName, symbol, attribute.RootLabel, attribute.LeafLabel, attribute.Label));
145+
var relationship = new Relationship(rootName, leafName, symbol, attribute.RootLabel, attribute.LeafLabel, attribute.Label);
146+
if (!items.Contains(relationship))
147+
items.Add(relationship);
146148
}
147149

148150
private void AddRelationship(TypeNameText leafName, TypeNameText rootName, string symbol, string nodeIdentifier)
149151
{
150-
items.Add(new Relationship(rootName, leafName, symbol, "", nodeIdentifier + leafName.TypeArguments));
152+
var relationship = new Relationship(rootName, leafName, symbol, "", nodeIdentifier + leafName.TypeArguments);
153+
if (!items.Contains(relationship))
154+
items.Add(relationship);
151155
}
152156

153157
public IEnumerator<Relationship> GetEnumerator()

src/PlantUmlClassDiagramGenerator.Library/TypeNameText.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,25 @@ public static TypeNameText From(BaseTypeDeclarationSyntax syntax)
7272
TypeArguments = typeArgs
7373
};
7474
}
75+
76+
private bool Equals(TypeNameText other)
77+
{
78+
return Identifier == other.Identifier && TypeArguments == other.TypeArguments;
79+
}
80+
81+
public override bool Equals(object obj)
82+
{
83+
if (ReferenceEquals(null, obj)) return false;
84+
if (ReferenceEquals(this, obj)) return true;
85+
if (obj.GetType() != this.GetType()) return false;
86+
return Equals((TypeNameText)obj);
87+
}
88+
89+
public override int GetHashCode()
90+
{
91+
unchecked
92+
{
93+
return ((Identifier != null ? Identifier.GetHashCode() : 0) * 397) ^ (TypeArguments != null ? TypeArguments.GetHashCode() : 0);
94+
}
95+
}
7596
}

0 commit comments

Comments
 (0)