|
3 | 3 | import static org.ugp.serialx.Utils.contains;
|
4 | 4 | import static org.ugp.serialx.Utils.indexOfNotInObj;
|
5 | 5 |
|
| 6 | +import java.util.Map; |
| 7 | + |
6 | 8 | import org.ugp.serialx.Registry;
|
7 | 9 |
|
8 | 10 | /**
|
@@ -41,14 +43,24 @@ public class StringConverter implements DataConverter
|
41 | 43 | *
|
42 | 44 | * @since 1.2.0 (moved to {@link StringConverter} since 1.3.0)
|
43 | 45 | */
|
44 |
| - public static boolean serializeStringNormally = true; |
| 46 | + public static boolean serializeStringNormally = true; //TODO |
| 47 | + |
| 48 | + protected Map<String, String> parsingCache; |
45 | 49 |
|
46 | 50 | @Override
|
47 | 51 | public String parse(ParserRegistry myHomeRegistry, String str, Object... args)
|
48 | 52 | {
|
| 53 | + String result; |
49 | 54 | int len;
|
50 | 55 | if ((len = str.length()) > 1 && str.charAt(0) == '\"' && str.charAt(--len) == '\"' && indexOfNotInObj(str, ' ') == -1)
|
51 | 56 | {
|
| 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 | + } |
52 | 64 | return str.substring(1, len);
|
53 | 65 | }
|
54 | 66 | return CONTINUE;
|
@@ -90,6 +102,31 @@ public CharSequence getDescription(ParserRegistry myHomeRegistry, Object obj, Ob
|
90 | 102 | return "";
|
91 | 103 | }
|
92 | 104 |
|
| 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 | + |
93 | 130 | /**
|
94 | 131 | * @param obj | Object to stringify directly.
|
95 | 132 | *
|
|
0 commit comments