org.snapscript.parse.SourceProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
package org.snapscript.parse;
import java.util.Map;
import org.snapscript.common.LeastRecentlyUsedMap;
public class SourceProcessor {
private final Map cache;
private final int limit;
public SourceProcessor(int limit) {
this.cache = new LeastRecentlyUsedMap();
this.limit = limit;
}
public SourceCode process(String source) {
char[] text = source.toCharArray();
if(text.length == 0) {
throw new SourceException("Source text is empty");
}
SourceCompressor compressor = new SourceCompressor(text);
if(text.length < limit) {
SourceCode code = cache.get(source);
if(code == null) {
code = compressor.compress();
cache.put(source, code); // cache small sources
}
return code;
}
return compressor.compress();
}
}