2009-07-30 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / corlib / System.Globalization / RegionInfo.cs
index 12d5335b021d3ba28e573b676c65f5820b46f9b1..c849a2511a0824a3902189c88c4b79cc5a6351f0 100644 (file)
@@ -1,5 +1,5 @@
 //
-// RegionInfo.cs
+// System.Globalization.RegionInfo.cs
 //
 // Author:
 //     Atsushi Enomoto  <atsushi@ximian.com>
@@ -32,6 +32,9 @@ using System.Runtime.CompilerServices;
 
 namespace System.Globalization
 {
+#if NET_2_0
+       [System.Runtime.InteropServices.ComVisible(true)]
+#endif
        [Serializable]
        public class RegionInfo
        {
@@ -53,6 +56,11 @@ namespace System.Globalization
                        }
                }
 
+#if NET_2_0
+               int lcid; // it is used only for Equals() (not even used in GetHashCode()).
+#endif
+
+#pragma warning disable 649
                int regionId;
                string iso2Name;
                string iso3Name;
@@ -61,13 +69,20 @@ namespace System.Globalization
                string currencySymbol;
                string isoCurrencySymbol;
                string currencyEnglishName;
+#pragma warning restore 649
 
-               public RegionInfo (int lcid)
+               public RegionInfo (int culture)
                {
-                       if (!construct_internal_region_from_lcid (lcid))
+#if NET_2_0
+                       if (!GetByTerritory (CultureInfo.GetCultureInfo (culture)))
+                               throw new ArgumentException (
+                                       String.Format ("Region ID {0} (0x{0:X4}) is not a supported region.", culture), "culture");
+#else
+                       if (!construct_internal_region_from_lcid (culture))
                                throw new ArgumentException (
                                        String.Format ("Region ID {0} (0x{0:X4}) is not a " +
-                                                       "supported region.", lcid), "lcid");
+                                                       "supported region.", culture), "culture");
+#endif
                }
 
                public RegionInfo (string name)
@@ -75,18 +90,42 @@ namespace System.Globalization
                        if (name == null)
                                throw new ArgumentNullException ();
 
+#if NET_2_0
+                       if (construct_internal_region_from_name (name.ToUpperInvariant ())) {
+                               lcid = name.GetHashCode (); // random-ish
+                               return;
+                       }
+                       if (!GetByTerritory (CultureInfo.GetCultureInfo (name)))
+                               throw new ArgumentException (String.Format ("Region name {0} is not supported.", name), "name");
+#else
                        if (!construct_internal_region_from_name (name.ToUpperInvariant ()))
                                throw new ArgumentException ("Region name " + name +
                                                " is not supported.", "name");
+#endif
                }
 
+#if NET_2_0
+               bool GetByTerritory (CultureInfo ci)
+               {
+                       if (ci == null)
+                               throw new Exception ("INTERNAL ERROR: should not happen.");
+                       if (ci.IsNeutralCulture || ci.Territory == null)
+                               return false;
+                       this.lcid = ci.LCID;
+                       return construct_internal_region_from_name (ci.Territory.ToUpperInvariant ());
+               }
+#endif         
+
+#if !NET_2_0
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern bool construct_internal_region_from_lcid (int lcid);
+#endif
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern bool construct_internal_region_from_name (string name);
 
 #if NET_2_0
+               [System.Runtime.InteropServices.ComVisible(false)]
                public virtual string CurrencyEnglishName {
                        get { return currencyEnglishName; }
                }
@@ -96,7 +135,7 @@ namespace System.Globalization
                        get { return currencySymbol; }
                }
 
-               [MonoTODO]
+               [MonoTODO ("DisplayName currently only returns the EnglishName")]
                public virtual string DisplayName {
                        get { return englishName; }
                }
@@ -105,6 +144,13 @@ namespace System.Globalization
                        get { return englishName; }
                }
 
+#if NET_2_0
+               [System.Runtime.InteropServices.ComVisible(false)]
+               public virtual int GeoId {
+                       get { return regionId; }
+               }
+#endif
+
                public virtual bool IsMetric {
                        get {
                                switch (iso2Name) {
@@ -122,12 +168,13 @@ namespace System.Globalization
                }
 
 #if NET_2_0
-               [MonoTODO]
+               [System.Runtime.InteropServices.ComVisible(false)]
                public virtual string NativeName {
                        get { return DisplayName; }
                }
 
-               [MonoTODO]
+               [MonoTODO ("Not implemented")]
+               [System.Runtime.InteropServices.ComVisible(false)]
                public virtual string CurrencyNativeName {
                        get { throw new NotImplementedException (); }
                }
@@ -153,6 +200,16 @@ namespace System.Globalization
                // methods
 
 #if NET_2_0
+               public override bool Equals (object value)
+               {
+                       RegionInfo other = value as RegionInfo;
+                       return other != null && lcid == other.lcid;
+               }
+
+               public override int GetHashCode ()
+               {
+                       return (int) (0x80000000 + (regionId << 3) + regionId); // it i still based on regionId
+               }
 #else
                public override bool Equals (object value)
                {