Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[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                 // This property is not synchronized with CurrentCulture, so
43                 // we need to use bootstrap CurrentCulture LCID.
44                 public static RegionInfo CurrentRegion {
45                         get {
46                                 if (currentRegion == null) {
47                                         // make sure to fill BootstrapCultureID.
48                                         CultureInfo ci = CultureInfo.CurrentCulture;
49                                         // If current culture is invariant then region is not available.
50                                         if (ci != null && CultureInfo.BootstrapCultureID != 0x7F)
51                                                 currentRegion = new RegionInfo (CultureInfo.BootstrapCultureID);
52                                         else
53 #if MONOTOUCH
54                                                 currentRegion = CreateFromNSLocale ();
55 #else
56                                                 currentRegion = null;
57 #endif
58                                 }
59                                 return currentRegion;
60                         }
61                 }
62                 
63                 // the following (instance) fields must be _first_ and stay synchronized
64                 // with the mono's MonoRegionInfo defined in mono/metadata/object-internals.h
65 #pragma warning disable 649
66                 int regionId;
67                 string iso2Name;
68                 string iso3Name;
69                 string win3Name;
70                 string englishName;
71                 string nativeName;
72                 string currencySymbol;
73                 string isoCurrencySymbol;
74                 string currencyEnglishName;
75                 string currencyNativeName;
76 #pragma warning restore 649
77                 
78                 public RegionInfo (int culture)
79                 {
80                         if (!GetByTerritory (CultureInfo.GetCultureInfo (culture)))
81                                 throw new ArgumentException (
82                                         String.Format ("Region ID {0} (0x{0:X4}) is not a supported region.", culture), "culture");
83                 }
84
85                 public RegionInfo (string name)
86                 {
87                         if (name == null)
88                                 throw new ArgumentNullException ();
89
90                         if (construct_internal_region_from_name (name.ToUpperInvariant ())) {
91                                 return;
92                         }
93                         if (!GetByTerritory (CultureInfo.GetCultureInfo (name)))
94                                 throw new ArgumentException (String.Format ("Region name {0} is not supported.", name), "name");
95                 }
96
97                 bool GetByTerritory (CultureInfo ci)
98                 {
99                         if (ci == null)
100                                 throw new Exception ("INTERNAL ERROR: should not happen.");
101                         if (ci.IsNeutralCulture || ci.Territory == null)
102                                 return false;
103
104                         return construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
105                 }
106
107                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
108                 private extern bool construct_internal_region_from_name (string name);
109
110                 [System.Runtime.InteropServices.ComVisible(false)]
111                 public virtual string CurrencyEnglishName {
112                         get { return currencyEnglishName; }
113                 }
114
115                 public virtual string CurrencySymbol {
116                         get { return currencySymbol; }
117                 }
118
119                 [MonoTODO ("DisplayName currently only returns the EnglishName")]
120                 public virtual string DisplayName {
121                         get { return englishName; }
122                 }
123
124                 public virtual string EnglishName {
125                         get { return englishName; }
126                 }
127
128                 [System.Runtime.InteropServices.ComVisible(false)]
129                 public virtual int GeoId {
130                         get { return regionId; }
131                 }
132
133                 public virtual bool IsMetric {
134                         get {
135                                 switch (iso2Name) {
136                                 case "US":
137                                 case "UK":
138                                         return false;
139                                 default:
140                                         return true;
141                                 }
142                         }
143                 }
144
145                 public virtual string ISOCurrencySymbol {
146                         get { return isoCurrencySymbol; }
147                 }
148
149                 [ComVisible(false)]
150                 public virtual string NativeName {
151                         get { return nativeName; }
152                 }
153
154                 [ComVisible(false)]
155                 public virtual string CurrencyNativeName {
156                         get { return currencyNativeName; }
157                 }
158
159                 public virtual string Name {
160                         get { return iso2Name; }
161                 }
162
163                 public virtual string ThreeLetterISORegionName {
164                         get { return iso3Name; }
165                 }
166
167                 public virtual string ThreeLetterWindowsRegionName {
168                         get { return win3Name; }
169                 }
170                 
171                 public virtual string TwoLetterISORegionName {
172                         get { return iso2Name; }
173                 }
174
175                 public override bool Equals (object value)
176                 {
177                         RegionInfo other = value as RegionInfo;
178                         return other != null && Name == other.Name;
179                 }
180
181                 public override int GetHashCode ()
182                 {
183                         return Name.GetHashCode ();
184                 }
185
186                 public override string ToString ()
187                 {
188                         return Name;
189                 }
190         }
191 }