svn commit: r912282 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string: FlexibleStringExpander.java test/FlexibleStringExpanderTests.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r912282 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string: FlexibleStringExpander.java test/FlexibleStringExpanderTests.java

doogie-3
Author: doogie
Date: Sun Feb 21 01:32:39 2010
New Revision: 912282

URL: http://svn.apache.org/viewvc?rev=912282&view=rev
Log:
Add variant of getInstance that allows the caching to be skipped.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/test/FlexibleStringExpanderTests.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java?rev=912282&r1=912281&r2=912282&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java Sun Feb 21 01:32:39 2010
@@ -139,9 +139,32 @@
      * @return A <code>FlexibleStringExpander</code> instance
      */
     public static FlexibleStringExpander getInstance(String expression) {
+        return getInstance(expression, true);
+    }
+
+    /* Returns a <code>FlexibleStringExpander</code> object. <p>A null or
+     * empty argument will return a <code>FlexibleStringExpander</code>
+     * object that represents an empty expression. That object is a shared
+     * singleton, so there is no memory or performance penalty in using it.</p>
+     * <p>If the method is passed a <code>String</code> argument that doesn't
+     * contain an expression, the <code>FlexibleStringExpander</code> object
+     * that is returned does not perform any evaluations on the original
+     * <code>String</code> - any methods that return a <code>String</code>
+     * will return the original <code>String</code>. The object returned by
+     * this method is very compact - taking less memory than the original
+     * <code>String</code>.</p>
+     *
+     * @param expression The original expression
+     * @param useCache whether to store things into a global cache
+     * @return A <code>FlexibleStringExpander</code> instance
+     */
+    public static FlexibleStringExpander getInstance(String expression, boolean useCache) {
         if (UtilValidate.isEmpty(expression)) {
             return nullExpr;
         }
+        if (!useCache) {
+            return parse(expression);
+        }
         // Remove the next three lines to cache all expressions
         if (!expression.contains(openBracket)) {
             return new ConstElem(expression);
@@ -149,18 +172,22 @@
         FlexibleStringExpander fse = exprCache.get(expression);
         if (fse == null) {
             synchronized (exprCache) {
-                FlexibleStringExpander[] strElems = getStrElems(expression);
-                if (strElems.length == 1) {
-                    fse = strElems[0];
-                } else {
-                    fse = new Elements(expression, strElems);
-                }
+                fse = parse(expression);
                 exprCache.put(expression, fse);
             }
         }
         return fse;
     }
 
+    private static FlexibleStringExpander parse(String expression) {
+        FlexibleStringExpander[] strElems = getStrElems(expression);
+        if (strElems.length == 1) {
+            return strElems[0];
+        } else {
+            return new Elements(expression, strElems);
+        }
+    }
+
     /** Parses an expression and returns an array of <code>FlexibleStringExpander</code>
      * instances.
      * @param expression The expression to be parsed

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/test/FlexibleStringExpanderTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/test/FlexibleStringExpanderTests.java?rev=912282&r1=912281&r2=912282&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/test/FlexibleStringExpanderTests.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/test/FlexibleStringExpanderTests.java Sun Feb 21 01:32:39 2010
@@ -46,51 +46,57 @@
         super(name);
     }
 
-    private static void parserTest(String label, String input, String toString) {
-        FlexibleStringExpander fse = FlexibleStringExpander.getInstance(input);
-        assertEquals(label + ":toString", toString, fse.toString());
+    private static void parserTest(String label, String input, boolean checkCache, String toString) {
+        FlexibleStringExpander fse = FlexibleStringExpander.getInstance(input, false);
+        //System.err.println("fse(" + fse + ").class=" + fse.getClass());
+        assertEquals(label + ":toString(no-cache)", toString, fse.toString());
+        fse = FlexibleStringExpander.getInstance(input, true);
+        assertEquals(label + ":toString(cache)", toString, fse.toString());
+        if (checkCache) {
+            assertEquals(label + ":same-cache", fse, FlexibleStringExpander.getInstance(input, true));
+        }
     }
 
     public void testParsing() {
-        parserTest("visible nested replacement", "${'Hello ${var}'}!", "${'Hello ${var}'}!");
-        parserTest("hidden (runtime) nested null callreplacement", "Hello ${${groovy:" + FlexibleStringExpanderTests.class.getName() + ".StaticReturnNull()}}World!", "Hello ${${groovy:" + FlexibleStringExpanderTests.class.getName() + ".StaticReturnNull()}}World!");
-        parserTest("UEL integration(nested): throw Exception", "${${throwException.value}}", "${throwException.value}");
-        parserTest("nested-constant-emptynest-emptynest", "${a${}${}", "a${}${}");
-        parserTest("null", null, "");
-        parserTest("empty", "", "");
-        parserTest("constant-only", "a", "a");
-        parserTest("nested-constant-emptynest-emptynest", "${a${}${}", "a${}${}");
-        parserTest("bsh", "${bsh:}", "");
-        parserTest("groovy", "${groovy:}", "");
-
-        parserTest("escaped", "\\${}", "\\${}");
-        parserTest("constant-escaped", "a\\${}", "a\\${}");
-        parserTest("escaped-bsd", "\\${bsh:}", "\\${bsh:}");
-        parserTest("escaped-groovy", "\\${groovy:}", "\\${groovy:}");
-
-        parserTest("missing-}", "${", "${");
-        parserTest("nested-constant-missing-}", "${a${}", "a${}");
-        parserTest("nested-constant-nested-nested-missing-}", "${a${${}", "a${${}");
-        parserTest("escaped-missing-}", "\\${", "\\${");
-        parserTest("constant-escaped-missing-}", "a\\${", "a\\${");
-
-        parserTest("currency", "${?currency(", "${?currency(");
-        parserTest("currency", "${?currency()", "${?currency()");
-        parserTest("currency", "${price?currency(", "${price?currency(");
-        parserTest("currency", "${price?currency()", "${price?currency()");
-        parserTest("currency", "${?currency(usd", "${?currency(usd");
-        parserTest("currency", "${?currency(usd)", "${?currency(usd)");
-        parserTest("currency", "${price?currency(usd", "${price?currency(usd");
-        parserTest("currency", "${price?currency(usd)", "${price?currency(usd)");
-        parserTest("currency", "${?currency(}", "?currency(");
-        parserTest("currency", "${?currency()}", "?currency()");
-        parserTest("currency", "${?currency(usd}", "?currency(usd");
-        parserTest("currency", "${?currency(usd)}", "?currency(usd)");
-        parserTest("currency", "${price?currency(}", "price?currency(");
-        parserTest("currency", "${price?currency()}", "price?currency()");
-        parserTest("currency", "${price?currency(usd}", "price?currency(usd");
-        parserTest("currency", "${price?currency(usd)}", "price?currency(usd)");
-        parserTest("currency", "a${price?currency(usd)}b", "a${price?currency(usd)}b");
+        parserTest("visible nested replacement", "${'Hello ${var}'}!", true, "${'Hello ${var}'}!");
+        parserTest("hidden (runtime) nested null callreplacement", "Hello ${${groovy:" + FlexibleStringExpanderTests.class.getName() + ".StaticReturnNull()}}World!", true, "Hello ${${groovy:" + FlexibleStringExpanderTests.class.getName() + ".StaticReturnNull()}}World!");
+        parserTest("UEL integration(nested): throw Exception", "${${throwException.value}}", true, "${throwException.value}");
+        parserTest("nested-constant-emptynest-emptynest", "${a${}${}", true, "a${}${}");
+        parserTest("null", null, true, "");
+        parserTest("empty", "", true, "");
+        parserTest("constant-only", "a", false, "a");
+        parserTest("nested-constant-emptynest-emptynest", "${a${}${}", true, "a${}${}");
+        parserTest("bsh", "${bsh:}", true, "");
+        parserTest("groovy", "${groovy:}", true, "");
+
+        parserTest("escaped", "\\${}", true, "\\${}");
+        parserTest("constant-escaped", "a\\${}", true, "a\\${}");
+        parserTest("escaped-bsd", "\\${bsh:}", true, "\\${bsh:}");
+        parserTest("escaped-groovy", "\\${groovy:}", true, "\\${groovy:}");
+
+        parserTest("missing-}", "${", true, "${");
+        parserTest("nested-constant-missing-}", "${a${}", true, "a${}");
+        parserTest("nested-constant-nested-nested-missing-}", "${a${${}", true, "a${${}");
+        parserTest("escaped-missing-}", "\\${", true, "\\${");
+        parserTest("constant-escaped-missing-}", "a\\${", true, "a\\${");
+
+        parserTest("currency", "${?currency(", true, "${?currency(");
+        parserTest("currency", "${?currency()", true, "${?currency()");
+        parserTest("currency", "${price?currency(", true, "${price?currency(");
+        parserTest("currency", "${price?currency()", true, "${price?currency()");
+        parserTest("currency", "${?currency(usd", true, "${?currency(usd");
+        parserTest("currency", "${?currency(usd)", true, "${?currency(usd)");
+        parserTest("currency", "${price?currency(usd", true, "${price?currency(usd");
+        parserTest("currency", "${price?currency(usd)", true, "${price?currency(usd)");
+        parserTest("currency", "${?currency(}", true, "?currency(");
+        parserTest("currency", "${?currency()}", true, "?currency()");
+        parserTest("currency", "${?currency(usd}", true, "?currency(usd");
+        parserTest("currency", "${?currency(usd)}", true, "?currency(usd)");
+        parserTest("currency", "${price?currency(}", true, "price?currency(");
+        parserTest("currency", "${price?currency()}", true, "price?currency()");
+        parserTest("currency", "${price?currency(usd}", true, "price?currency(usd");
+        parserTest("currency", "${price?currency(usd)}", true, "price?currency(usd)");
+        parserTest("currency", "a${price?currency(usd)}b", true, "a${price?currency(usd)}b");
     }
 
     private static void fseTest(String label, String input, Map<String, Object> context, Object compare, boolean isEmpty) {
@@ -168,7 +174,7 @@
             assertEquals("static expandString(input, null):" + label, input, FlexibleStringExpander.expandString(input, null, locale));
         }
         if (!fse.isEmpty()) {
-            fse = FlexibleStringExpander.getInstance(input);
+            fse = FlexibleStringExpander.getInstance(input, false);
             doFseTest(label, input, fse, context, timeZone, locale, compare, isEmpty);
         }
     }