This commit is contained in:
@@ -18,6 +18,17 @@ import java.util.function.*;
|
|||||||
*/
|
*/
|
||||||
public class FilterNode {
|
public class FilterNode {
|
||||||
|
|
||||||
|
private static final Map<Class, Class> 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 boolean signand = true;
|
||||||
|
|
||||||
protected String tabalis;
|
protected String tabalis;
|
||||||
@@ -222,7 +233,7 @@ public class FilterNode {
|
|||||||
protected <T> Predicate<T> createFilterPredicate(final EntityInfo<T> info, FilterBean bean) {
|
protected <T> Predicate<T> createFilterPredicate(final EntityInfo<T> info, FilterBean bean) {
|
||||||
if (info == null || (column == null && this.nodes == null)) return null;
|
if (info == null || (column == null && this.nodes == null)) return null;
|
||||||
final Serializable val = getValue(bean);
|
final Serializable val = getValue(bean);
|
||||||
Predicate<T> filter = (val == null || column == null) ? null : createFilterPredicate(info.getAttribute(column), val);
|
Predicate<T> filter = createFilterPredicate(column == null ? null : info.getAttribute(column), val);
|
||||||
if (this.nodes == null) return filter;
|
if (this.nodes == null) return filter;
|
||||||
for (FilterNode node : this.nodes) {
|
for (FilterNode node : this.nodes) {
|
||||||
Predicate<T> f = node.createFilterPredicate(info, bean);
|
Predicate<T> f = node.createFilterPredicate(info, bean);
|
||||||
@@ -257,22 +268,130 @@ public class FilterNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected final <T> Predicate<T> createFilterPredicate(final Attribute<T, Serializable> attr, Serializable val0) {
|
protected final <T> Predicate<T> createFilterPredicate(final Attribute<T, Serializable> attr, Serializable val0) {
|
||||||
|
if (val0 == null) {
|
||||||
|
if (express == ISNULL) return new Predicate<T>() {
|
||||||
|
|
||||||
|
@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<T>() {
|
||||||
|
|
||||||
|
@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;
|
if (attr == null) return null;
|
||||||
|
|
||||||
final Class atype = attr.type();
|
final Class atype = attr.type();
|
||||||
if (val0 != null && atype != val0.getClass() && val0 instanceof Number) {
|
final Class valtype = val0.getClass();
|
||||||
if (atype == short.class || atype == Short.class) {
|
if (atype != valtype && val0 instanceof Number) {
|
||||||
val0 = ((Number) val0).shortValue();
|
if (atype == int.class || atype == Integer.class) {
|
||||||
|
val0 = ((Number) val0).intValue();
|
||||||
} else if (atype == long.class || atype == Long.class) {
|
} else if (atype == long.class || atype == Long.class) {
|
||||||
val0 = ((Number) val0).longValue();
|
val0 = ((Number) val0).longValue();
|
||||||
} else if (atype == byte.class || atype == Byte.class) {
|
} else if (atype == short.class || atype == Short.class) {
|
||||||
val0 = ((Number) val0).byteValue();
|
val0 = ((Number) val0).shortValue();
|
||||||
} else if (atype == int.class || atype == Integer.class) {
|
|
||||||
val0 = ((Number) val0).intValue();
|
|
||||||
} else if (atype == float.class || atype == Float.class) {
|
} else if (atype == float.class || atype == Float.class) {
|
||||||
val0 = ((Number) val0).floatValue();
|
val0 = ((Number) val0).floatValue();
|
||||||
|
} else if (atype == byte.class || atype == Byte.class) {
|
||||||
|
val0 = ((Number) val0).byteValue();
|
||||||
} else if (atype == double.class || atype == Double.class) {
|
} else if (atype == double.class || atype == Double.class) {
|
||||||
val0 = ((Number) val0).doubleValue();
|
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<Number>) collection) {
|
||||||
|
list.add(num.intValue());
|
||||||
|
}
|
||||||
|
} else if (atype == long.class || atype == Long.class) {
|
||||||
|
for (Number num : (Collection<Number>) collection) {
|
||||||
|
list.add(num.longValue());
|
||||||
|
}
|
||||||
|
} else if (atype == short.class || atype == Short.class) {
|
||||||
|
for (Number num : (Collection<Number>) collection) {
|
||||||
|
list.add(num.shortValue());
|
||||||
|
}
|
||||||
|
} else if (atype == float.class || atype == Float.class) {
|
||||||
|
for (Number num : (Collection<Number>) collection) {
|
||||||
|
list.add(num.floatValue());
|
||||||
|
}
|
||||||
|
} else if (atype == byte.class || atype == Byte.class) {
|
||||||
|
for (Number num : (Collection<Number>) collection) {
|
||||||
|
list.add(num.byteValue());
|
||||||
|
}
|
||||||
|
} else if (atype == double.class || atype == Double.class) {
|
||||||
|
for (Number num : (Collection<Number>) collection) {
|
||||||
|
list.add(num.doubleValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val0 = list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
final Serializable val = val0;
|
final Serializable val = val0;
|
||||||
switch (express) {
|
switch (express) {
|
||||||
@@ -348,30 +467,7 @@ public class FilterNode {
|
|||||||
return attr.field() + ' ' + express.value() + ' ' + val;
|
return attr.field() + ' ' + express.value() + ' ' + val;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
case ISNULL: return new Predicate<T>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean test(T t) {
|
|
||||||
return attr.get(t) == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return attr.field() + " = null";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
case ISNOTNULL: return new Predicate<T>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean test(T t) {
|
|
||||||
return attr.get(t) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return attr.field() + " != null";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
case OPAND: return new Predicate<T>() {
|
case OPAND: return new Predicate<T>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -477,7 +573,6 @@ public class FilterNode {
|
|||||||
Predicate<T> filter;
|
Predicate<T> filter;
|
||||||
if (val instanceof Collection) {
|
if (val instanceof Collection) {
|
||||||
Collection array = (Collection) val;
|
Collection array = (Collection) val;
|
||||||
if (array.isEmpty()) return null;
|
|
||||||
filter = new Predicate<T>() {
|
filter = new Predicate<T>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -492,7 +587,6 @@ public class FilterNode {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
if (Array.getLength(val) < 1) return null;
|
|
||||||
Class type = val.getClass();
|
Class type = val.getClass();
|
||||||
if (type == int[].class) {
|
if (type == int[].class) {
|
||||||
filter = new Predicate<T>() {
|
filter = new Predicate<T>() {
|
||||||
|
|||||||
Reference in New Issue
Block a user