Made createFetcherCache abstract.

GWT doesn't provide Collections.synchronizedMap so we can't default to that.
Because we don't want someone to extend BasicCollector on a multi-threaded
platform and get an unsynchronized HashMap, which would cause things to fail,
we instead make creatFetcherCache abstract and document that if you extend
BasicCollector, you need to return a synchronized map (if the platform you're
targeting is multithreaded).
This commit is contained in:
Michael Bayne
2012-10-03 09:51:26 -07:00
parent 4926b326b9
commit 31190d3cb5
2 changed files with 11 additions and 5 deletions
@@ -4,7 +4,9 @@
package com.samskivert.mustache;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A collector used when running in GWT.
@@ -22,5 +24,10 @@ public class DefaultCollector extends BasicCollector
return null;
}
@Override
public <K,V> Map<K,V> createFetcherCache () {
return new HashMap<K,V>();
}
// TODO: override createFetcher and do some magic for JavaScript/JSON objects
}
@@ -4,14 +4,13 @@
package com.samskivert.mustache;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A collector that does not use reflection and can be used in GWT.
*/
public class BasicCollector implements Mustache.Collector
public abstract class BasicCollector implements Mustache.Collector
{
public Iterator<?> toIterator (final Object value) {
if (value instanceof Iterable<?>) {
@@ -36,9 +35,9 @@ public class BasicCollector implements Mustache.Collector
return null;
}
public <K,V> Map<K,V> createFetcherCache () {
return Collections.synchronizedMap(new HashMap<K,V>());
}
/** This should return a thread-safe map, either {@link Collections#synchronizedMap} called on
* a standard {@link Map} implementation or something like {@code ConcurrentHashMap}. */
public abstract <K,V> Map<K,V> createFetcherCache ();
protected static final Mustache.VariableFetcher MAP_FETCHER = new Mustache.VariableFetcher() {
public Object get (Object ctx, String name) throws Exception {