// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Diagnostics; namespace Microsoft.Management.UI.Internal { /// /// The CustomTypeComparer is responsible for holding custom comparers /// for different types, which are in turn used to perform comparison /// operations instead of the default IComparable comparison. /// with a custom comparer /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")] public static class CustomTypeComparer { private static Dictionary comparers = new Dictionary(); /// /// The static constructor. /// static CustomTypeComparer() { comparers.Add(typeof(DateTime), new DateTimeApproximationComparer()); } /// /// Compares two objects and returns a value indicating /// whether one is less than, equal to, or greater than the other. /// /// /// The first object to compare. /// /// /// The second object to compare. /// /// /// A type implementing IComparable. /// /// /// If value1 is less than value2, then a value less than zero is returned. /// If value1 equals value2, than zero is returned. /// If value1 is greater than value2, then a value greater than zero is returned. /// public static int Compare(T value1, T value2) where T : IComparable { IComparer comparer; if (TryGetCustomComparer(out comparer) == false) { return value1.CompareTo(value2); } return comparer.Compare(value1, value2); } private static bool TryGetCustomComparer(out IComparer comparer) where T : IComparable { comparer = null; object uncastComparer = null; if (comparers.TryGetValue(typeof(T), out uncastComparer) == false) { return false; } Debug.Assert(uncastComparer is IComparer, "must be IComparer"); comparer = (IComparer)uncastComparer; return true; } } }