public Mustache.VariableFetcher createFetcher (Object ctx, String name) { if (ctx instanceof Mustache.CustomContext) return CUSTOM_FETCHER; if (ctx instanceof Map<?,?>) return MAP_FETCHER; // if the name looks like a number, potentially use one of our 'indexing' fetchers char c = name.charAt(0); if (c >= '0' && c <= '9') { if (ctx instanceof List<?>) return LIST_FETCHER; if (ctx instanceof Iterator<?>) return ITER_FETCHER; if (ctx.getClass().isArray()) return arrayHelper(ctx); } return null; }
public Iterator<?> toIterator (final Object value) { if (value instanceof Iterable<?>) { return ((Iterable<?>)value).iterator(); } if (value instanceof Iterator<?>) { return (Iterator<?>)value; } if (value.getClass().isArray()) { final ArrayHelper helper = arrayHelper(value); return new Iterator<Object>() { private int _count = helper.length(value), _idx; @Override public boolean hasNext () { return _idx < _count; } @Override public Object next () { return helper.get(value, _idx++); } @Override public void remove () { throw new UnsupportedOperationException(); } }; } return null; }
public Mustache.VariableFetcher createFetcher (Object ctx, String name) { // support both .name and this.name to fetch members if (name == Template.DOT_NAME || name == Template.THIS_NAME) return THIS_FETCHER; if (ctx instanceof Map<?,?>) return MAP_FETCHER; // if the name looks like a number, potentially use one of our 'indexing' fetchers char c = name.charAt(0); if (c >= '0' && c <= '9') { if (ctx instanceof List<?>) return LIST_FETCHER; if (ctx instanceof Iterator<?>) return ITER_FETCHER; if (ctx.getClass().isArray()) return arrayHelper(ctx); } return null; }
public Iterator<?> toIterator (final Object value) { if (value instanceof Iterable<?>) { return ((Iterable<?>)value).iterator(); } if (value instanceof Iterator<?>) { return (Iterator<?>)value; } if (value.getClass().isArray()) { final ArrayHelper helper = arrayHelper(value); return new Iterator<Object>() { private int _count = helper.length(value), _idx; @Override public boolean hasNext () { return _idx < _count; } @Override public Object next () { return helper.get(value, _idx++); } @Override public void remove () { throw new UnsupportedOperationException(); } }; } return null; }