Entity数据库实体类支持AtomicInteger、AtomicLong字段类型

This commit is contained in:
Redkale
2018-03-14 17:36:29 +08:00
parent 285cb86891
commit 8631b4bdf5
4 changed files with 57 additions and 21 deletions

View File

@@ -10,6 +10,7 @@ import java.net.URL;
import java.sql.*;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.*;
import java.util.stream.Stream;
@@ -323,6 +324,10 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
Blob blob = conn.createBlob();
blob.setBytes(1, (byte[]) val);
prestmt.setObject(++i, blob);
} else if (val instanceof AtomicInteger) {
prestmt.setObject(++i, ((AtomicInteger) val).get());
} else if (val instanceof AtomicLong) {
prestmt.setObject(++i, ((AtomicLong) val).get());
} else {
prestmt.setObject(++i, val);
}
@@ -599,6 +604,10 @@ public class DataJdbcSource extends AbstractService implements DataSource, DataC
Blob blob = conn.createBlob();
blob.setBytes(1, (byte[]) val);
prestmt.setObject(++k, blob);
} else if (val instanceof AtomicInteger) {
prestmt.setObject(++k, ((AtomicInteger) val).get());
} else if (val instanceof AtomicLong) {
prestmt.setObject(++k, ((AtomicLong) val).get());
} else {
prestmt.setObject(++k, val);
}

View File

