Created
November 1, 2013 18:31
-
-
Save alex-berezan/7269718 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// General purpose extensions. | |
/// </summary> | |
public static class Extensions | |
{ | |
/// <summary> | |
/// Guarantees the not null. | |
/// </summary> | |
/// <typeparam name="T">Type of passed instance.</typeparam> | |
/// <param name="instance">The instance.</param> | |
/// <param name="instanceName">Name of the instance.</param> | |
/// <returns> | |
/// Instance passed as input parameter. | |
/// </returns> | |
/// <exception cref="System.ArgumentException">When instance passed is empty.</exception> | |
public static T GuaranteedNotNull<T>(this T instance, string instanceName) where T : class | |
{ | |
if (instance == null) | |
{ | |
throw new ArgumentException(string.Format("'{0}' is not allowed to be null", instanceName), instanceName); | |
} | |
return instance; | |
} | |
/// <summary> | |
/// Convert instance to single line string. | |
/// </summary> | |
/// <param name="instance">The instance.</param> | |
/// <param name="separator">Separator that joins all the parts into one big string.</param> | |
/// <returns> | |
/// The <see cref="System.String"/> instance. | |
/// </returns> | |
public static string PublicPropertiesToString(this object instance, string separator = ", ") | |
{ | |
Params.ShouldNotBeNull(instance, "instance"); | |
PropertyInfo[] properties = instance.GetType().GetProperties() | |
.Where(pi => !pi.GetMethod.GetParameters().Any()) | |
.ToArray(); | |
string[] entityDetails = properties.ToDictionary(pi => pi.Name, pi => pi.GetValue(instance, null)) | |
.Select(PropertyPairToString) | |
.ToArray(); | |
return String.Join(separator, entityDetails); | |
} | |
/// <summary> | |
/// Converts collection of strings to one multi-line string. | |
/// </summary> | |
/// <param name="lines">The lines.</param> | |
/// <returns> | |
/// The <see cref="System.String"/> instance. | |
/// </returns> | |
public static string ToMultiLineString(this IEnumerable<string> lines) | |
{ | |
return String.Join(Environment.NewLine, lines); | |
} | |
/// <summary> | |
/// Determines whether current collection is equal to specified. Order matters. | |
/// </summary> | |
/// <typeparam name="T">Type of items stored in collections.</typeparam> | |
/// <param name="left">The left.</param> | |
/// <param name="right">The right.</param> | |
/// <returns> | |
/// Comparison result. | |
/// </returns> | |
public static bool IsOrderedEqualTo<T>(this IEnumerable<T> left, IEnumerable<T> right) | |
{ | |
return IsOrderedEqualTo<T>(left, right, null); | |
} | |
/// <summary> | |
/// Determines whether current collection is equal to specified. Order matters. | |
/// </summary> | |
/// <typeparam name="T">Type of items stored in collections.</typeparam> | |
/// <param name="left">The left.</param> | |
/// <param name="right">The right.</param> | |
/// <param name="compare">Does the custom comparison.</param> | |
/// <returns>Comparison result.</returns> | |
public static bool IsOrderedEqualTo<T>(this IEnumerable<T> left, IEnumerable<T> right, Func<T, T, bool> compare) | |
{ | |
if (ReferenceEquals(left, right)) | |
{ | |
return true; | |
} | |
List<T> leftList = left.ToList(); | |
List<T> rightList = right.ToList(); | |
if (leftList.Count != rightList.Count) | |
{ | |
return false; | |
} | |
Func<T, T, bool> areEqual = compare ?? AreEqual; | |
foreach (int i in Enumerable.Range(0, leftList.Count)) | |
{ | |
if (!areEqual(leftList[i], rightList[i])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
/// <summary> | |
/// Determines whether current collection is equal to specified. Order does not matter. | |
/// </summary> | |
/// <typeparam name="T">Type of items stored in collections.</typeparam> | |
/// <param name="left">The left.</param> | |
/// <param name="right">The right.</param> | |
/// <returns>Comparison result.</returns> | |
public static bool IsUnOrderedEqualTo<T>(this IEnumerable<T> left, IEnumerable<T> right) | |
{ | |
return IsUnOrderedEqualTo<T>(left, right, null); | |
} | |
/// <summary> | |
/// Determines whether current collection is equal to specified. Order does not matter. | |
/// </summary> | |
/// <typeparam name="T">Type of items stored in collections.</typeparam> | |
/// <param name="left">The left.</param> | |
/// <param name="right">The right.</param> | |
/// <param name="compare">Does the custom comparison.</param> | |
/// <returns>Comparison result.</returns> | |
public static bool IsUnOrderedEqualTo<T>(this IEnumerable<T> left, IEnumerable<T> right, Func<T, T, bool> compare) | |
{ | |
if (ReferenceEquals(left, right)) | |
{ | |
return true; | |
} | |
List<T> leftList = left.ToList(); | |
List<T> rightList = right.ToList(); | |
if (leftList.Count != rightList.Count) | |
{ | |
return false; | |
} | |
Func<T, T, bool> areEqual = compare ?? AreEqual; | |
for (int i = 0; i < leftList.Count; i++) | |
{ | |
T l = leftList.Last(); | |
T equalRight = rightList.FirstOrDefault(r => areEqual(l, r)); | |
if (areEqual(equalRight, l)) | |
{ | |
leftList.RemoveAt(leftList.Count - 1); | |
rightList.Remove(equalRight); | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
/// <summary> | |
/// Ares the equal. | |
/// </summary> | |
/// <typeparam name="T">Type of items stored in collections.</typeparam> | |
/// <param name="a">A.</param> | |
/// <param name="b">The b.</param> | |
/// <returns> | |
/// The <see cref="System.Boolean"/> instance. | |
/// </returns> | |
private static bool AreEqual<T>(T a, T b) | |
{ | |
if (ReferenceEquals(a, b)) | |
{ | |
return true; | |
} | |
if (ReferenceEquals(a, null)) | |
{ | |
return false; | |
} | |
if (a.GetType() != b.GetType()) | |
{ | |
return false; | |
} | |
return a.Equals(b); | |
} | |
/// <summary> | |
/// Converts property to string. | |
/// </summary> | |
/// <param name="pair">The pair with property name and value.</param> | |
/// <returns> | |
/// The <see cref="System.String"/> instance. | |
/// </returns> | |
private static string PropertyPairToString(KeyValuePair<string, object> pair) | |
{ | |
return String.Format("{0}='{1}'", pair.Key, pair.Value == null ? "<null>" : pair.Value.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment