Commit 00f7aa00 authored by yanzg's avatar yanzg

Merge branch 'ver1.2' of http://192.168.0.204/yzg/yzg-util into ver1.3

parents cb8585ba d1f15a2a
...@@ -281,7 +281,7 @@ public class ObjectHelper { ...@@ -281,7 +281,7 @@ public class ObjectHelper {
* @param fromName 来源名称 * @param fromName 来源名称
* @return 名称结果 * @return 名称结果
*/ */
private static String getSimpleName(String fromName) { public static String getMethodSimpleName(String fromName) {
String toName = getSimpleFieldName(fromName); String toName = getSimpleFieldName(fromName);
if (toName.startsWith(METHOD_IS)) { if (toName.startsWith(METHOD_IS)) {
toName = toName.substring(METHOD_IS.length()); toName = toName.substring(METHOD_IS.length());
...@@ -361,7 +361,7 @@ public class ObjectHelper { ...@@ -361,7 +361,7 @@ public class ObjectHelper {
} }
String methodNameSource = method.getName(); String methodNameSource = method.getName();
String methodNameSimple = methodNameSource.toLowerCase(); String methodNameSimple = methodNameSource.toLowerCase();
String toName = getSimpleName(methodNameSource); String toName = getMethodSimpleName(methodNameSource);
if ("getClass".equals(methodNameSource)) { if ("getClass".equals(methodNameSource)) {
continue; continue;
} }
......
package com.yanzuoguang.util.helper; package com.yanzuoguang.util.helper;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/** /**
* 字符串帮主类 * 字符串帮主类
...@@ -76,8 +73,16 @@ public class ArrayHelper { ...@@ -76,8 +73,16 @@ public class ArrayHelper {
* @return * @return
*/ */
public static boolean isArrayOrList(Object val) { public static boolean isArrayOrList(Object val) {
boolean isArray = val != null && (val instanceof List || val.getClass().isArray()); return val != null && (val instanceof List || val.getClass().isArray());
return isArray; }
/**
* 判断对象是否是数组或者List
*
* @param val
* @return
*/
public static boolean isArrayOrCollection(Object val) {
return val != null && (val instanceof List || val.getClass().isArray());
} }
...@@ -122,6 +127,27 @@ public class ArrayHelper { ...@@ -122,6 +127,27 @@ public class ArrayHelper {
} }
return list; return list;
} }
/**
* 获取对象为对象列表
*
* @param val 需要获取的对象,可以为List或者Array或者其他对象.
* @return
*/
public static Collection getCollection(Object val) {
boolean isArray = isArrayOrCollection(val);
Collection list;
// 判断处理
if (val instanceof Collection) {
list = (Collection) val;
} else if (isArray) {
Object[] arr = (Object[]) val;
list = Arrays.asList(arr);
} else {
list = new ArrayList();
list.add(val);
}
return list;
}
/** /**
* 将数组添加到列表中 * 将数组添加到列表中
......
...@@ -76,12 +76,12 @@ public class StringHelper { ...@@ -76,12 +76,12 @@ public class StringHelper {
return true; return true;
} }
// 判断是否属于数组 // 判断是否属于数组
boolean isArray = ArrayHelper.isArrayOrList(val); boolean isArray = ArrayHelper.isArrayOrCollection(val);
// 不是数组,则为常规值 // 不是数组,则为常规值
if (!isArray) { if (!isArray) {
return false; return false;
} }
List list = ArrayHelper.getList(val); Collection list = ArrayHelper.getCollection(val);
return list.isEmpty(); return list.isEmpty();
} }
......
...@@ -2,6 +2,7 @@ package helper; ...@@ -2,6 +2,7 @@ package helper;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.JsonHelper;
import helper.vo.MapSub;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -42,6 +43,32 @@ public class TestJsonHelper { ...@@ -42,6 +43,32 @@ public class TestJsonHelper {
Assert.assertNotNull(to.getList()); Assert.assertNotNull(to.getList());
Assert.assertNull(to.getListNull()); Assert.assertNull(to.getListNull());
} }
@Test
public void testMapRow() {
String key = "age";
String name = "颜佐光";
MapSub from = new MapSub();
MapSub from1 = new MapSub();
from.setName(name);
from.setName(name);
from1.setName(name);
from1.setName(name);
from.setYear(15);
from1.setYear(15);
from.put("age", 15);
String json = JsonHelper.serialize(from);
MapSub to = JsonHelper.deserialize(json, new TypeReference<MapSub>() {
});
Assert.assertEquals(from.getName(), to.getName());
Assert.assertEquals(from.getYear(), to.getYear());
Assert.assertEquals(from.get(key), to.get(key));
Assert.assertEquals(from.getName(), name);
Assert.assertEquals(from, to);
}
} }
...@@ -129,4 +156,5 @@ class FanxingData { ...@@ -129,4 +156,5 @@ class FanxingData {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
} }
\ No newline at end of file
package helper.vo;
import com.alibaba.fastjson.TypeReference;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.vo.MapRow;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MapRowCache extends MapRow {
private final static Map<String, TypeReference<?>> mapMethodReference = new ConcurrentHashMap<>();
private final static Map<Class<?>, DataInfo> mapClassDataInfo = new ConcurrentHashMap<>();
public MapRowCache() {
}
@Override
public Object get(Object key) {
return super.get(key);
}
@Override
public Object put(String key, Object value) {
return super.put(key, value);
}
protected <T> T getField(Get<T> getter) {
DataInfo dataInfo = getDataInfo(getter.getClass(), 3);
System.out.println(dataInfo.getName() + "()" + " called by " + dataInfo.getCls().getName());
return null;
}
protected <T> void putField(Set<T> setter, T value) {
DataInfo dataInfo = getDataInfo(setter.getClass(), 3);
System.out.println(dataInfo.getName() + "()" + " called by " + dataInfo.getCls().getName());
}
private DataInfo getDataInfo(Class<?> cls, int pos) {
if (!mapClassDataInfo.containsKey(cls)) {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
String methodName = stackTraceElements[pos].getMethodName();
String fieldName = getMethodFieldName(methodName);
String fieldClsName = getMethodFieldClsName(this.getClass(), fieldName);
if (!mapMethodReference.containsKey(fieldClsName)) {
throw new IllegalArgumentException("未注册类型:" + cls.getName() + " 方法关联:" + methodName);
}
DataInfo dataInfo = new DataInfo(methodName, cls, mapMethodReference.get(cls));
mapClassDataInfo.put(cls, dataInfo);
return dataInfo;
}
return mapClassDataInfo.get(cls);
}
private static String getMethodFieldClsName(Class<?> cls, String fieldName) {
return cls.getName() + "." + fieldName;
}
protected static void register(Class<?> cls, String fieldName, TypeReference<?> typeReference) {
String methodFieldClsName = getMethodFieldClsName(cls, fieldName);
mapMethodReference.put(methodFieldClsName, typeReference);
}
protected interface Get<T> {
T get();
}
protected interface Set<T> {
void set(T t);
}
/**
* 字段关联的数据信息
*/
protected static class DataInfo {
private final String name;
private final Class<?> cls;
private final TypeReference<?> typeReference;
public DataInfo(String name, Class<?> cls, TypeReference<?> typeReference) {
this.name = name;
this.cls = cls;
this.typeReference = typeReference;
}
public String getName() {
return name;
}
public Class<?> getCls() {
return cls;
}
public TypeReference<?> getTypeReference() {
return typeReference;
}
}
/**
* 获取方法字段名
*
* @param fullName 方法全名
* @return 方法字段名
*/
protected String getMethodFieldName(String fullName) {
return ObjectHelper.getMethodSimpleName(fullName);
}
}
package helper.vo;
import com.alibaba.fastjson.TypeReference;
public class MapSub extends MapRowCache {
static {
TypeReference<String> typeStringReference = new TypeReference<String>() {
};
TypeReference<Integer> typeIntegerReference = new TypeReference<Integer>() {
};
register(MapSub.class, "name", typeStringReference);
register(MapSub.class, "year", typeIntegerReference);
}
public String getName() {
return this.getField(this::getName);
}
public void setName(String name) {
this.putField(this::setName, name);
}
public int getYear() {
return this.getField(this::getYear);
}
public void setYear(int age) {
this.putField(this::setYear, age);
}
}
...@@ -6,10 +6,7 @@ import com.yanzuoguang.util.base.ObjectHelper; ...@@ -6,10 +6,7 @@ import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.ArrayHelper; import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* 包含的值字段 * 包含的值字段
...@@ -90,7 +87,7 @@ public class SqlCondEquals extends SqlCondBase<SqlCondEquals> { ...@@ -90,7 +87,7 @@ public class SqlCondEquals extends SqlCondBase<SqlCondEquals> {
val = StringHelper.toInt(val); val = StringHelper.toInt(val);
} }
// 判断值是否相等 // 判断值是否相等
List list = ArrayHelper.getList(val); Collection list = ArrayHelper.getCollection(val);
for (Object item : list) { for (Object item : list) {
// 值处理 // 值处理
Object itemTo = item; Object itemTo = item;
......
...@@ -9,6 +9,7 @@ import com.yanzuoguang.util.helper.ArrayHelper; ...@@ -9,6 +9,7 @@ import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -79,7 +80,7 @@ public class SqlCondItem extends SqlCondBase<SqlCondItem> { ...@@ -79,7 +80,7 @@ public class SqlCondItem extends SqlCondBase<SqlCondItem> {
throw YzgError.getRuntimeException("026", this.getClass().getSimpleName(), sqlData.getName(), fieldName); throw YzgError.getRuntimeException("026", this.getClass().getSimpleName(), sqlData.getName(), fieldName);
} }
Object value = ObjectHelper.get(model, fieldName); Object value = ObjectHelper.get(model, fieldName);
List list = ArrayHelper.getList(value); Collection list = ArrayHelper.getCollection(value);
for (Object item : list) { for (Object item : list) {
// 处理代码片段 // 处理代码片段
......
...@@ -707,7 +707,7 @@ public abstract class BaseDaoSql { ...@@ -707,7 +707,7 @@ public abstract class BaseDaoSql {
sql = getSqlCodeParameter(paras, sql, name, (SqlCodeParameter) val); sql = getSqlCodeParameter(paras, sql, name, (SqlCodeParameter) val);
} else { } else {
// 判断是否为数组 // 判断是否为数组
boolean isArray = ArrayHelper.isArrayOrList(val); boolean isArray = ArrayHelper.isArrayOrCollection(val);
if (isArray) { if (isArray) {
sql = getListSql(paras, sql, name, val); sql = getListSql(paras, sql, name, val);
} else { } else {
...@@ -747,13 +747,11 @@ public abstract class BaseDaoSql { ...@@ -747,13 +747,11 @@ public abstract class BaseDaoSql {
* @return 获取包含列表的SQL语句的值 * @return 获取包含列表的SQL语句的值
*/ */
private String getListSql(List<Object> paras, String sql, String name, Object val) { private String getListSql(List<Object> paras, String sql, String name, Object val) {
List list = ArrayHelper.getList(val); Collection list = ArrayHelper.getCollection(val);
int length = list.size();
// 进行循环 // 进行循环
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) { int i = 0;
Object item = list.get(i); for (Object item : list) {
// 判断列表是否为常规对象 // 判断列表是否为常规对象
if (item == null || item instanceof Number || item.getClass().isEnum() || item instanceof String || item instanceof Date) { if (item == null || item instanceof Number || item.getClass().isEnum() || item instanceof String || item instanceof Date) {
addArrayIn(paras, sb, i, item); addArrayIn(paras, sb, i, item);
...@@ -762,6 +760,7 @@ public abstract class BaseDaoSql { ...@@ -762,6 +760,7 @@ public abstract class BaseDaoSql {
} else { } else {
addArrayTable(paras, sb, i, item); addArrayTable(paras, sb, i, item);
} }
i++;
} }
// 生成最终的SQL语句 // 生成最终的SQL语句
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment