Skip to content

Commit 1f3d98f

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 1f3d98f

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,31 @@ 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)
20+
&& Equals(subTypeName, other.subTypeName)
21+
&& Equals(baseLabel, other.baseLabel)
22+
&& Equals(subLabel, other.subLabel);
23+
}
24+
25+
public override bool Equals(object obj)
26+
{
27+
if (ReferenceEquals(null, obj)) return false;
28+
if (ReferenceEquals(this, obj)) return true;
29+
if (obj.GetType() != this.GetType()) return false;
30+
return Equals((Relationship)obj);
31+
}
32+
33+
public override int GetHashCode()
34+
{
35+
unchecked
36+
{
37+
var hashCode = (baseTypeName != null ? baseTypeName.GetHashCode() : 0);
38+
hashCode = (hashCode * 397) ^ (subTypeName != null ? subTypeName.GetHashCode() : 0);
39+
hashCode = (hashCode * 397) ^ (baseLabel != null ? baseLabel.GetHashCode() : 0);
40+
return hashCode;
41+
}
42+
}
1643
}

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)