org.snapscript.parse.SourceProcessor Maven / Gradle / Ivy
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();
}
}