From b366de49e330e1ebd93bb10ee3ebbd1e35d249bb Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Thu, 23 Aug 2007 18:08:08 +0000
Subject: [PATCH] Message/MessageDescriptor performance improvements:

---
 opends/src/messages/src/org/opends/messages/MessageDescriptor.java |  140 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 137 insertions(+), 3 deletions(-)

diff --git a/opends/src/messages/src/org/opends/messages/MessageDescriptor.java b/opends/src/messages/src/org/opends/messages/MessageDescriptor.java
index 214ed13..aa6365f 100644
--- a/opends/src/messages/src/org/opends/messages/MessageDescriptor.java
+++ b/opends/src/messages/src/org/opends/messages/MessageDescriptor.java
@@ -29,6 +29,8 @@
 
 import java.util.Locale;
 import java.util.ResourceBundle;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Base class for all Message descriptor classes.
@@ -68,6 +70,8 @@
      */
     private Message message;
 
+    private boolean requiresFormat;
+
     /**
      * Creates a parameterized instance.
      * @param rbBase base of the backing resource bundle
@@ -81,6 +85,7 @@
               Severity severity, int ordinal, ClassLoader classLoader) {
       super(rbBase, key, category, severity, ordinal, classLoader);
       message = new Message(this);
+      requiresFormat = containsArgumentLiterals(getFormatString());
     }
 
     /**
@@ -96,6 +101,7 @@
               Severity severity, int ordinal, ClassLoader classLoader) {
       super(rbBase, key, mask, severity, ordinal, classLoader);
       message = new Message(this);
+      requiresFormat = containsArgumentLiterals(getFormatString());
     }
 
     /**
@@ -106,6 +112,12 @@
       return message;
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return requiresFormat;
+    }
   }
 
   /**
@@ -152,6 +164,13 @@
       return new Message(this, a1);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -199,6 +218,13 @@
       return new Message(this, a1, a2);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -247,6 +273,13 @@
       return new Message(this, a1, a2, a3);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -296,6 +329,13 @@
       return new Message(this, a1, a2, a3, a4);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -346,6 +386,13 @@
       return new Message(this, a1, a2, a3, a4, a5);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -397,6 +444,13 @@
       return new Message(this, a1, a2, a3, a4, a5, a6);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -451,6 +505,13 @@
       return new Message(this, a1, a2, a3, a4, a5, a6, a7);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -507,6 +568,13 @@
       return new Message(this, a1, a2, a3, a4, a5, a6, a7, a8);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -563,6 +631,13 @@
       return new Message(this, a1, a2, a3, a4, a5, a6, a7, a8, a9);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -620,6 +695,13 @@
       return new Message(this, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -679,6 +761,13 @@
       return new Message(this, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -728,6 +817,13 @@
       return new Message(this, args);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return true;
+    }
+
   }
 
   /**
@@ -740,6 +836,8 @@
 
     private String formatString;
 
+    private boolean requiresFormatter;
+
     /**
      * Creates a parameterized instance.
      * @param formatString for created messages
@@ -758,6 +856,7 @@
                                 Severity severity) {
       super(null, null, category, severity, null, null);
       this.formatString = formatString != null ? formatString.toString() : "";
+      this.requiresFormatter = formatString.toString().matches(".*%.*");
     }
 
     /**
@@ -794,6 +893,13 @@
       return this.formatString;
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    boolean requiresFormatter() {
+      return this.requiresFormatter;
+    }
+
   }
 
   /** String for accessing backing resource bundle. */
@@ -832,6 +938,9 @@
    */
   protected ClassLoader classLoader;
 
+
+  private Map<Locale,String> formatStrMap = new HashMap<Locale,String>();
+
   /**
    * Obtains the category of this descriptor.  Gauranteed not to be null.
    * @return Category of this message
@@ -903,6 +1012,14 @@
   }
 
   /**
+   * Indicates whether or not this descriptor format string should
+   * be processed by Formatter during string rendering.
+   * @return boolean where true means Formatter should be used; false otherwise
+   * @see java.util.Formatter
+   */
+  abstract boolean requiresFormatter();
+
+  /**
    * Obtains the format string for constructing the string
    * value of this message according to the default
    * locale.
@@ -920,8 +1037,26 @@
    * @return format string
    */
   String getFormatString(Locale locale) {
-    ResourceBundle bundle = getBundle(locale);
-    return bundle.getString(this.key);
+    String fmtStr = formatStrMap.get(locale);
+    if (fmtStr == null) {
+      ResourceBundle bundle = getBundle(locale);
+      fmtStr = bundle.getString(this.key);
+      formatStrMap.put(locale, fmtStr);
+    }
+    return fmtStr;
+  }
+
+  /**
+   * Indicates whether or not formatting should be applied
+   * to the given format string.  Note that a format string
+   * might have literal specifiers (%% or %n for example) that
+   * require formatting but are not replaced by arguments.
+   * @param s candiate for formatting
+   * @return boolean where true indicates that the format
+   *         string requires formatting
+   */
+  protected boolean containsArgumentLiterals(String s) {
+    return s.matches(".*%[n|%].*"); // match Formatter literals
   }
 
   private ResourceBundle getBundle(Locale locale) {
@@ -982,5 +1117,4 @@
     this.mask = mask;
   }
 
-
 }

--
Gitblit v1.10.0