| | | 1 | | using System.Text.Json.Serialization; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Workflows.Activities.Flowchart.Models; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A connection between a source and a target endpoint. |
| | | 7 | | /// </summary> |
| | | 8 | | public class Connection : IEquatable<Connection> |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Initializes a new instance of the <see cref="Connection"/> class. |
| | | 12 | | /// </summary> |
| | | 13 | | [JsonConstructor] |
| | 164 | 14 | | public Connection() |
| | | 15 | | { |
| | 164 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="Connection"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="source">The source endpoint.</param> |
| | | 22 | | /// <param name="target">The target endpoint.</param> |
| | 572 | 23 | | public Connection(Endpoint source, Endpoint target) |
| | | 24 | | { |
| | 572 | 25 | | Source = source; |
| | 572 | 26 | | Target = target; |
| | 572 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="Connection"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="source">The source endpoint.</param> |
| | | 33 | | /// <param name="target">The target endpoint.</param> |
| | 128 | 34 | | public Connection(IActivity source, IActivity target) |
| | | 35 | | { |
| | 128 | 36 | | Source = new(source); |
| | 128 | 37 | | Target = new(target); |
| | 128 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The source endpoint. |
| | | 42 | | /// </summary> |
| | 16788 | 43 | | public Endpoint Source { get; set; } = null!; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The target endpoint. |
| | | 47 | | /// </summary> |
| | 13554 | 48 | | public Endpoint Target { get; set; } = null!; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// A collection of points representing the vertices of the connection. |
| | | 52 | | /// </summary> |
| | 1495 | 53 | | public ICollection<Position> Vertices { get; set; } = []; |
| | | 54 | | |
| | | 55 | | public override string ToString() => |
| | 715 | 56 | | $"{Source.Activity.Id}{(string.IsNullOrEmpty(Source.Port) ? "" : $":{Source.Port}")}->" + |
| | 715 | 57 | | $"{Target.Activity.Id}{(string.IsNullOrEmpty(Target.Port) ? "" : $":{Target.Port}")}"; |
| | | 58 | | |
| | | 59 | | // Implement equality logic |
| | | 60 | | public bool Equals(Connection? other) |
| | | 61 | | { |
| | 143 | 62 | | if (other == null) return false; |
| | 143 | 63 | | return AreEndpointsEqual(Source, other.Source) && AreEndpointsEqual(Target, other.Target); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public override bool Equals(object? obj) |
| | | 67 | | { |
| | 0 | 68 | | return obj is Connection other && Equals(other); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public override int GetHashCode() |
| | | 72 | | { |
| | 2110 | 73 | | return HashCode.Combine(GetEndpointHashCode(Source), GetEndpointHashCode(Target)); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | private static bool AreEndpointsEqual(Endpoint e1, Endpoint e2) |
| | | 77 | | { |
| | 270 | 78 | | return e1.Activity.Equals(e2.Activity) && e1.Port == e2.Port; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static int GetEndpointHashCode(Endpoint endpoint) |
| | | 82 | | { |
| | 4220 | 83 | | return HashCode.Combine(endpoint.Activity.GetHashCode(), endpoint.Port?.GetHashCode() ?? 0); |
| | | 84 | | } |
| | | 85 | | } |