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