BeanUtil

来源:互联网 发布:js分割字符串包含转义 编辑:程序博客网 时间:2024/05/18 00:22
public class BeanUtil {public String prefix_String = "FLD";public boolean isNoNeedPrefix = false;public BeanUtil(){}public BeanUtil(boolean isNoNeedPrefix){this.isNoNeedPrefix = isNoNeedPrefix;}public <T> T setFields(T oldEntity,T newEntity) throws IllegalArgumentException, IllegalAccessException{if(oldEntity == null || newEntity == null) {throw new IllegalArgumentException("entity is null");}List<Field> oldFields = toFieldList(oldEntity);List<Field> newFields = toFieldList(newEntity);for (int i = 0; i < newFields.size(); i++) {Object fieldValue = oldFields.get(i).get(oldEntity);newFields.get(i).set(newEntity, fieldValue);}return newEntity;}public List<Field> toFieldList(Object entity) {if(entity == null ) {throw new IllegalArgumentException("entity is null");}List<Field> tblFieldList = new ArrayList<Field>();//   Field[] fields = entity.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {fields[i].setAccessible(true);if (fields[i].getName().substring(0, prefix_String.length()).equalsIgnoreCase(prefix_String) || isNoNeedPrefix) {tblFieldList.add(fields[i]);}}return tblFieldList;}public List<String> toFieldNameList(Object entity) {if(entity == null ) {throw new IllegalArgumentException("entity is null");}List<String> tblFieldList = new ArrayList<String>();Field[] fields = entity.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {if (fields[i].getName().substring(0, prefix_String.length()).equalsIgnoreCase(prefix_String) || isNoNeedPrefix) {tblFieldList.add(fields[i].getName().toUpperCase());}}return tblFieldList;}public List<Object> toFieldValueList(Object entity,String... fieldNames){return toFieldValueList(entity,false,fieldNames);}public List<Object> toFieldValueList(Object entity,boolean cutNull,String... fieldNames){if(entity == null ) {throw new IllegalArgumentException("entity is null");}List<Field>fieldList = toFieldList(entity);List<Object> valueList = new ArrayList<Object>();if(fieldNames == null || fieldNames.length == 0){for (Field field : fieldList) {Object value = null;try {if(cutNull){value = cutNull(field,entity);}else{value = field.get(entity);}} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}valueList.add(value);}}else{Map<String, Field> fieldMap = toFieldMap(entity);for(String fieldName : fieldNames){String key = fieldName.toUpperCase();Object value = null;if(fieldMap.containsKey(key)){if(cutNull){value = cutNull(fieldMap.get(key),entity);}else{try {value = fieldMap.get(key).get(entity);} catch (Exception e) {e.printStackTrace();}}}valueList.add(value);}//Map<String, Object> fieldMap = toFieldValueMap(entity);//for(String fieldName : fieldNames){//String key = fieldName.toUpperCase();//Object value = null;//if(fieldMap.containsKey(key)){//value = fieldMap.get(key);//if(cutNull){//value = cutNull(value);//}//}//valueList.add(value);//}}return valueList;}private Object cutNull(Field field,Object entity){if(field==null){return null;}Object value = null;try {value = field.get(entity);} catch (Exception e) {e.printStackTrace();}if(value != null){return value;}if(field.getType() == String.class){value = "";}else if(field.getType() == Integer.class || field.getType() == Long.class){value = 0;}else if(field.getType() == Double.class || field.getType() == Float.class){value = 0;}return value;}public Map<String, Field> toFieldMap(Object entity) {if(entity == null ) {throw new IllegalArgumentException("entity is null");}Map<String, Field> fieldMap = new HashMap<String, Field>();Field[] fields = entity.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {fields[i].setAccessible(true);if (fields[i].getName().substring(0, prefix_String.length()).equalsIgnoreCase(prefix_String) || isNoNeedPrefix) {fieldMap.put(fields[i].getName().toUpperCase(), fields[i]);}}return fieldMap;}public Map<String, Object> toFieldValueMap(Object entity){if(entity == null ) {throw new IllegalArgumentException("entity is null");}Map<String, Object> fieldValueMap = new HashMap<String, Object>();Field[] fields = entity.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {fields[i].setAccessible(true);if (fields[i].getName().substring(0, prefix_String.length()).equalsIgnoreCase(prefix_String) || isNoNeedPrefix) {Object value = null;try {value = fields[i].get(entity);} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}fieldValueMap.put(fields[i].getName().toUpperCase(), value);}}return fieldValueMap;}public String toFieldNameString(Object entity){if(entity == null ) {throw new IllegalArgumentException("entity is null");}String filedNameString = "";List<String> fieldNameList = toFieldNameList(entity);for(String fieldName : fieldNameList){filedNameString += fieldName + ",";}return filedNameString.substring(0, filedNameString.length()-1);}public String toFieldValueString(Object entity,String ...fieldNames ){if(entity == null ) {throw new IllegalArgumentException("entity is null");}String filedValueString = "";List<Object> fieldValueList = toFieldValueList(entity, fieldNames);for(Object fieldValue:fieldValueList){filedValueString += fieldValue + ",";}return filedValueString.substring(0, filedValueString.length()-1);}public <T> T[] toFieldNameArray(Object entity,T...returnTypes){if(entity == null ) {throw new IllegalArgumentException("entity is null");}List<String> fieldNameList = toFieldNameList(entity);return fieldNameList.toArray(returnTypes);}public Object[] toFieldValueArray(Object entity,String...fieldNames){if(entity == null ) {throw new IllegalArgumentException("entity is null");}List<Object> fieldValueList = toFieldValueList(entity, fieldNames);return fieldValueList.toArray();}public String cutPrefix(String content,String ...preFix ){if(preFix.length ==1){return content.replace(preFix[0], "").toLowerCase();}else{return content.replace(preFix[0], AppControler.DB_PREFIX).toLowerCase();}}public <T> boolean equalsBeanValues(T originalObj,T compareObj){if(originalObj.getClass() != compareObj.getClass()){System.out.println("sdfsadfas");return false;}List<Object> originaValuelList = toFieldValueList(originalObj);List<Object> compareValuelList = toFieldValueList(compareObj);for(int i = 0;i<originaValuelList.size();i++){if(originaValuelList.get(i) == null && compareValuelList.get(i)  == null){}else if(originaValuelList.get(i)  != null && compareValuelList.get(i)  != null){if(!originaValuelList.get(i).equals(compareValuelList.get(i))){System.out.println(originaValuelList.get(i) + ","+  i);return false;}}else if(originaValuelList.get(i) != null && compareValuelList.get(i)  == null){if(originaValuelList.get(i) instanceof String && originaValuelList.get(i).toString().length() == 0){}else {System.out.println(originaValuelList.get(i) + ","+  i);return false;}}else if(originaValuelList.get(i) == null && compareValuelList.get(i)  != null){if(compareValuelList.get(i) instanceof String && compareValuelList.get(i).toString().length() == 0){}else{System.out.println(compareValuelList.get(i) + ","+  i);return false;}}}return true;}}

0 0