private static List<ZenCodingFilter> getFilters(ZenCodingNode node, PsiElement context) { List<ZenCodingFilter> result = new ArrayList<ZenCodingFilter>(); while (node instanceof FilterNode) { FilterNode filterNode = (FilterNode) node; String filterSuffix = filterNode.getFilter(); boolean filterFound = false; for (ZenCodingFilter filter : ZenCodingFilter.getInstances()) { if (filter.isMyContext(context) && filter.getSuffix().equals(filterSuffix)) { filterFound = true; result.add(filter); } } assert filterFound; node = filterNode.getNode(); } for (ZenCodingFilter filter : ZenCodingFilter.getInstances()) { if (filter.isMyContext(context) && filter.isAppliedByDefault(context)) { result.add(filter); } } Collections.reverse(result); return result; }
@Nullable private static ZenCodingGenerator findApplicableGenerator( ZenCodingNode node, PsiElement context, boolean wrapping) { ZenCodingGenerator defaultGenerator = null; List<ZenCodingGenerator> generators = ZenCodingGenerator.getInstances(); for (ZenCodingGenerator generator : generators) { if (defaultGenerator == null && generator.isMyContext(context, wrapping) && generator.isAppliedByDefault(context)) { defaultGenerator = generator; } } while (node instanceof FilterNode) { FilterNode filterNode = (FilterNode) node; String suffix = filterNode.getFilter(); for (ZenCodingGenerator generator : generators) { if (generator.isMyContext(context, wrapping)) { if (suffix != null && suffix.equals(generator.getSuffix())) { return generator; } } } node = filterNode.getNode(); } return defaultGenerator; }
@Override protected void putJoinTabalis(Map<Class, String> map) { if (this.joinClass != null && !map.containsKey(this.joinClass)) map.put(joinClass, String.valueOf((char) ('b' + map.size()))); if (this.nodes == null) return; for (FilterNode node : this.nodes) { node.putJoinTabalis(map); } }
@Override protected boolean isCacheUseable(final Function<Class, EntityInfo> entityApplyer) { if (this.joinEntity == null) this.joinEntity = entityApplyer.apply(this.joinClass); if (!this.joinEntity.isCacheFullLoaded()) return false; if (this.nodes == null) return true; for (FilterNode node : this.nodes) { if (!node.isCacheUseable(entityApplyer)) return false; } return true; }
@Override protected <T, E> Predicate<T> createPredicate(final EntityCache<T> cache) { if (column == null && this.nodes == null) return null; final EntityCache<E> joinCache = this.joinEntity.getCache(); final AtomicBoolean more = new AtomicBoolean(); Predicate<E> filter = createJoinPredicate(more); Predicate<T> rs = null; if (filter == null && !more.get()) return rs; if (filter != null) { final Predicate<E> inner = filter; rs = new Predicate<T>() { @Override public boolean test(final T t) { Predicate<E> joinPredicate = null; for (String joinColumn : joinColumns) { final Serializable key = cache.getAttribute(joinColumn).get(t); final Attribute<E, Serializable> joinAttr = joinCache.getAttribute(joinColumn); Predicate<E> p = (E e) -> key.equals(joinAttr.get(e)); joinPredicate = joinPredicate == null ? p : joinPredicate.and(p); } return joinCache.exists(inner.and(joinPredicate)); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(" #-- ON ") .append(joinColumns[0]) .append("=") .append(joinClass == null ? "null" : joinClass.getSimpleName()) .append(".") .append(joinColumns[0]); for (int i = 1; i < joinColumns.length; i++) { sb.append(" AND ") .append(joinColumns[i]) .append("=") .append(joinClass == null ? "null" : joinClass.getSimpleName()) .append(".") .append(joinColumns[i]); } sb.append(" --# ").append(inner.toString()); return sb.toString(); } }; } if (more.get()) { // 存在不同Class的关联表 if (this.nodes != null) { for (FilterNode node : this.nodes) { if (((FilterJoinNode) node).joinClass == this.joinClass) continue; Predicate<T> f = node.createPredicate(cache); if (f == null) continue; final Predicate<T> one = rs; final Predicate<T> two = f; rs = (rs == null) ? f : (or ? new Predicate<T>() { @Override public boolean test(T t) { return one.test(t) || two.test(t); } @Override public String toString() { return "(" + one + " OR " + two + ")"; } } : new Predicate<T>() { @Override public boolean test(T t) { return one.test(t) && two.test(t); } @Override public String toString() { return "(" + one + " AND " + two + ")"; } }); } } } return rs; }