Each parseEach(final Arguments arguments, final String input, final boolean preprocess) { final String trimmedInput = input.trim(); final String preprocessedInput = (preprocess ? preprocess(arguments, trimmedInput) : trimmedInput); final Each cachedEach = CACHE.getEachFromCache(arguments.getConfiguration(), preprocessedInput); if (cachedEach != null) { return cachedEach; } final Each each = Each.parse(preprocessedInput); if (each == null) { throw new TemplateProcessingException("Could not parse as each: \"" + input + "\""); } CACHE.putEachIntoCache(arguments.getConfiguration(), preprocessedInput, each); return each; }
/** * 用回调的方式,遍历一个对象,可以支持遍历 * * <ul> * <li>数组 * <li>集合 * <li>Map * <li>单一元素 * </ul> * * @param obj 对象 * @param loopMap 是否循环 Map,如果循环 Map 则主要看 callback 的 T,如果是 Map.Entry 则循环 Entry 否循环 value。如果本值为 * false, 则将 Map 当作一个完整的对象来看待 * @param callback 回调 */ @SuppressWarnings({"rawtypes", "unchecked"}) public static <T> void each(Object obj, boolean loopMap, Each<T> callback) { if (null == obj || null == callback) return; try { // 循环开始 if (callback instanceof Loop) if (!((Loop) callback).begin()) return; // 进行循环 Class<T> eType = Mirror.getTypeParam(callback.getClass(), 0); if (obj.getClass().isArray()) { int len = Array.getLength(obj); for (int i = 0; i < len; i++) try { callback.invoke(i, (T) Array.get(obj, i), len); } catch (ContinueLoop e) { } catch (ExitLoop e) { break; } } else if (obj instanceof Collection) { int len = ((Collection) obj).size(); int i = 0; for (Iterator<T> it = ((Collection) obj).iterator(); it.hasNext(); ) try { callback.invoke(i++, it.next(), len); } catch (ContinueLoop e) { } catch (ExitLoop e) { break; } } else if (loopMap && obj instanceof Map) { Map map = (Map) obj; int len = map.size(); int i = 0; if (null != eType && eType != Object.class && eType.isAssignableFrom(Entry.class)) { for (Object v : map.entrySet()) try { callback.invoke(i++, (T) v, len); } catch (ContinueLoop e) { } catch (ExitLoop e) { break; } } else { for (Object v : map.entrySet()) try { callback.invoke(i++, (T) ((Entry) v).getValue(), len); } catch (ContinueLoop e) { } catch (ExitLoop e) { break; } } } else if (obj instanceof Iterator<?>) { Iterator<?> it = (Iterator<?>) obj; int i = 0; while (it.hasNext()) { try { callback.invoke(i++, (T) it.next(), -1); } catch (ContinueLoop e) { } catch (ExitLoop e) { break; } } } else try { callback.invoke(0, (T) obj, 1); } catch (ContinueLoop e) { } catch (ExitLoop e) { } // 循环结束 if (callback instanceof Loop) ((Loop) callback).end(); } catch (LoopException e) { throw Lang.wrapThrow(e.getCause()); } }
/** * 用回调的方式,遍历一个对象,可以支持遍历 * * <ul> * <li>数组 * <li>集合 * <li>Map * <li>单一元素 * </ul> * * @param elems 对象 * @param callback 回调 */ @SuppressWarnings({"unchecked", "rawtypes"}) public static <T> void each(Object elems, Each<T> callback) { if (null == elems || null == callback) return; try { Class<T> eType = getTypeParam(callback.getClass(), 0); if (elems.getClass().isArray()) { int len = Array.getLength(elems); for (int i = 0; i < len; i++) try { callback.loop(i, (T) Array.get(elems, i), len); } catch (ExitLoop e) { break; } } else if (elems instanceof Collection) { int len = ((Collection) elems).size(); int i = 0; for (Iterator<T> it = ((Collection) elems).iterator(); it.hasNext(); ) try { callback.loop(i++, it.next(), len); } catch (ExitLoop e) { break; } } else if (elems instanceof Map) { Map map = (Map) elems; int len = map.size(); int i = 0; if (null != eType && eType != Object.class && eType.isAssignableFrom(Map.Entry.class)) { for (Object v : map.entrySet()) try { callback.loop(i++, (T) v, len); } catch (ExitLoop e) { break; } } else { for (Object v : map.entrySet()) try { callback.loop(i++, (T) ((Map.Entry) v).getValue(), len); } catch (ExitLoop e) { break; } } } else if (elems instanceof Iterator<?>) { Iterator<?> it = (Iterator<?>) elems; int i = 0; while (it.hasNext()) { try { callback.loop(i++, (T) it.next(), -1); } catch (ExitLoop e) { break; } } } else try { callback.loop(0, (T) elems, 1); } catch (ExitLoop e) { } } catch (LoopException e) { throw LE.wrapThrow(e.getCause()); } }