diff --git a/src/main/java/org/redkale/cluster/spi/ClusterModuleEngine.java b/src/main/java/org/redkale/cluster/spi/ClusterModuleEngine.java index e21257c7e..f89b8d703 100644 --- a/src/main/java/org/redkale/cluster/spi/ClusterModuleEngine.java +++ b/src/main/java/org/redkale/cluster/spi/ClusterModuleEngine.java @@ -125,6 +125,7 @@ public class ClusterModuleEngine extends ModuleEngine { * @param namespace 命名空间 * @param events 变更项 */ + @Override public void onEnvironmentChanged(String namespace, List events) { Set clusterRemovedKeys = new HashSet<>(); Properties clusterChangedProps = new Properties(); diff --git a/src/main/java/org/redkale/source/EntityCache.java b/src/main/java/org/redkale/source/EntityCache.java index b5724ee7d..6ba0b596d 100644 --- a/src/main/java/org/redkale/source/EntityCache.java +++ b/src/main/java/org/redkale/source/EntityCache.java @@ -434,10 +434,10 @@ public final class EntityCache { } public boolean exists(final Predicate filter) { - return (filter != null) && this.list.stream().filter(filter).findFirst().isPresent(); + return (filter != null) && this.list.stream().anyMatch(filter); } - public Map queryColumnMap(final String keyColumn, final FilterFunc func, final String funcColumn, FilterNode node) { + public Map queryColumnMap(final String keyColumn, final FilterFunc func, final String funcColumn, FilterNode node) { final Attribute keyAttr = info.getAttribute(keyColumn); final Predicate filter = node == null ? null : node.createPredicate(this); final Attribute funcAttr = funcColumn == null ? null : info.getAttribute(funcColumn); @@ -460,7 +460,7 @@ public final class EntityCache { collector = (Collector) Collectors.counting(); break; case DISTINCTCOUNT: - collector = (Collector) Collectors.mapping((t) -> funcAttr.get(t), Collectors.toSet()); + collector = (Collector) Collectors.mapping(funcAttr::get, Collectors.toSet()); break; case MAX: case MIN: @@ -476,8 +476,8 @@ public final class EntityCache { break; } } - Map rs = collector == null ? stream.collect(Collectors.toMap(t -> keyAttr.get(t), t -> funcAttr.get(t), (key1, key2) -> key2)) - : stream.collect(Collectors.groupingBy(t -> keyAttr.get(t), LinkedHashMap::new, collector)); + Map rs = collector == null ? stream.collect(Collectors.toMap(keyAttr::get, funcAttr::get, (key1, key2) -> key2)) + : stream.collect(Collectors.groupingBy(keyAttr::get, LinkedHashMap::new, collector)); if (func == MAX || func == MIN) { Map rs2 = new LinkedHashMap(); rs.forEach((x, y) -> { @@ -565,7 +565,8 @@ public final class EntityCache { return null; } - private Number getNumberResult(final Collection entityList, final FilterFunc func, final Number defResult, final Class attrType, final Function attrFunc, final FilterNode node) { + private Number getNumberResult(final Collection entityList, final FilterFunc func, + final Number defResult, final Class attrType, final Function attrFunc, final FilterNode node) { final Predicate filter = node == null ? null : node.createPredicate(this); Stream stream = entityList.stream(); if (filter != null) { @@ -574,10 +575,10 @@ public final class EntityCache { switch (func) { case AVG: if (attrType == int.class || attrType == Integer.class || attrType == AtomicInteger.class) { - OptionalDouble rs = stream.mapToInt(x -> ((Number) attrFunc.apply(x)).intValue()).average(); + OptionalDouble rs = stream.mapToInt(x -> attrFunc.apply(x).intValue()).average(); return rs.isPresent() ? (int) rs.getAsDouble() : defResult; } else if (attrType == long.class || attrType == Long.class || attrType == AtomicLong.class || attrType == LongAdder.class) { - OptionalDouble rs = stream.mapToLong(x -> ((Number) attrFunc.apply(x)).longValue()).average(); + OptionalDouble rs = stream.mapToLong(x -> attrFunc.apply(x).longValue()).average(); return rs.isPresent() ? (long) rs.getAsDouble() : defResult; } else if (attrType == short.class || attrType == Short.class) { OptionalDouble rs = stream.mapToInt(x -> ((Short) attrFunc.apply(x)).intValue()).average(); @@ -593,14 +594,14 @@ public final class EntityCache { case COUNT: return stream.count(); case DISTINCTCOUNT: - return stream.map(x -> attrFunc.apply(x)).distinct().count(); + return stream.map(attrFunc::apply).distinct().count(); case MAX: if (attrType == int.class || attrType == Integer.class || attrType == AtomicInteger.class) { - OptionalInt rs = stream.mapToInt(x -> ((Number) attrFunc.apply(x)).intValue()).max(); + OptionalInt rs = stream.mapToInt(x -> attrFunc.apply(x).intValue()).max(); return rs.isPresent() ? rs.getAsInt() : defResult; } else if (attrType == long.class || attrType == Long.class || attrType == AtomicLong.class || attrType == LongAdder.class) { - OptionalLong rs = stream.mapToLong(x -> ((Number) attrFunc.apply(x)).longValue()).max(); + OptionalLong rs = stream.mapToLong(x -> attrFunc.apply(x).longValue()).max(); return rs.isPresent() ? rs.getAsLong() : defResult; } else if (attrType == short.class || attrType == Short.class) { OptionalInt rs = stream.mapToInt(x -> ((Short) attrFunc.apply(x)).intValue()).max(); @@ -616,10 +617,10 @@ public final class EntityCache { case MIN: if (attrType == int.class || attrType == Integer.class || attrType == AtomicInteger.class) { - OptionalInt rs = stream.mapToInt(x -> ((Number) attrFunc.apply(x)).intValue()).min(); + OptionalInt rs = stream.mapToInt(x -> attrFunc.apply(x).intValue()).min(); return rs.isPresent() ? rs.getAsInt() : defResult; } else if (attrType == long.class || attrType == Long.class || attrType == AtomicLong.class || attrType == LongAdder.class) { - OptionalLong rs = stream.mapToLong(x -> ((Number) attrFunc.apply(x)).longValue()).min(); + OptionalLong rs = stream.mapToLong(x -> attrFunc.apply(x).longValue()).min(); return rs.isPresent() ? rs.getAsLong() : defResult; } else if (attrType == short.class || attrType == Short.class) { OptionalInt rs = stream.mapToInt(x -> ((Short) attrFunc.apply(x)).intValue()).min(); @@ -635,9 +636,9 @@ public final class EntityCache { case SUM: if (attrType == int.class || attrType == Integer.class || attrType == AtomicInteger.class) { - return stream.mapToInt(x -> ((Number) attrFunc.apply(x)).intValue()).sum(); + return stream.mapToInt(x -> attrFunc.apply(x).intValue()).sum(); } else if (attrType == long.class || attrType == Long.class || attrType == AtomicLong.class || attrType == LongAdder.class) { - return stream.mapToLong(x -> ((Number) attrFunc.apply(x)).longValue()).sum(); + return stream.mapToLong(x -> attrFunc.apply(x).longValue()).sum(); } else if (attrType == short.class || attrType == Short.class) { return (short) stream.mapToInt(x -> ((Short) attrFunc.apply(x)).intValue()).sum(); } else if (attrType == float.class || attrType == Float.class) { @@ -660,7 +661,7 @@ public final class EntityCache { return querySheet(true, false, selects, flipper, node); } - protected Stream distinctStream(Stream stream, final List> keyattrs) { + protected Stream distinctStream(Stream stream, final List> keyattrs) { if (keyattrs == null) { return stream; } @@ -680,7 +681,7 @@ public final class EntityCache { return stream.filter(filter); } - public Sheet querySheet(final boolean needtotal, final boolean distinct, final SelectColumn selects, final Flipper flipper, FilterNode node) { + public Sheet querySheet(final boolean needTotal, boolean distinct, final SelectColumn selects, final Flipper flipper, FilterNode node) { final Predicate filter = node == null ? null : node.createPredicate(this); final Comparator comparator = createComparator(flipper); long total = 0; @@ -694,7 +695,7 @@ public final class EntityCache { }); keyattrs = attrs; } - if (needtotal) { + if (needTotal) { Stream stream = this.list.stream(); if (filter != null) { stream = stream.filter(filter); @@ -704,7 +705,7 @@ public final class EntityCache { } total = stream.count(); } - if (needtotal && total == 0) { + if (needTotal && total == 0) { return new Sheet<>(0, new ArrayList()); } Stream stream = this.list.stream(); @@ -751,7 +752,7 @@ public final class EntityCache { stream.forEach(action); } } - if (!needtotal) { + if (!needTotal) { total = rs.size(); } return new Sheet<>(total, rs); @@ -1014,7 +1015,8 @@ public final class EntityCache { case MOD: case AND: case ORR: - numb = getNumberValue((Number) attr.get(entity), express, val instanceof ColumnNumberNode ? ((ColumnNumberNode) val).getValue() : (Number) val); + numb = getNumberValue((Number) attr.get(entity), express, + val instanceof ColumnNumberNode ? ((ColumnNumberNode) val).getValue() : (Number) val); break; case SET: if (val instanceof ColumnExpNode) { @@ -1164,7 +1166,8 @@ public final class EntityCache { //------------------------------------------------------------------------------------------------------------------------------- protected Comparator createComparator(Flipper flipper) { - if (flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty() || flipper.getSort().indexOf(';') >= 0 || flipper.getSort().indexOf('\n') >= 0) { + if (flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty() + || flipper.getSort().indexOf(';') >= 0 || flipper.getSort().indexOf('\n') >= 0) { return null; } final String sort = flipper.getSort(); @@ -1189,7 +1192,8 @@ public final class EntityCache { Function getter = null; if (pattr.type() == int.class || pattr.type() == Integer.class || pattr.type() == AtomicInteger.class) { getter = x -> Math.abs(((Number) pattr.get((T) x)).intValue()); - } else if (pattr.type() == long.class || pattr.type() == Long.class || pattr.type() == AtomicLong.class || pattr.type() == LongAdder.class) { + } else if (pattr.type() == long.class || pattr.type() == Long.class + || pattr.type() == AtomicLong.class || pattr.type() == LongAdder.class) { getter = x -> Math.abs(((Number) pattr.get((T) x)).longValue()); } else if (pattr.type() == float.class || pattr.type() == Float.class) { getter = x -> Math.abs(((Number) pattr.get((T) x)).floatValue()); @@ -1198,7 +1202,8 @@ public final class EntityCache { } else { throw new SourceException("Flipper not supported sort illegal type by ABS (" + flipper.getSort() + ")"); } - attr = (Attribute) Attribute.create(pattr.declaringClass(), pattr.field(), pattr.type(), getter, (o, v) -> pattr.set(o, v)); + attr = (Attribute) Attribute.create(pattr.declaringClass(), + pattr.field(), pattr.type(), getter, (o, v) -> pattr.set(o, v)); } else if (func.isEmpty()) { attr = pattr; } else { diff --git a/src/main/java/org/redkale/source/FilterJoinNode.java b/src/main/java/org/redkale/source/FilterJoinNode.java index c63260979..7445ed1fd 100644 --- a/src/main/java/org/redkale/source/FilterJoinNode.java +++ b/src/main/java/org/redkale/source/FilterJoinNode.java @@ -33,7 +33,8 @@ public class FilterJoinNode extends FilterNode { public FilterJoinNode() { } - protected FilterJoinNode(FilterJoinType joinType, Class joinClass, String[] joinColumns, String column, FilterExpress express, Serializable value) { + protected FilterJoinNode(FilterJoinType joinType, Class joinClass, + String[] joinColumns, String column, FilterExpress express, Serializable value) { Objects.requireNonNull(joinClass); Objects.requireNonNull(joinColumns); if (express == null && value != null) { @@ -163,9 +164,11 @@ public class FilterJoinNode extends FilterNode { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append(" #-- ON ").append(localJoinColumns[0][0]).append("=").append(joinClass == null ? "null" : joinClass.getSimpleName()).append(".").append(localJoinColumns[0][1]); + sb.append(" #-- ON ").append(localJoinColumns[0][0]).append("=") + .append(joinClass == null ? "null" : joinClass.getSimpleName()).append(".").append(localJoinColumns[0][1]); for (int i = 1; i < localJoinColumns.length; i++) { - sb.append(" AND ").append(localJoinColumns[i][0]).append("=").append(joinClass == null ? "null" : joinClass.getSimpleName()).append(".").append(localJoinColumns[i][1]); + sb.append(" AND ").append(localJoinColumns[i][0]).append("=") + .append(joinClass == null ? "null" : joinClass.getSimpleName()).append(".").append(localJoinColumns[i][1]); } sb.append(" --# ").append(inner.toString()); return sb.toString(); @@ -265,7 +268,8 @@ public class FilterJoinNode extends FilterNode { } @Override - protected CharSequence createSQLJoin(final Function func, final boolean update, final Map joinTabalis, final Set haset, final EntityInfo info) { + protected CharSequence createSQLJoin(final Function func, + final boolean update, final Map joinTabalis, final Set haset, final EntityInfo info) { boolean moreJoin = false; if (this.joinEntity == null) { if (this.joinClass != null) { @@ -313,27 +317,33 @@ public class FilterJoinNode extends FilterNode { return sb; } - private static CharSequence createElementSQLJoin(final boolean update, final Map joinTabalis, final Set haset, final EntityInfo info, final FilterJoinNode node) { + private static CharSequence createElementSQLJoin(final boolean update, + final Map joinTabalis, final Set haset, final EntityInfo info, final FilterJoinNode node) { if (node.joinClass == null || (haset != null && haset.contains(joinTabalis.get(node.joinClass)))) { return null; } StringBuilder sb = new StringBuilder(); - String[] joinColumns = node.joinColumns; - int pos = joinColumns[0].indexOf('='); + String[] joinCols = node.joinColumns; + int pos = joinCols[0].indexOf('='); if (update) { sb.append('[').append(node.joinEntity.getTable(node)).append(" ").append(joinTabalis.get(node.joinClass)).append(']'); - sb.append('{').append(info.getSQLColumn("a", pos > 0 ? joinColumns[0].substring(0, pos) : joinColumns[0])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinColumns[0].substring(pos + 1) : joinColumns[0])); - for (int i = 1; i < joinColumns.length; i++) { - pos = joinColumns[i].indexOf('='); - sb.append(" AND ").append(info.getSQLColumn("a", pos > 0 ? joinColumns[i].substring(0, pos) : joinColumns[i])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinColumns[i].substring(pos + 1) : joinColumns[i])); + sb.append('{').append(info.getSQLColumn("a", pos > 0 ? joinCols[0].substring(0, pos) : joinCols[0])).append(" = ") + .append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinCols[0].substring(pos + 1) : joinCols[0])); + for (int i = 1; i < joinCols.length; i++) { + pos = joinCols[i].indexOf('='); + sb.append(" AND ").append(info.getSQLColumn("a", pos > 0 ? joinCols[i].substring(0, pos) : joinCols[i])).append(" = ") + .append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinCols[i].substring(pos + 1) : joinCols[i])); } sb.append('}'); } else { - sb.append(" ").append(node.joinType).append(" JOIN ").append(node.joinEntity.getTables(node)[0]).append(" ").append(joinTabalis.get(node.joinClass)) - .append(" ON ").append(info.getSQLColumn("a", pos > 0 ? joinColumns[0].substring(0, pos) : joinColumns[0])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinColumns[0].substring(pos + 1) : joinColumns[0])); - for (int i = 1; i < joinColumns.length; i++) { - pos = joinColumns[i].indexOf('='); - sb.append(" AND ").append(info.getSQLColumn("a", pos > 0 ? joinColumns[i].substring(0, pos) : joinColumns[i])).append(" = ").append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinColumns[i].substring(pos + 1) : joinColumns[i])); + sb.append(" ").append(node.joinType).append(" JOIN ").append(node.joinEntity.getTables(node)[0]).append(" ") + .append(joinTabalis.get(node.joinClass)).append(" ON ") + .append(info.getSQLColumn("a", pos > 0 ? joinCols[0].substring(0, pos) : joinCols[0])).append(" = ") + .append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinCols[0].substring(pos + 1) : joinCols[0])); + for (int i = 1; i < joinCols.length; i++) { + pos = joinCols[i].indexOf('='); + sb.append(" AND ").append(info.getSQLColumn("a", pos > 0 ? joinCols[i].substring(0, pos) : joinCols[i])).append(" = ") + .append(node.joinEntity.getSQLColumn(joinTabalis.get(node.joinClass), pos > 0 ? joinCols[i].substring(pos + 1) : joinCols[i])); } } if (haset != null) {