@@ -9,6 +9,7 @@ import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.*;
import java.util.stream.*;
@@ -226,6 +227,10 @@ public final class EntityCache<T> {
id = ((Number) id).byteValue();
} else if (atype == double.class || atype == Double.class) {
id = ((Number) id).doubleValue();
} else if (atype == AtomicInteger.class) {
id = new AtomicInteger(((Number) id).intValue());
} else if (atype == AtomicLong.class) {
id = new AtomicLong(((Number) id).longValue());
}
}
return this.map.containsKey(id);
@@ -299,11 +304,11 @@ public final class EntityCache<T> {
if (filter != null) stream = stream.filter(filter);
switch (func) {
case AVG:
if (attr.type() == int.class || attr.type() == Integer.class) {
OptionalDouble rs = stream.mapToInt(x -> (Integer) attr.get(x)).average();
if (attr.type() == int.class || attr.type() == Integer.class || attr.type() == AtomicInteger.class) {
OptionalDouble rs = stream.mapToInt(x -> ((Number) attr.get(x)).intValue()).average();
return rs.isPresent() ? (int) rs.getAsDouble() : defResult;
} else if (attr.type() == long.class || attr.type() == Long.class) {
OptionalDouble rs = stream.mapToLong(x -> (Long) attr.get(x)).average();
} else if (attr.type() == long.class || attr.type() == Long.class || attr.type() == AtomicLong.class) {
OptionalDouble rs = stream.mapToLong(x -> ((Number) attr.get(x)).longValue()).average();
return rs.isPresent() ? (long) rs.getAsDouble() : defResult;
} else if (attr.type() == short.class || attr.type() == Short.class) {
OptionalDouble rs = stream.mapToInt(x -> ((Short) attr.get(x)).intValue()).average();
@@ -322,11 +327,11 @@ public final class EntityCache<T> {
return stream.map(x -> attr.get(x)).distinct().count();
case MAX:
if (attr.type() == int.class || attr.type() == Integer.class) {
OptionalInt rs = stream.mapToInt(x -> (Integer) attr.get(x)).max();
if (attr.type() == int.class || attr.type() == Integer.class || attr.type() == AtomicInteger.class) {
OptionalInt rs = stream.mapToInt(x -> ((Number) attr.get(x)).intValue()).max();
return rs.isPresent() ? rs.getAsInt() : defResult;
} else if (attr.type() == long.class || attr.type() == Long.class) {
OptionalLong rs = stream.mapToLong(x -> (Long) attr.get(x)).max();
} else if (attr.type() == long.class || attr.type() == Long.class || attr.type() == AtomicLong.class) {
OptionalLong rs = stream.mapToLong(x -> ((Number) attr.get(x)).longValue()).max();
return rs.isPresent() ? rs.getAsLong() : defResult;
} else if (attr.type() == short.class || attr.type() == Short.class) {
OptionalInt rs = stream.mapToInt(x -> ((Short) attr.get(x)).intValue()).max();
@@ -341,11 +346,11 @@ public final class EntityCache<T> {
throw new RuntimeException("getNumberResult error(type:" + type + ", attr.declaringClass: " + attr.declaringClass() + ", attr.field: " + attr.field() + ", attr.type: " + attr.type());
case MIN:
if (attr.type() == int.class || attr.type() == Integer.class) {
OptionalInt rs = stream.mapToInt(x -> (Integer) attr.get(x)).min();
if (attr.type() == int.class || attr.type() == Integer.class || attr.type() == AtomicInteger.class) {
OptionalInt rs = stream.mapToInt(x -> ((Number) attr.get(x)).intValue()).min();
return rs.isPresent() ? rs.getAsInt() : defResult;
} else if (attr.type() == long.class || attr.type() == Long.class) {
OptionalLong rs = stream.mapToLong(x -> (Long) attr.get(x)).min();
} else if (attr.type() == long.class || attr.type() == Long.class || attr.type() == AtomicLong.class) {
OptionalLong rs = stream.mapToLong(x -> ((Number) attr.get(x)).longValue()).min();
return rs.isPresent() ? rs.getAsLong() : defResult;
} else if (attr.type() == short.class || attr.type() == Short.class) {
OptionalInt rs = stream.mapToInt(x -> ((Short) attr.get(x)).intValue()).min();
@@ -360,10 +365,10 @@ public final class EntityCache<T> {
throw new RuntimeException("getNumberResult error(type:" + type + ", attr.declaringClass: " + attr.declaringClass() + ", attr.field: " + attr.field() + ", attr.type: " + attr.type());
case SUM:
if (attr.type() == int.class || attr.type() == Integer.class) {
return stream.mapToInt(x -> (Integer) attr.get(x)).sum();
} else if (attr.type() == long.class || attr.type() == Long.class) {
return stream.mapToLong(x -> (Long) attr.get(x)).sum();
if (attr.type() == int.class || attr.type() == Integer.class || attr.type() == AtomicInteger.class) {
return stream.mapToInt(x -> ((Number) attr.get(x)).intValue()).sum();
} else if (attr.type() == long.class || attr.type() == Long.class || attr.type() == AtomicLong.class) {
return stream.mapToLong(x -> ((Number) attr.get(x)).longValue()).sum();
} else if (attr.type() == short.class || attr.type() == Short.class) {
return (short) stream.mapToInt(x -> ((Short) attr.get(x)).intValue()).sum();
} else if (attr.type() == float.class || attr.type() == Float.class) {
@@ -628,6 +633,10 @@ public final class EntityCache<T> {
newval = numb.doubleValue();
} else if (ft == byte.class || ft == Byte.class) {
newval = numb.byteValue();
} else if (ft == AtomicInteger.class) {
newval = new AtomicInteger(numb.intValue());
} else if (ft == AtomicLong.class) {
newval = new AtomicLong(numb.longValue());
}
}
attr.set(rs, (V) newval);
@@ -657,9 +666,9 @@ public final class EntityCache<T> {
final String func = sub[0].substring(0, pos);
if ("ABS".equalsIgnoreCase(func)) {
Function getter = null;
if (pattr.type() == int.class || pattr.type() == Integer.class) {
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) {
} else if (pattr.type() == long.class || pattr.type() == Long.class || pattr.type() == AtomicLong.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());

View File

@@ -10,6 +10,7 @@ import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.*;
import javax.persistence.*;
@@ -760,6 +761,18 @@ public final class EntityInfo<T> {
} else if (t == char.class) {
o = (char) 0;
}
} else if (t == AtomicInteger.class) {
if (o != null) {
o = new AtomicInteger(((Number) o).intValue());
} else {
o = new AtomicInteger();
}
} else if (t == AtomicLong.class) {
if (o != null) {
o = new AtomicLong(((Number) o).longValue());
} else {
o = new AtomicLong();
}
}
}
return o;

View File

@@ -23,11 +23,16 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
public D apply(D dest, S src);
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass) {
return create(destClass, srcClass, null);
return create(destClass, srcClass, (BiPredicate) null);
}
@SuppressWarnings("unchecked")
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass, final Predicate<String> columnPredicate) {
return create(destClass, srcClass, (sc, m) -> columnPredicate.test(m));
}
@SuppressWarnings("unchecked")
public static <D, S> Reproduce<D, S> create(final Class<D> destClass, final Class<S> srcClass, final BiPredicate<Class<S>, String> columnPredicate) {
// ------------------------------------------------------------------------------
final String supDynName = Reproduce.class.getName().replace('.', '/');
final String destName = destClass.getName().replace('.', '/');
@@ -72,7 +77,7 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
final String fname = field.getName();
try {
if (!field.getType().equals(destClass.getField(fname).getType())) continue;
if (!columnPredicate.test(fname)) continue;
if (!columnPredicate.test(srcClass, fname)) continue;
} catch (Exception e) {
continue;
}
@@ -99,7 +104,7 @@ public interface Reproduce<D, S> extends BiFunction<D, S, D> {
cs[0] = Character.toLowerCase(cs[0]);
col = new String(cs);
}
if (!columnPredicate.test(col)) continue;
if (!columnPredicate.test(srcClass, col)) continue;
}
} catch (Exception e) {
continue;