[corlib] Update ValueTuple implementation
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Model / PropertyEntryPropertyOrderComparer.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Model 
5 {
6     using System;
7     using System.Collections;
8     using System.Collections.Generic;
9     using System.Activities.Presentation.PropertyEditing;
10
11     // <summary>
12     // Compares PropertyEntry instances based on their PropertyOrder tokens
13     // as well as their DisplayNames
14     // </summary>
15     internal class PropertyEntryPropertyOrderComparer : IComparer, IComparer<PropertyEntry> 
16     {
17
18         private static readonly PropertyOrder DefaultOrder = PropertyOrder.Default;
19         private static PropertyEntryPropertyOrderComparer _instance;
20
21         // <summary>
22         // Gets a singleton instance of this class
23         // </summary>
24         public static PropertyEntryPropertyOrderComparer Instance 
25         {
26             get {
27                 if (_instance == null)
28                 {
29                     _instance = new PropertyEntryPropertyOrderComparer();
30                 }
31
32                 return _instance;
33             }
34         }
35
36         // <summary>
37         // Compares two instances of PropertyEntry class, using both
38         // PropertyOrder and DisplayName to cast its vote.
39         // </summary>
40         // <param name="x">Left side</param>
41         // <param name="y">Right side</param>
42         // <returns>Comparison result</returns>
43         public int Compare(object x, object y) 
44         {
45             return CompareCore(x, y);
46         }
47
48         // <summary>
49         // Compares two instances of PropertyEntry class, using both
50         // PropertyOrder and DisplayName to cast its vote.
51         // Same method, different signature.
52         // </summary>
53         // <param name="x">Left</param>
54         // <param name="y">Right</param>
55         // <param name="x">Left side</param>
56         // <param name="y">Right side</param>
57         // <returns>Comparison result</returns>
58         public int Compare(PropertyEntry x, PropertyEntry y) 
59         {
60             return CompareCore(x, y);
61         }
62
63         private static int CompareCore(object x, object y) 
64         {
65             ModelPropertyEntry j = x as ModelPropertyEntry;
66             ModelPropertyEntry k = y as ModelPropertyEntry;
67
68             if (j == null && k == null) 
69             {
70                 return 0;
71             }
72             if (j == null) 
73             {
74                 return -1;
75             }
76             if (k == null) 
77             {
78                 return 1;
79             }
80
81             PropertyOrder a = j.PropertyOrder ?? DefaultOrder;
82             PropertyOrder b = k.PropertyOrder ?? DefaultOrder;
83
84             int result = a.CompareTo(b);
85
86             return result != 0 ? result : string.Compare(j.DisplayName, k.DisplayName, StringComparison.CurrentCulture);
87         }
88     }
89 }