|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace T4Immutable { |
| 8 | + /// <summary> |
| 9 | + /// Collection of helper methods for T4Immutable. |
| 10 | + /// </summary> |
| 11 | + public static class Helpers { |
| 12 | + /// <summary> |
| 13 | + /// Check if two objects are equal. |
| 14 | + /// </summary> |
| 15 | + /// <typeparam name="T">Object type.</typeparam> |
| 16 | + /// <param name="a">First object.</param> |
| 17 | + /// <param name="b">Second object.</param> |
| 18 | + /// <returns>true if they are equal, false otherwise.</returns> |
| 19 | + public static bool BasicAreEqual<T>(T a, T b) { |
| 20 | + bool aIsNull = ReferenceEquals(a, null), bIsNull = ReferenceEquals(b, null); |
| 21 | + if (aIsNull && bIsNull) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + if (aIsNull || bIsNull) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + return a.Equals(b); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Check if two objects are equal, plus some special checking for KeyValuePairs and ICollections. |
| 32 | + /// </summary> |
| 33 | + /// <typeparam name="T">Object type.</typeparam> |
| 34 | + /// <param name="a">First object.</param> |
| 35 | + /// <param name="b">Second object.</param> |
| 36 | + /// <returns>true if they are equal, false otherwise.</returns> |
| 37 | + public static bool AreEqual<T>(T a, T b) { |
| 38 | + bool aIsNull = ReferenceEquals(a, null), bIsNull = ReferenceEquals(b, null); |
| 39 | + if (aIsNull && bIsNull) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + if (aIsNull || bIsNull) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + if (a.Equals(b)) { |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + // check for the special case of KeyValuePair (items of a dictionary) |
| 50 | + var aKvp = KeyValuePairHelper.TryExtractKeyValuePair(a); |
| 51 | + if (aKvp != null) { |
| 52 | + var bKvp = KeyValuePairHelper.TryExtractKeyValuePair(b); |
| 53 | + return AreEqual(aKvp.Item1, bKvp.Item1) && AreEqual(aKvp.Item2, bKvp.Item2); |
| 54 | + } |
| 55 | + |
| 56 | + // one extra check for collections |
| 57 | + |
| 58 | + var aCollection = a as ICollection; |
| 59 | + var bCollection = b as ICollection; |
| 60 | + if ((aCollection == null) || (bCollection == null)) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + if (aCollection.Count != bCollection.Count) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + var aEnum = aCollection.GetEnumerator(); |
| 69 | + var bEnum = bCollection.GetEnumerator(); |
| 70 | + |
| 71 | + while (aEnum.MoveNext()) { |
| 72 | + if (!bEnum.MoveNext()) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + object aCurrent = aEnum.Current, bCurrent = bEnum.Current; |
| 76 | + if (!AreEqual(aCurrent, bCurrent)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // all items so far are the same, but does b have one more? |
| 82 | + return !bEnum.MoveNext(); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Gets the hashcode of a single object. If the object is a collection it will make a hashcode of the collection. |
| 87 | + /// </summary> |
| 88 | + /// <param name="o">Object to make the hashcode for.</param> |
| 89 | + /// <returns>A hashcode.</returns> |
| 90 | + public static int GetHashCodeForSingleObject(object o) { |
| 91 | + if (ReferenceEquals(o, null)) { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + var oCollection = o as ICollection; |
| 96 | + if (oCollection == null) { |
| 97 | + // check for the special case of KeyValuePair (items of a dictionary) |
| 98 | + var kvp = KeyValuePairHelper.TryExtractKeyValuePair(o); |
| 99 | + if (kvp != null) { |
| 100 | + return GetHashCodeFor(kvp.Item1, kvp.Item2); |
| 101 | + } |
| 102 | + |
| 103 | + return o.GetHashCode(); |
| 104 | + } |
| 105 | + |
| 106 | + // make a hash of the items if it is a collection |
| 107 | + var oEnum = oCollection.GetEnumerator(); |
| 108 | + |
| 109 | + var list = new List<object>(); |
| 110 | + while (oEnum.MoveNext()) { |
| 111 | + list.Add(oEnum.Current); |
| 112 | + } |
| 113 | + |
| 114 | + return GetHashCodeFor(list.ToArray()); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Returns the hash code of the combination of a series of objects. |
| 119 | + /// </summary> |
| 120 | + /// <param name="objs">Objects to make the hash code for.</param> |
| 121 | + /// <returns>A hashcode.</returns> |
| 122 | + public static int GetHashCodeFor(params object[] objs) { |
| 123 | + if (objs.Length == 0) { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + |
| 127 | + const int prime = 486187739; |
| 128 | + // overflow is fine |
| 129 | + unchecked { |
| 130 | + var hash = 17; |
| 131 | + foreach (var o in objs) { |
| 132 | + hash = hash * prime + GetHashCodeForSingleObject(o); |
| 133 | + } |
| 134 | + return hash; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Returns the string representation of an object, null if null or [items] if a collection. |
| 140 | + /// </summary> |
| 141 | + /// <param name="o"></param> |
| 142 | + /// <returns>The string representation.</returns> |
| 143 | + public static string ToStringForSingleObject(object o) { |
| 144 | + if (ReferenceEquals(o, null)) { |
| 145 | + return "null"; |
| 146 | + } |
| 147 | + |
| 148 | + var oCollection = o as ICollection; |
| 149 | + if (oCollection == null) { |
| 150 | + // check for the special case of KeyValuePair (items of a dictionary) |
| 151 | + var kvp = KeyValuePairHelper.TryExtractKeyValuePair(o); |
| 152 | + if (kvp != null) { |
| 153 | + return "(" + ToStringForSingleObject(kvp.Item1) + ", " + ToStringForSingleObject(kvp.Item2) + ")"; |
| 154 | + } |
| 155 | + |
| 156 | + return o.ToString(); |
| 157 | + } |
| 158 | + |
| 159 | + // make a list of the items if it is a collection |
| 160 | + var oEnum = oCollection.GetEnumerator(); |
| 161 | + |
| 162 | + var list = new List<object>(); |
| 163 | + while (oEnum.MoveNext()) { |
| 164 | + list.Add(oEnum.Current); |
| 165 | + } |
| 166 | + |
| 167 | + var sb = new StringBuilder(); |
| 168 | + sb.Append("[ "); |
| 169 | + // TODO: this could be optimized by not using select and using the append on each object instead |
| 170 | + sb.Append(string.Join(", ", list.Select(ToStringForSingleObject))); |
| 171 | + sb.Append(" ]"); |
| 172 | + return sb.ToString(); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Returns the string representation of an immutable object. |
| 177 | + /// </summary> |
| 178 | + /// <param name="name">Immutable object type name.</param> |
| 179 | + /// <param name="objs">Properties of the immutable objects (name and value).</param> |
| 180 | + /// <returns>The string representation.</returns> |
| 181 | + public static string ToStringFor(string name, params Tuple<string, object>[] objs) { |
| 182 | + var sb = new StringBuilder(); |
| 183 | + sb.Append(name + " { "); |
| 184 | + // TODO: this could be optimized by not using select and using the append on each object instead |
| 185 | + sb.Append(string.Join(", ", objs.Select(v => v.Item1 + "=" + ToStringForSingleObject(v.Item2)))); |
| 186 | + sb.Append(" }"); |
| 187 | + return sb.ToString(); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments