From 8fb33880772030fb5e3d9a89cb9d7be51101bd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Wed, 28 Oct 2015 14:51:19 +0800 Subject: [PATCH] --- src/com/wentch/redkale/source/FilterNode.java | 160 ++++++++++++++---- 1 file changed, 127 insertions(+), 33 deletions(-) diff --git a/src/com/wentch/redkale/source/FilterNode.java b/src/com/wentch/redkale/source/FilterNode.java index d06d9c8e4..63fdfec72 100644 --- a/src/com/wentch/redkale/source/FilterNode.java +++ b/src/com/wentch/redkale/source/FilterNode.java @@ -18,6 +18,17 @@ import java.util.function.*; */ public class FilterNode { + private static final Map class2 = new HashMap<>(); + + static { + class2.put(Integer.class, int.class); + class2.put(Long.class, long.class); + class2.put(Short.class, short.class); + class2.put(Float.class, float.class); + class2.put(Byte.class, byte.class); + class2.put(Double.class, double.class); + } + protected boolean signand = true; protected String tabalis; @@ -222,7 +233,7 @@ public class FilterNode { protected Predicate createFilterPredicate(final EntityInfo info, FilterBean bean) { if (info == null || (column == null && this.nodes == null)) return null; final Serializable val = getValue(bean); - Predicate filter = (val == null || column == null) ? null : createFilterPredicate(info.getAttribute(column), val); + Predicate filter = createFilterPredicate(column == null ? null : info.getAttribute(column), val); if (this.nodes == null) return filter; for (FilterNode node : this.nodes) { Predicate f = node.createFilterPredicate(info, bean); @@ -257,22 +268,130 @@ public class FilterNode { } protected final Predicate createFilterPredicate(final Attribute attr, Serializable val0) { + if (val0 == null) { + if (express == ISNULL) return new Predicate() { + + @Override + public boolean test(T t) { + return attr.get(t) == null; + } + + @Override + public String toString() { + return attr.field() + " = null"; + } + }; + if (express == ISNOTNULL) return new Predicate() { + + @Override + public boolean test(T t) { + return attr.get(t) != null; + } + + @Override + public String toString() { + return attr.field() + " != null"; + } + }; + return null; + } if (attr == null) return null; + final Class atype = attr.type(); - if (val0 != null && atype != val0.getClass() && val0 instanceof Number) { - if (atype == short.class || atype == Short.class) { - val0 = ((Number) val0).shortValue(); + final Class valtype = val0.getClass(); + if (atype != valtype && val0 instanceof Number) { + if (atype == int.class || atype == Integer.class) { + val0 = ((Number) val0).intValue(); } else if (atype == long.class || atype == Long.class) { val0 = ((Number) val0).longValue(); - } else if (atype == byte.class || atype == Byte.class) { - val0 = ((Number) val0).byteValue(); - } else if (atype == int.class || atype == Integer.class) { - val0 = ((Number) val0).intValue(); + } else if (atype == short.class || atype == Short.class) { + val0 = ((Number) val0).shortValue(); } else if (atype == float.class || atype == Float.class) { val0 = ((Number) val0).floatValue(); + } else if (atype == byte.class || atype == Byte.class) { + val0 = ((Number) val0).byteValue(); } else if (atype == double.class || atype == Double.class) { val0 = ((Number) val0).doubleValue(); } + } else if (valtype.isArray()) { + final int len = Array.getLength(val0); + if (len == 0) return null; + final Class compType = valtype.getComponentType(); + if (atype != compType) { + if (!compType.isPrimitive() && Number.class.isAssignableFrom(compType)) throw new RuntimeException("param(" + val0 + ") type not match " + atype + " for column " + column); + if (atype == int.class || atype == Integer.class) { + int[] vs = new int[len]; + for (int i = 0; i < len; i++) { + vs[i] = ((Number) Array.get(val0, i)).intValue(); + } + val0 = vs; + } else if (atype == long.class || atype == Long.class) { + long[] vs = new long[len]; + for (int i = 0; i < len; i++) { + vs[i] = ((Number) Array.get(val0, i)).longValue(); + } + val0 = vs; + } else if (atype == short.class || atype == Short.class) { + short[] vs = new short[len]; + for (int i = 0; i < len; i++) { + vs[i] = ((Number) Array.get(val0, i)).shortValue(); + } + val0 = vs; + } else if (atype == float.class || atype == Float.class) { + float[] vs = new float[len]; + for (int i = 0; i < len; i++) { + vs[i] = ((Number) Array.get(val0, i)).floatValue(); + } + val0 = vs; + } else if (atype == byte.class || atype == Byte.class) { + byte[] vs = new byte[len]; + for (int i = 0; i < len; i++) { + vs[i] = ((Number) Array.get(val0, i)).byteValue(); + } + val0 = vs; + } else if (atype == double.class || atype == Double.class) { + double[] vs = new double[len]; + for (int i = 0; i < len; i++) { + vs[i] = ((Number) Array.get(val0, i)).doubleValue(); + } + val0 = vs; + } + } + } else if (val0 instanceof Collection) { + final Collection collection = (Collection) val0; + if (collection.isEmpty()) return null; + Iterator it = collection.iterator(); + it.hasNext(); + Class fs = it.next().getClass(); + if (Number.class.isAssignableFrom(fs) && atype != fs && atype != class2.get(fs)) { //需要转换 + ArrayList list = new ArrayList(collection.size()); + if (atype == int.class || atype == Integer.class) { + for (Number num : (Collection) collection) { + list.add(num.intValue()); + } + } else if (atype == long.class || atype == Long.class) { + for (Number num : (Collection) collection) { + list.add(num.longValue()); + } + } else if (atype == short.class || atype == Short.class) { + for (Number num : (Collection) collection) { + list.add(num.shortValue()); + } + } else if (atype == float.class || atype == Float.class) { + for (Number num : (Collection) collection) { + list.add(num.floatValue()); + } + } else if (atype == byte.class || atype == Byte.class) { + for (Number num : (Collection) collection) { + list.add(num.byteValue()); + } + } else if (atype == double.class || atype == Double.class) { + for (Number num : (Collection) collection) { + list.add(num.doubleValue()); + } + } + val0 = list; + } } final Serializable val = val0; switch (express) { @@ -348,30 +467,7 @@ public class FilterNode { return attr.field() + ' ' + express.value() + ' ' + val; } }; - case ISNULL: return new Predicate() { - @Override - public boolean test(T t) { - return attr.get(t) == null; - } - - @Override - public String toString() { - return attr.field() + " = null"; - } - }; - case ISNOTNULL: return new Predicate() { - - @Override - public boolean test(T t) { - return attr.get(t) != null; - } - - @Override - public String toString() { - return attr.field() + " != null"; - } - }; case OPAND: return new Predicate() { @Override @@ -477,7 +573,6 @@ public class FilterNode { Predicate filter; if (val instanceof Collection) { Collection array = (Collection) val; - if (array.isEmpty()) return null; filter = new Predicate() { @Override @@ -492,7 +587,6 @@ public class FilterNode { } }; } else { - if (Array.getLength(val) < 1) return null; Class type = val.getClass(); if (type == int[].class) { filter = new Predicate() {