Skip to content

Commit

Permalink
Misc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbosecke committed Jan 25, 2014
1 parent 8730c24 commit 3eaf8e8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 29 deletions.
18 changes: 8 additions & 10 deletions src/main/java/com/mitchellbosecke/pebble/PebbleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import com.mitchellbosecke.pebble.lexer.LexerImpl;
import com.mitchellbosecke.pebble.lexer.TokenStream;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.loader.PebbleDefaultLoader;
import com.mitchellbosecke.pebble.loader.DefaultLoader;
import com.mitchellbosecke.pebble.node.NodeRoot;
import com.mitchellbosecke.pebble.operator.BinaryOperator;
import com.mitchellbosecke.pebble.operator.UnaryOperator;
Expand Down Expand Up @@ -77,7 +77,7 @@ public class PebbleEngine {
/**
* Template cache
*/
private TemplateLoadingCache<String, PebbleTemplate> loadingTemplateCache;
private TemplateLoadingCache loadingTemplateCache;

/*
* Extensions
Expand All @@ -103,7 +103,7 @@ public class PebbleEngine {
private final Semaphore compilationMutex = new Semaphore(1);

public PebbleEngine() {
this(new PebbleDefaultLoader());
this(new DefaultLoader());
}

/**
Expand Down Expand Up @@ -170,19 +170,17 @@ public PebbleTemplate call() throws PebbleException, InterruptedException {
TokenStream tokenStream = getLexer().tokenize(templateSource, templateName);
NodeRoot root = getParser().parse(tokenStream);
String javaSource = getCompiler().compile(root).getSource();

// we are now done with the non-thread-safe objects, so release
// the compilation mutex
compilationMutex.release();

PebbleTemplate parent = null;
if(root.hasParent()){
if (root.hasParent()) {
parent = self.compile(root.getParentFileName());
}
instance = getCompiler().instantiateTemplate(javaSource, className, parent);



// init blocks and macros
instance.initBlocks();
instance.initMacros();
Expand Down Expand Up @@ -387,11 +385,11 @@ public Class<?> getTemplateParentClass() {
return templateParentClass;
}

public TemplateLoadingCache<String, PebbleTemplate> getTemplateCache() {
public TemplateLoadingCache getTemplateCache() {
return loadingTemplateCache;
}

public void setTemplateCache(TemplateLoadingCache<String, PebbleTemplate> cache) {
public void setTemplateCache(TemplateLoadingCache cache) {
this.loadingTemplateCache = cache;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

public class DefaultTemplateLoadingCache implements TemplateLoadingCache<String, PebbleTemplate> {
public class DefaultTemplateLoadingCache implements TemplateLoadingCache {

private final ConcurrentMap<String, Future<PebbleTemplate>> cache = new ConcurrentHashMap<>();

Expand All @@ -31,8 +31,8 @@ public PebbleTemplate get(String key, Callable<PebbleTemplate> loadingFunction)
try {
return future.get();
} catch (ExecutionException | InterruptedException e) {
if(e.getCause() instanceof PebbleException){
throw (PebbleException)e.getCause();
if (e.getCause() instanceof PebbleException) {
throw (PebbleException) e.getCause();
}
throw new RuntimeException("An error occurred while retrieving from cache");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.util.concurrent.Callable;

import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

public interface TemplateLoadingCache<K,V> {
public interface TemplateLoadingCache {

public V get(K key, Callable<V> loadingFunction) throws PebbleException;
public PebbleTemplate get(String key, Callable<PebbleTemplate> loadingFunction) throws PebbleException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public String getName() {
return "even";
}

public Boolean apply(Object input, List<Object> args) {
public boolean apply(Object input, List<Object> args) {

Integer obj = (Integer) input;
return (obj % 2 == 0);
Expand All @@ -368,7 +368,7 @@ public String getName() {
return "odd";
}

public Boolean apply(Object input, List<Object> args) {
public boolean apply(Object input, List<Object> args) {

return evenTest.apply(input, args) == false;
}
Expand All @@ -379,7 +379,7 @@ public String getName() {
return "null";
}

public Boolean apply(Object input, List<Object> args) {
public boolean apply(Object input, List<Object> args) {

return input == null;
}
Expand All @@ -390,7 +390,7 @@ public String getName() {
return "empty";
}

public Boolean apply(Object input, List<Object> args) {
public boolean apply(Object input, List<Object> args) {
boolean isEmpty = input == null;

if (!isEmpty && input instanceof String) {
Expand All @@ -414,7 +414,7 @@ public String getName() {
return "iterable";
}

public Boolean apply(Object input, List<Object> args) {
public boolean apply(Object input, List<Object> args) {

return input instanceof Iterable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface Test {

public String getName();

public Boolean apply(Object input, List<Object> args);
public boolean apply(Object input, List<Object> args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

import com.mitchellbosecke.pebble.error.LoaderException;

public class PebbleDefaultLoader implements Loader {
public class DefaultLoader implements Loader {

private static final Logger logger = LoggerFactory.getLogger(PebbleDefaultLoader.class);
private static final Logger logger = LoggerFactory.getLogger(DefaultLoader.class);

private String prefix;

Expand Down Expand Up @@ -59,7 +59,7 @@ public Reader getReader(String templateName) throws LoaderException {
is = ccl.getResourceAsStream(location);

// try ResourceLoader's class loader
ClassLoader rcl = PebbleDefaultLoader.class.getClassLoader();
ClassLoader rcl = DefaultLoader.class.getClassLoader();
if (is == null) {
is = rcl.getResourceAsStream(location);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/mitchellbosecke/pebble/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.mitchellbosecke.pebble.compiler.Compiler;
import com.mitchellbosecke.pebble.lexer.Lexer;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.loader.PebbleDefaultLoader;
import com.mitchellbosecke.pebble.loader.DefaultLoader;
import com.mitchellbosecke.pebble.parser.Parser;

public abstract class AbstractTest {
Expand All @@ -22,7 +22,7 @@ public abstract class AbstractTest {

public AbstractTest() {

Loader templateLoader = new PebbleDefaultLoader();
Loader templateLoader = new DefaultLoader();
templateLoader.setPrefix("templates");

// main testing engine uses all default settings
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/mitchellbosecke/pebble/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.loader.PebbleDefaultLoader;
import com.mitchellbosecke.pebble.loader.DefaultLoader;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

public class CacheTest extends AbstractTest {
Expand All @@ -32,7 +32,7 @@ public class CacheTest extends AbstractTest {
*/
@Test
public void templatesWithSameNameOverridingCache() throws PebbleException, IOException {
Loader loader = new PebbleDefaultLoader();
Loader loader = new DefaultLoader();
PebbleEngine engine = new PebbleEngine(loader);

PebbleTemplate cache1 = engine.compile("templates/cache/cache1/template.cache.peb");
Expand Down Expand Up @@ -60,7 +60,7 @@ public void templatesWithSameNameOverridingCache() throws PebbleException, IOExc
*/
@Test
public void ensureChildTemplateNotCached() throws PebbleException, IOException {
Loader loader = new PebbleDefaultLoader();
Loader loader = new DefaultLoader();
PebbleEngine engine = new PebbleEngine(loader);

PebbleTemplate cache1 = engine.compile("templates/cache/template.cacheChild.peb");
Expand Down

0 comments on commit 3eaf8e8

Please sign in to comment.