| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | namespace DirectSight.Parser.Analysis; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents the metrics of a method. |
| | | 9 | | /// </summary> |
| | | 10 | | public class MethodMetric |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// List of metrics. |
| | | 14 | | /// </summary> |
| | 2996 | 15 | | private readonly List<Metric> metrics = []; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="MethodMetric"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="fullName">The full method name.</param> |
| | | 21 | | /// <param name="shortName">The short method name.</param> |
| | | 22 | | /// <param name="metrics">The metrics.</param> |
| | 2996 | 23 | | public MethodMetric(string fullName, string shortName, IEnumerable<Metric> metrics) |
| | 2996 | 24 | | { |
| | 2996 | 25 | | this.FullName = fullName; |
| | 2996 | 26 | | this.ShortName = shortName; |
| | 2996 | 27 | | this.AddMetrics(metrics); |
| | 2996 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the list of metrics. |
| | | 32 | | /// </summary> |
| | 615 | 33 | | public IEnumerable<Metric> Metrics => this.metrics; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the full name of the method. |
| | | 37 | | /// </summary> |
| | 6410 | 38 | | public string FullName { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the name of the method. |
| | | 42 | | /// </summary> |
| | 73 | 43 | | public string ShortName { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the line number. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <value> |
| | | 49 | | /// The line number. |
| | | 50 | | /// </value> |
| | 7700 | 51 | | public int? Line { get; internal set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Returns a <see cref="string" /> that represents this instance. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <returns> |
| | | 57 | | /// A <see cref="string" /> that represents this instance. |
| | | 58 | | /// </returns> |
| | | 59 | | public override string ToString() |
| | 0 | 60 | | { |
| | 0 | 61 | | return this.ShortName; |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Determines whether the specified <see cref="object"/> is equal to this instance. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="obj">The <see cref="object"/> to compare with this instance.</param> |
| | | 68 | | /// <returns> |
| | | 69 | | /// <c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>. |
| | | 70 | | /// </returns> |
| | | 71 | | public override bool Equals(object obj) |
| | 1674 | 72 | | { |
| | 1674 | 73 | | if (obj == null || !obj.GetType().Equals(typeof(MethodMetric))) |
| | 2 | 74 | | { |
| | 2 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | else |
| | 1672 | 78 | | { |
| | 1672 | 79 | | var methodMetric = (MethodMetric)obj; |
| | 1672 | 80 | | return methodMetric.FullName.Equals(this.FullName) && methodMetric.Line == this.Line; |
| | | 81 | | } |
| | 1674 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Returns a hash code for this instance. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <returns> |
| | | 88 | | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
| | | 89 | | /// </returns> |
| | 2988 | 90 | | public override int GetHashCode() => this.FullName.GetHashCode() + this.Line.GetHashCode(); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Adds the given metric. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="metric">The metric.</param> |
| | | 96 | | internal void AddMetric(Metric metric) |
| | 2 | 97 | | { |
| | 2 | 98 | | this.metrics.Add(metric); |
| | 2 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Adds the given metrics. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="metrics">The metrics to add.</param> |
| | | 105 | | internal void AddMetrics(IEnumerable<Metric> metrics) |
| | 2996 | 106 | | { |
| | 2996 | 107 | | this.metrics.AddRange(metrics); |
| | 2996 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Merges the given method metric with the current instance. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="methodMetric">The method metric to merge.</param> |
| | | 114 | | internal void Merge(MethodMetric methodMetric) |
| | 752 | 115 | | { |
| | 752 | 116 | | if (methodMetric == null) |
| | 0 | 117 | | { |
| | 0 | 118 | | throw new ArgumentNullException(nameof(methodMetric)); |
| | | 119 | | } |
| | | 120 | | |
| | 9574 | 121 | | foreach (var metric in methodMetric.metrics) |
| | 3659 | 122 | | { |
| | 14485 | 123 | | var existingMetric = this.metrics.FirstOrDefault(m => m.Name == metric.Name); |
| | 3659 | 124 | | if (existingMetric != null) |
| | 3658 | 125 | | { |
| | 3658 | 126 | | if (existingMetric.Value.HasValue) |
| | 3656 | 127 | | { |
| | 3656 | 128 | | if (metric.Value.HasValue) |
| | 3655 | 129 | | { |
| | 3655 | 130 | | if (metric.MergeOrder == MetricMergeOrder.HigherIsBetter) |
| | 1503 | 131 | | { |
| | 1503 | 132 | | existingMetric.Value = Math.Max(existingMetric.Value.Value, metric.Value.Value); |
| | 1503 | 133 | | } |
| | | 134 | | else |
| | 2152 | 135 | | { |
| | 2152 | 136 | | existingMetric.Value = Math.Min(existingMetric.Value.Value, metric.Value.Value); |
| | 2152 | 137 | | } |
| | 3655 | 138 | | } |
| | 3656 | 139 | | } |
| | | 140 | | else |
| | 2 | 141 | | { |
| | 2 | 142 | | existingMetric.Value = metric.Value; |
| | 2 | 143 | | } |
| | 3658 | 144 | | } |
| | | 145 | | else |
| | 1 | 146 | | { |
| | 1 | 147 | | this.AddMetric(metric); |
| | 1 | 148 | | } |
| | 3659 | 149 | | } |
| | 752 | 150 | | } |
| | | 151 | | } |