Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / corlib / System.Globalization / RegionInfo.cs
1 //
2 // System.Globalization.RegionInfo.cs
3 //
4 // Authors:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.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.Runtime.CompilerServices;
31 using System.Runtime.InteropServices;
32
33 namespace System.Globalization
34 {
35         [System.Runtime.InteropServices.ComVisible(true)]
36         [Serializable]
37         [StructLayout (LayoutKind.Sequential)]
38         public partial class RegionInfo
39         {
40                 static RegionInfo currentRegion;
41
42                 public static RegionInfo CurrentRegion {
43                         get {
44                                 if (currentRegion == null) {
45                                         CultureInfo ci = CultureInfo.CurrentCulture;
46                                         if (ci != null)
47                                                 return currentRegion = new RegionInfo (ci);
48
49 #if MONOTOUCH
50                                         currentRegion = CreateFromNSLocale ();
51 #endif
52                                 }
53                                 return currentRegion;
54                         }
55                 }
56                 
57                 // the following (instance) fields must be _first_ and stay synchronized
58                 // with the mono's MonoRegionInfo defined in mono/metadata/object-internals.h
59 #pragma warning disable 649
60                 int regionId;
61                 string iso2Name;
62                 string iso3Name;
63                 string win3Name;
64                 string englishName;
65                 string nativeName;
66                 string currencySymbol;
67                 string isoCurrencySymbol;
68                 string currencyEnglishName;
69                 string currencyNativeName;
70 #pragma warning restore 649
71                 
72                 public RegionInfo (int culture)
73                 {
74                         if (!GetByTerritory (CultureInfo.GetCultureInfo (culture)))
75                                 throw new ArgumentException (
76                                         String.Format ("Region ID {0} (0x{0:X4}) is not a supported region.", culture), "culture");
77                 }
78
79                 public RegionInfo (string name)
80                 {
81                         if (name == null)
82                                 throw new ArgumentNullException ();
83
84                         if (construct_internal_region_from_name (name.ToUpperInvariant ())) {
85                                 return;
86                         }
87                         if (!GetByTerritory (CultureInfo.GetCultureInfo (name)))
88                                 throw new ArgumentException (String.Format ("Region name {0} is not supported.", name), "name");
89                 }
90
91                 RegionInfo (CultureInfo ci)
92                 {
93                         if (ci.LCID == CultureInfo.InvariantCultureId) {
94                                 regionId = 244;
95                                 iso2Name = "IV";
96                                 iso3Name = "ivc";
97                                 win3Name = "IVC";
98                                 nativeName = englishName = "Invariant Country";
99                                 currencySymbol = "\u00A4";
100                                 isoCurrencySymbol ="XDR";
101                                 currencyEnglishName = currencyNativeName = "International Monetary Fund";
102                                 return;
103                         }
104
105                         if (ci.Territory == null)
106                                 throw new NotImplementedException ("Neutral region info");
107
108                         construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
109                 }
110
111                 bool GetByTerritory (CultureInfo ci)
112                 {
113                         if (ci == null)
114                                 throw new Exception ("INTERNAL ERROR: should not happen.");
115                         if (ci.IsNeutralCulture || ci.Territory == null)
116                                 return false;
117
118                         return construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
119                 }
120
121                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
122                 private extern bool construct_internal_region_from_name (string name);
123
124                 [System.Runtime.InteropServices.ComVisible(false)]
125                 public virtual string CurrencyEnglishName {
126                         get { return currencyEnglishName; }
127                 }
128
129                 public virtual string CurrencySymbol {
130                         get { return currencySymbol; }
131                 }
132
133                 [MonoTODO ("DisplayName currently only returns the EnglishName")]
134                 public virtual string DisplayName {
135                         get { return englishName; }
136                 }
137
138                 public virtual string EnglishName {
139                         get { return englishName; }
140                 }
141
142                 [System.Runtime.InteropServices.ComVisible(false)]
143                 public virtual int GeoId {
144                         get { return regionId; }
145                 }
146
147                 public virtual bool IsMetric {
148                         get {
149                                 switch (iso2Name) {
150                                 case "US":
151                                 case "UK":
152                                         return false;
153                                 default:
154                                         return true;
155                                 }
156                         }
157                 }
158
159                 public virtual string ISOCurrencySymbol {
160                         get { return isoCurrencySymbol; }
161                 }
162
163                 [ComVisible(false)]
164                 public virtual string NativeName {
165                         get { return nativeName; }
166                 }
167
168                 [ComVisible(false)]
169                 public virtual string CurrencyNativeName {
170                         get { return currencyNativeName; }
171                 }
172
173                 public virtual string Name {
174                         get { return iso2Name; }
175                 }
176
177                 public virtual string ThreeLetterISORegionName {
178                         get { return iso3Name; }
179                 }
180
181                 public virtual string ThreeLetterWindowsRegionName {
182                         get { return win3Name; }
183                 }
184                 
185                 public virtual string TwoLetterISORegionName {
186                         get { return iso2Name; }
187                 }
188
189                 public override bool Equals (object value)
190                 {
191                         RegionInfo other = value as RegionInfo;
192                         return other != null && Name == other.Name;
193                 }
194
195                 public override int GetHashCode ()
196                 {
197                         return Name.GetHashCode ();
198                 }
199
200                 public override string ToString ()
201                 {
202                         return Name;
203                 }
204         }
205 }