Created
September 20, 2012 22:34
-
-
Save spullara/3758766 to your computer and use it in GitHub Desktop.
Scope object for Tina
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.mustachejava.util; | |
import java.util.HashMap; | |
import java.util.concurrent.ExecutionException; | |
import com.google.common.cache.CacheBuilder; | |
import com.google.common.cache.CacheLoader; | |
import com.google.common.cache.LoadingCache; | |
import com.github.mustachejava.Binding; | |
import com.github.mustachejava.MustacheException; | |
import com.github.mustachejava.reflect.SimpleObjectHandler; | |
public class Scope extends HashMap<String, Object> { | |
private static final SimpleObjectHandler oh = new SimpleObjectHandler(); | |
private static final LoadingCache<String, Binding> CACHE = CacheBuilder.newBuilder().build(new CacheLoader<String, Binding>() { | |
@Override | |
public Binding load(String key) throws Exception { | |
return oh.createBinding(key, null, null); | |
} | |
}); | |
private Object[] wrapped; | |
public Scope(Object wrapped) { | |
this.wrapped = new Object[] { wrapped }; | |
} | |
public Scope(Object... wrapped) { | |
this.wrapped = wrapped; | |
} | |
public Object get(String key) { | |
try { | |
Binding binding = CACHE.get(key); | |
Object o = binding.get(wrapped); | |
if (o == null) { | |
o = super.get(key); | |
} | |
return o; | |
} catch (ExecutionException e) { | |
throw new MustacheException("Failed to get key", e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.mustachejava.util; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertEquals; | |
import static junit.framework.Assert.assertTrue; | |
public class ScopeTest { | |
@Test | |
public void testWithMap() { | |
Map map = new HashMap(); | |
map.put("sam", "pullara"); | |
assertEquals("pullara", new Scope(map).get("sam")); | |
} | |
@Test | |
public void testWithField() { | |
Object o = new Object() { | |
String sam = "pullara"; | |
}; | |
assertEquals("pullara", new Scope(o).get("sam")); | |
} | |
@Test | |
public void testWithMethod() { | |
Object o = new Object() { | |
String sam() { | |
return "pullara"; | |
} | |
}; | |
assertEquals("pullara", new Scope(o).get("sam")); | |
} | |
@Test | |
public void testWithProperty() { | |
Object o = new Object() { | |
String getSam() { | |
return "pullara"; | |
} | |
boolean isSet() { | |
return true; | |
} | |
}; | |
assertEquals("pullara", new Scope(o).get("sam")); | |
assertTrue((Boolean) new Scope(o).get("isSet")); | |
} | |
@Test | |
public void testContexts() { | |
Object o1 = new Object() { | |
String sam = "pullara"; | |
String lucy = "pullara"; | |
}; | |
Object o2 = new Object() { | |
String sam = "Pullara"; | |
}; | |
assertEquals("Pullara", new Scope(o1, o2).get("sam")); | |
assertEquals("pullara", new Scope(o2, o1).get("sam")); | |
assertEquals("pullara", new Scope(o1, o1).get("lucy")); | |
assertEquals("pullara", new Scope(o2, o1).get("lucy")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment