Author: nbelaevski
Date: 2008-03-17 15:50:27 -0400 (Mon, 17 Mar 2008)
New Revision: 6882
Modified:
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java
Log:
SuggestionBox: optimization - tokens cache added
Modified:
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java
===================================================================
---
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java 2008-03-17
19:50:23 UTC (rev 6881)
+++
trunk/ui/suggestionbox/src/main/java/org/richfaces/renderkit/html/SuggestionBoxRenderer.java 2008-03-17
19:50:27 UTC (rev 6882)
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -53,11 +54,16 @@
import org.richfaces.component.UISuggestionBox;
import org.richfaces.skin.Skin;
import org.richfaces.skin.SkinFactory;
+import org.richfaces.util.ReferenceMap;
/**
* Renderer for SuggestionBox component.
*/
public class SuggestionBoxRenderer extends AjaxComponentRendererBase {
+
+ private static final Map<String, Pattern> tokensCache =
+ Collections.synchronizedMap(new ReferenceMap<String, Pattern>());
+
/**
* Component options.
*/
@@ -120,17 +126,26 @@
//TODO nick - cache ?
String tokens = (String) component.getAttributes().get("tokens");
if (tokens != null && tokens.length() != 0) {
- StringBuilder patternSource = new StringBuilder();
- char[] array = tokens.toCharArray();
- int l = array.length;
- for (int i = 0; i < l; i++) {
- if (i != 0) {
- patternSource.append('|');
- }
- patternSource.append(Pattern.quote(String.valueOf(array[i])));
- }
-
- return Pattern.compile(patternSource.toString());
+ Pattern pattern;
+ synchronized (tokensCache) {
+ pattern = tokensCache.get(tokens);
+ if (pattern == null) {
+ StringBuilder patternSource = new StringBuilder();
+ char[] array = tokens.toCharArray();
+ int l = array.length;
+ for (int i = 0; i < l; i++) {
+ if (i != 0) {
+ patternSource.append('|');
+ }
+ patternSource.append(Pattern.quote(String.valueOf(array[i])));
+ }
+
+ pattern = Pattern.compile(patternSource.toString());
+ tokensCache.put(tokens, pattern);
+ }
+ }
+
+ return pattern;
} else {
return null;
}