2007-05-15 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Globalization / RegionInfo.cs
1 //
2 // RegionInfo.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System.Globalization;
31 using System.Runtime.CompilerServices;
32
33 namespace System.Globalization
34 {
35 #if NET_2_0
36         [System.Runtime.InteropServices.ComVisible(true)]
37 #endif
38         [Serializable]
39         public class RegionInfo
40         {
41                 static RegionInfo currentRegion;
42
43                 // This property is not synchronized with CurrentCulture, so
44                 // we need to use bootstrap CurrentCulture LCID.
45                 public static RegionInfo CurrentRegion {
46                         get {
47                                 if (currentRegion == null) {
48                                         // make sure to fill BootstrapCultureID.
49                                         CultureInfo ci = CultureInfo.CurrentCulture;
50                                         // If current culture is invariant then region is not available.
51                                         if (ci == null || CultureInfo.BootstrapCultureID == 0x7F)
52                                                 return null;
53                                         currentRegion = new RegionInfo (CultureInfo.BootstrapCultureID);
54                                 }
55                                 return currentRegion;
56                         }
57                 }
58
59                 int lcid; // it is used only for Equals() (not even used in GetHashCode()).
60                 int regionId;
61                 string iso2Name;
62                 string iso3Name;
63                 string win3Name;
64                 string englishName;
65                 string currencySymbol;
66                 string isoCurrencySymbol;
67                 string currencyEnglishName;
68
69                 public RegionInfo (int lcid)
70                 {
71 #if NET_2_0
72                         if (!GetByTerritory (CultureInfo.GetCultureInfo (lcid)))
73                                 throw new ArgumentException (
74                                         String.Format ("Region ID {0} (0x{0:X4}) is not a supported region.", lcid), "lcid");
75 #else
76                         if (!construct_internal_region_from_lcid (lcid))
77                                 throw new ArgumentException (
78                                         String.Format ("Region ID {0} (0x{0:X4}) is not a " +
79                                                         "supported region.", lcid), "lcid");
80 #endif
81                 }
82
83                 public RegionInfo (string name)
84                 {
85                         if (name == null)
86                                 throw new ArgumentNullException ();
87
88 #if NET_2_0
89                         if (construct_internal_region_from_name (name.ToUpperInvariant ())) {
90                                 lcid = name.GetHashCode (); // random-ish
91                                 return;
92                         }
93                         if (!GetByTerritory (CultureInfo.GetCultureInfo (name)))
94                                 throw new ArgumentException (String.Format ("Region name {0} is not supported.", name), "name");
95 #else
96                         if (!construct_internal_region_from_name (name.ToUpperInvariant ()))
97                                 throw new ArgumentException ("Region name " + name +
98                                                 " is not supported.", "name");
99 #endif
100                 }
101
102                 bool GetByTerritory (CultureInfo ci)
103                 {
104                         if (ci == null)
105                                 throw new Exception ("INTERNAL ERROR: should not happen.");
106                         if (ci.IsNeutralCulture || ci.Territory == null)
107                                 return false;
108                         this.lcid = ci.LCID;
109                         return construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
110                 }
111
112                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
113                 private extern bool construct_internal_region_from_lcid (int lcid);
114
115                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
116                 private extern bool construct_internal_region_from_name (string name);
117
118 #if NET_2_0
119                 [System.Runtime.InteropServices.ComVisible(false)]
120                 public virtual string CurrencyEnglishName {
121                         get { return currencyEnglishName; }
122                 }
123 #endif
124
125                 public virtual string CurrencySymbol {
126                         get { return currencySymbol; }
127                 }
128
129                 [MonoTODO ("DisplayName currently only returns the EnglishName")]
130                 public virtual string DisplayName {
131                         get { return englishName; }
132                 }
133
134                 public virtual string EnglishName {
135                         get { return englishName; }
136                 }
137
138 #if NET_2_0
139                 public virtual int GeoId {
140                         get { return regionId; }
141                 }
142 #endif
143
144                 public virtual bool IsMetric {
145                         get {
146                                 switch (iso2Name) {
147                                 case "US":
148                                 case "UK":
149                                         return false;
150                                 default:
151                                         return true;
152                                 }
153                         }
154                 }
155
156                 public virtual string ISOCurrencySymbol {
157                         get { return isoCurrencySymbol; }
158                 }
159
160 #if NET_2_0
161                 [System.Runtime.InteropServices.ComVisible(false)]
162                 public virtual string NativeName {
163                         get { return DisplayName; }
164                 }
165
166                 [MonoTODO ("Not implemented")]
167                 public virtual string CurrencyNativeName {
168                         get { throw new NotImplementedException (); }
169                 }
170 #endif
171
172                 public virtual string Name {
173                         get { return iso2Name; }
174                 }
175
176                 public virtual string ThreeLetterISORegionName {
177                         get { return iso3Name; }
178                 }
179
180                 public virtual string ThreeLetterWindowsRegionName {
181                         get { return win3Name; }
182                 }
183                 
184                 public virtual string TwoLetterISORegionName {
185                         get { return iso2Name; }
186                 }
187
188                 //
189                 // methods
190
191 #if NET_2_0
192                 public override bool Equals (object value)
193                 {
194                         RegionInfo other = value as RegionInfo;
195                         return other != null && lcid == other.lcid;
196                 }
197
198                 public override int GetHashCode ()
199                 {
200                         return (int) (0x80000000 + (regionId << 3) + regionId); // it i still based on regionId
201                 }
202 #else
203                 public override bool Equals (object value)
204                 {
205                         RegionInfo other = value as RegionInfo;
206                         return other != null && regionId == other.regionId;
207                 }
208
209                 public override int GetHashCode () {
210                         return regionId;
211                 }
212 #endif
213
214                 public override string ToString ()
215                 {
216                         return Name;
217                 }
218         }
219 }