2003-12-06 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.Globalization / SortKey.cs
1 //
2 // System.Globalization.SortKey.cs
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc.
8 //
9
10 namespace System.Globalization {
11
12         [Serializable]
13         public class SortKey {
14                 private string str;
15                 private CompareOptions options;
16                 private byte[] key;
17                 private int lcid;
18                 
19                 /* Hide the .ctor() */
20                 SortKey() {}
21
22                 internal SortKey (int lcid, string source,
23                                   CompareOptions options)
24                 {
25                         this.lcid=lcid;
26                         str=source;
27                         this.options=options;
28                 }
29
30                 public virtual byte[] KeyData
31                 {
32                         get {
33                                 return(key);
34                         }
35                 }
36
37                 public virtual string OriginalString
38                 {
39                         get {
40                                 return(str);
41                         }
42                 }
43
44                 public static int Compare(SortKey sortkey1, SortKey sortkey2)
45                 {
46                         if(sortkey1==null) {
47                                 throw new ArgumentNullException ("sortkey1");
48                         }
49                         if(sortkey2==null) {
50                                 throw new ArgumentNullException ("sortkey2");
51                         }
52
53                         byte[] keydata1=sortkey1.key;
54                         byte[] keydata2=sortkey2.key;
55
56                         if(keydata1.Length==0) {
57                                 if(keydata2.Length==0) {
58                                         return(0);
59                                 }
60                                 return(-1);
61                         }
62                         
63                         int min_len=(keydata1.Length < keydata2.Length)?
64                                 keydata1.Length:keydata2.Length;
65
66                         for(int i=0; i<min_len; i++) {
67                                 if(keydata1[i] > keydata2[i]) {
68                                         return(1);
69                                 } else if(keydata1[i] < keydata2[i]) {
70                                         return(-1);
71                                 }
72                         }
73
74                         if(keydata1.Length < keydata2.Length) {
75                                 return(-1);
76                         } else if (keydata1.Length > keydata2.Length) {
77                                 return(1);
78                         } else {
79                                 return(0);
80                         }
81                 }
82
83                 public override bool Equals(object value)
84                 {
85                         SortKey other=(value as SortKey);
86                         if(other!=null) {
87                                 if((this.lcid==other.lcid) &&
88                                    (this.options==other.options) &&
89                                    (Compare (this, other)==0)) {
90                                         return(true);
91                                 }
92                         }
93
94                         return(false);
95                 }
96
97                 public override int GetHashCode()
98                 {
99                         return(str.GetHashCode ());
100                 }
101
102                 public override string ToString()
103                 {
104                         return("SortKey - "+lcid+", "+options+", "+str);
105                 }
106         }
107 }