[kaffe] CVS kaffe (robilad): Resynced with GNU Classpath: fixes for Calendar

Kaffe CVS cvs-commits at kaffe.org
Tue Sep 28 21:53:17 PDT 2004


PatchSet 5232 
Date: 2004/09/29 04:49:19
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath: fixes for Calendar

2004-09-28  Dalibor Topic  <robilad at kaffe.org>

* libraries/javalib/java/util/Calendar.java:
Resynced with GNU Classpath.

2004-09-27  Bryce McKinlay  <mckinlay at redhat.com>

* java.util.Calendar.java (cache): New private static field. Cached
mappings of locales->calendar classes.
(ctorArgTypes): New private static field. Singleton argument for
calendar class constructor lookup.
(getInstance): Cache Locale->Calendar class mappings using HashMap.
Optimize by bypassing reflection instantiation for the
GregorianCalendar case.

Members: 
	ChangeLog:1.2786->1.2787 
	libraries/javalib/java/util/Calendar.java:1.23->1.24 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.2786 kaffe/ChangeLog:1.2787
--- kaffe/ChangeLog:1.2786	Wed Sep 29 04:23:49 2004
+++ kaffe/ChangeLog	Wed Sep 29 04:49:19 2004
@@ -5,6 +5,21 @@
 
 2004-09-28  Dalibor Topic  <robilad at kaffe.org>
 
+	* libraries/javalib/java/util/Calendar.java:
+	Resynced with GNU Classpath.
+	
+	2004-09-27  Bryce McKinlay  <mckinlay at redhat.com>
+
+	* java.util.Calendar.java (cache): New private static field. Cached
+	mappings of locales->calendar classes.
+	(ctorArgTypes): New private static field. Singleton argument for
+	calendar class constructor lookup.
+	(getInstance): Cache Locale->Calendar class mappings using HashMap.
+	Optimize by bypassing reflection instantiation for the
+	GregorianCalendar case.
+
+2004-09-28  Dalibor Topic  <robilad at kaffe.org>
+
 	* libraries/javalib/java/awt/image/BandedSampleModel.java:
 	New file, taken from GNU Classpath.
 	
Index: kaffe/libraries/javalib/java/util/Calendar.java
diff -u kaffe/libraries/javalib/java/util/Calendar.java:1.23 kaffe/libraries/javalib/java/util/Calendar.java:1.24
--- kaffe/libraries/javalib/java/util/Calendar.java:1.23	Wed Aug 18 13:44:26 2004
+++ kaffe/libraries/javalib/java/util/Calendar.java	Wed Sep 29 04:49:20 2004
@@ -42,6 +42,7 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 
 /**
@@ -436,6 +437,16 @@
     return getInstance(TimeZone.getDefault(), locale);
   }
 
+  /** 
+   * Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle
+   * lookup for every getInstance call.  
+   */
+  private static HashMap cache = new HashMap();
+
+  /** Preset argument types for calendar-class constructor lookup.  */
+  private static Class[] ctorArgTypes
+    = new Class[] {TimeZone.class, Locale.class};
+
   /**
    * Creates a calendar representing the actual time, using the given
    * time zone and locale.
@@ -444,29 +455,58 @@
    */
   public static synchronized Calendar getInstance(TimeZone zone, Locale locale)
   {
-    String calendarClassName = null;
-    ResourceBundle rb = getBundle(locale);
-    calendarClassName = rb.getString("calendarClass");
-    if (calendarClassName != null)
+    Class calendarClass = (Class) cache.get(locale);
+    Throwable exception = null;
+
+    try
       {
-	try
+	if (calendarClass == null)
 	  {
-	    Class calendarClass = Class.forName(calendarClassName);
-	    if (Calendar.class.isAssignableFrom(calendarClass))
+	    ResourceBundle rb = getBundle(locale);
+	    String calendarClassName = rb.getString("calendarClass");
+
+	    if (calendarClassName != null)
 	      {
-		return (Calendar) calendarClass.getConstructor(
-		  new Class[] { TimeZone.class, Locale.class}
-		).newInstance(new Object[] {zone, locale} );
+		calendarClass = Class.forName(calendarClassName);
+		if (Calendar.class.isAssignableFrom(calendarClass))
+		  cache.put(locale, calendarClass);
 	      }
 	  }
-	catch (ClassNotFoundException ex) {}
-	catch (IllegalAccessException ex) {}
-	catch (NoSuchMethodException ex) {}
-	catch (InstantiationException ex) {}
-	catch (InvocationTargetException ex) {}
-	// XXX should we ignore these errors or throw an exception ?
+
+        // GregorianCalendar is by far the most common case. Optimize by 
+	// avoiding reflection.
+	if (calendarClass == GregorianCalendar.class)
+	  return new GregorianCalendar(zone, locale);
+
+	if (Calendar.class.isAssignableFrom(calendarClass))
+	  {
+	    Constructor ctor = calendarClass.getConstructor(ctorArgTypes);
+	    return (Calendar) ctor.newInstance(new Object[] {zone, locale});
+	  }
+      }
+    catch (ClassNotFoundException ex)
+      {
+	exception = ex;
+      }
+    catch (IllegalAccessException ex)
+      {
+	exception = ex;
+      }
+    catch (NoSuchMethodException ex)
+      {
+	exception = ex;
+      }
+    catch (InstantiationException ex)
+      {
+	exception = ex;
+      }
+    catch (InvocationTargetException ex)
+      {
+	exception = ex;
       }
-    return new GregorianCalendar(zone, locale);
+    
+    throw new RuntimeException("Error instantiating calendar for locale " +
+			       locale, exception);
   }
 
   /**




More information about the kaffe mailing list