|
| 1 | +using KellermanSoftware.CompareNetObjects.IgnoreOrderTypes; |
| 2 | +using System; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.ObjectModel; |
| 6 | +using System.Globalization; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | + |
| 10 | +namespace KellermanSoftware.CompareNetObjects.TypeComparers |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Logic to compare two ReadOnlyCollections. |
| 14 | + /// </summary> |
| 15 | + public class ReadOnlyCollectionComparer : BaseTypeComparer |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// The main constructor. |
| 19 | + /// </summary> |
| 20 | + public ReadOnlyCollectionComparer(RootComparer rootComparer) : base(rootComparer) |
| 21 | + { |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Returns true if both objects are ReadOnlyCollections. |
| 27 | + /// </summary> |
| 28 | + /// <param name="type1">The type of the first object</param> |
| 29 | + /// <param name="type2">The type of the second object</param> |
| 30 | + /// <returns></returns> |
| 31 | + public override bool IsTypeMatch(Type type1, Type type2) |
| 32 | + { |
| 33 | + return TypeHelper.IsReadOnlyCollection(type1) && TypeHelper.IsReadOnlyCollection(type2); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Compare two ReadOnlyCollections. |
| 38 | + /// </summary> |
| 39 | + public override void CompareType(CompareParms parms) |
| 40 | + { |
| 41 | + if (!parms.Config.CompareReadOnly) |
| 42 | + return; |
| 43 | + |
| 44 | + try |
| 45 | + { |
| 46 | + parms.Result.AddParent(parms.Object1); |
| 47 | + parms.Result.AddParent(parms.Object2); |
| 48 | + |
| 49 | + Type t1 = parms.Object1.GetType(); |
| 50 | + Type t2 = parms.Object2.GetType(); |
| 51 | + |
| 52 | + //Check if the class type should be excluded based on the configuration |
| 53 | + if (ExcludeLogic.ShouldExcludeClass(parms.Config, t1, t2)) |
| 54 | + return; |
| 55 | + |
| 56 | + parms.Object1Type = t1; |
| 57 | + parms.Object2Type = t2; |
| 58 | + |
| 59 | + bool countsDifferent = CollectionsDifferentCount(parms); |
| 60 | + |
| 61 | + if (parms.Result.ExceededDifferences) |
| 62 | + return; |
| 63 | + |
| 64 | + if (parms.Config.IgnoreCollectionOrder) |
| 65 | + { |
| 66 | + IgnoreOrderLogic logic = new IgnoreOrderLogic(RootComparer); |
| 67 | + logic.CompareEnumeratorIgnoreOrder(parms, countsDifferent); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + CompareItems(parms); |
| 72 | + } |
| 73 | + } |
| 74 | + finally |
| 75 | + { |
| 76 | + parms.Result.RemoveParent(parms.Object1); |
| 77 | + parms.Result.RemoveParent(parms.Object2); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void CompareItems(CompareParms parms) |
| 82 | + { |
| 83 | + int count = 0; |
| 84 | + |
| 85 | + IEnumerator enumerator1 = ((ICollection)parms.Object1).GetEnumerator(); |
| 86 | + IEnumerator enumerator2 = ((ICollection)parms.Object2).GetEnumerator(); |
| 87 | + |
| 88 | + while (enumerator1.MoveNext() && enumerator2.MoveNext()) |
| 89 | + { |
| 90 | + string currentBreadCrumb = AddBreadCrumb(parms.Config, parms.BreadCrumb, string.Empty, string.Empty, count); |
| 91 | + |
| 92 | + CompareParms childParms = new CompareParms |
| 93 | + { |
| 94 | + Result = parms.Result, |
| 95 | + Config = parms.Config, |
| 96 | + ParentObject1 = parms.Object1, |
| 97 | + ParentObject2 = parms.Object2, |
| 98 | + Object1 = enumerator1.Current, |
| 99 | + Object2 = enumerator2.Current, |
| 100 | + BreadCrumb = currentBreadCrumb |
| 101 | + }; |
| 102 | + |
| 103 | + RootComparer.Compare(childParms); |
| 104 | + |
| 105 | + if (parms.Result.ExceededDifferences) |
| 106 | + return; |
| 107 | + |
| 108 | + count++; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private bool CollectionsDifferentCount(CompareParms parms) |
| 113 | + { |
| 114 | + //Get count by reflection since we can't cast it to HashSet<> |
| 115 | + int count1 = ((ICollection)parms.Object1).Count; |
| 116 | + int count2 = ((ICollection)parms.Object2).Count; |
| 117 | + |
| 118 | + //Objects must be the same length |
| 119 | + if (count1 != count2) |
| 120 | + { |
| 121 | + Difference difference = new Difference |
| 122 | + { |
| 123 | + ParentObject1 = parms.ParentObject1, |
| 124 | + ParentObject2 = parms.ParentObject2, |
| 125 | + PropertyName = parms.BreadCrumb, |
| 126 | + Object1Value = count1.ToString(CultureInfo.InvariantCulture), |
| 127 | + Object2Value = count2.ToString(CultureInfo.InvariantCulture), |
| 128 | + ChildPropertyName = "Count", |
| 129 | + Object1 = parms.Object1, |
| 130 | + Object2 = parms.Object2 |
| 131 | + }; |
| 132 | + |
| 133 | + AddDifference(parms.Result, difference); |
| 134 | + |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments