Skip to content

Commit ea69c1a

Browse files
add ability to cache for StringConv
1 parent 0552ca3 commit ea69c1a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

SerialX-core/src/main/java/org/ugp/serialx/converters/NumberConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public String format(Number num)
176176
*
177177
* @since 1.3.7
178178
*/
179-
public static Number numberOf(CharSequence str, char ch0, int end, int base, int type) //TODO
179+
public static Number numberOf(CharSequence str, char ch0, int end, int base, int type)
180180
{
181181
int start = 0;
182182

SerialX-core/src/main/java/org/ugp/serialx/converters/StringConverter.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.ugp.serialx.Utils.contains;
44
import static org.ugp.serialx.Utils.indexOfNotInObj;
55

6+
import java.util.Map;
7+
68
import org.ugp.serialx.Registry;
79

810
/**
@@ -41,14 +43,24 @@ public class StringConverter implements DataConverter
4143
*
4244
* @since 1.2.0 (moved to {@link StringConverter} since 1.3.0)
4345
*/
44-
public static boolean serializeStringNormally = true;
46+
public static boolean serializeStringNormally = true; //TODO
47+
48+
protected Map<String, String> parsingCache;
4549

4650
@Override
4751
public String parse(ParserRegistry myHomeRegistry, String str, Object... args)
4852
{
53+
String result;
4954
int len;
5055
if ((len = str.length()) > 1 && str.charAt(0) == '\"' && str.charAt(--len) == '\"' && indexOfNotInObj(str, ' ') == -1)
5156
{
57+
if (parsingCache != null)
58+
{
59+
if ((result = parsingCache.get(str)) != null)
60+
return result;
61+
parsingCache.put(str, result = str.substring(1, len));
62+
return result;
63+
}
5264
return str.substring(1, len);
5365
}
5466
return CONTINUE;
@@ -90,6 +102,31 @@ public CharSequence getDescription(ParserRegistry myHomeRegistry, Object obj, Ob
90102
return "";
91103
}
92104

105+
/**
106+
* @param cache | Instance of {@link Map}, preferably {@link HashMap}, that will be used as cache for parsed strings where keys will be strings with " and values will be string without them. This cache will then be prioritized over creating a new instance of string during parsing, similarly to Java's string pool. Doing this can save you some memory with minimal performance overhead!<br>
107+
* Setting this to null will disable the parsing result caching by this {@link StringConverter} as it is by default.<br>
108+
* Recommended: Enable this when parsing a lot of strings that are the same, otherwise this will not have a big impact.<br>
109+
* Rule of thumb, is that this cache should be modified only by this converter however adding some pre-cached entries is possible but should be performed with caution!
110+
*
111+
* @since 1.3.7
112+
*/
113+
public void setParsingCache(Map<String, String> cache)
114+
{
115+
parsingCache = cache;
116+
}
117+
118+
/**
119+
* @return Instance of {@link Map}, preferably {@link HashMap}, that will be used as cache for parsed strings where keys will be strings with " and values will be string without them. This cache will then be prioritized over creating a new instance of string during parsing, similarly to Java's string pool.<br>
120+
* Null will be returned if caching is disabled, which is by default...<br>
121+
* Note: Rule of thumb, is that this cache should be modified only by this converter however adding some pre-cached entries is possible but should be performed with caution!
122+
*
123+
* @since 1.3.7
124+
*/
125+
public Map<String, String> getParsingCache()
126+
{
127+
return parsingCache;
128+
}
129+
93130
/**
94131
* @param obj | Object to stringify directly.
95132
*

0 commit comments

Comments
 (0)