123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- package com.keao.edu.util.collection;
- import org.apache.commons.lang3.StringUtils;
- import java.beans.BeanInfo;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.math.BigDecimal;
- import java.util.*;
- public class MapUtil {
- private static final Object[] EMPTY_ARRAY = {};
- /**
- * 地球半径,单位:公里/千米
- */
- private static final double EARTH_RADIUS = 6378.137;
- /**
- * 将一个bean转换成map
- * @param map
- * @param bean
- * @return
- */
- public static Map<String, Object> populateMap(Map<String, Object> map, Object bean) {
- return populateMap(map, bean, null);
- }
- /**
- * 假设prefix=detail.,bean带有一个属性name,则map中将有一个项:
- * key=detail.name,value为bean的name属性值。
- */
- public static Map<String, Object> populateMap(Map<String, Object> map, Object bean, String prefix) {
- boolean withoutPrefix = StringUtils.isBlank(prefix);
- try {
- Method[] methods = bean.getClass().getMethods();
- for (int i = 0; i < methods.length; i++) {
- String methodName = methods[i].getName();
- Class<?>[] pts = methods[i].getParameterTypes();
- Class<?> rt = methods[i].getReturnType();
- if (methodName.startsWith("get") && pts.length == 0 && !Void.class.equals(rt)) {
- String propName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
- if ("class".equals(propName)) {
- continue;
- }
- String key = withoutPrefix ? propName : prefix + propName;
- Object value = methods[i].invoke(bean, EMPTY_ARRAY);
- if (value != null) {
- map.put(key, value);
- }
- }
- }
- return map;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * map to javabean
- * @param clazz
- * @param map
- * @return
- * @throws IllegalArgumentException
- * @throws IllegalAccessException
- * @throws IntrospectionException
- * @throws InstantiationException
- */
- @SuppressWarnings("rawtypes")
- public static <T> T mapToJavaBean(Class<T> clazz, Map map) throws IllegalAccessException, IllegalArgumentException, IntrospectionException,
- InstantiationException {
- T obj = null;
- BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
- obj = clazz.newInstance();
- PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
- for (int i = 0; i < propertyDescriptors.length; i++) {
- PropertyDescriptor descriptor = propertyDescriptors[i];
- String propertyName = descriptor.getName();
- if (map.containsKey(propertyName)) {
- Object value = map.get(propertyName);
- if ("".equals(value)) {
- value = null;
- }
- Object[] args = new Object[1];
- args[0] = value;
- try {
- descriptor.getWriteMethod().invoke(obj, args);
- } catch (InvocationTargetException e) {
- System.out.println("params poxy failed");
- }
- }
- }
- return (T) obj;
- }
- /**
- * @Author: Joburgess
- * @Date: 2019/10/12
- * @params [maps]
- * @return java.util.Map
- * @describe mybatis返回结果转换为Map
- */
- /*public static <T,K> Map convertMybatisMap(List<Map<T,K>> maps){
- int size = 0;
- if(maps != null){
- size = maps.size();
- }
- Map<T,K> result = new HashMap(size);
- for (Map<T, K> map : maps) {
- if(map.get("value") != null){
- result.put((T) (map.get("key").toString()),(K) (map.get("value").toString()));
- }
- }
- *//*maps.forEach(stringIntegerMap -> {
- if(Objects.isNull(stringIntegerMap)){
- return;
- }
- result.put(stringIntegerMap.get("key"),stringIntegerMap.get("value"));
- });*//*
- return result;
- }*/
- /**
- * @param <K>
- * @Author: Joburgess
- * @Date: 2019/10/12
- * @params [maps]
- * @return java.util.Map
- * @describe mybatis返回结果转换为Map
- */
- public static <K, V> Map<K, V> convertMybatisMap(List<Map<K, V>> maps,Class keyType,Class valueType) {
- int size = 0;
- if(maps != null){
- size = maps.size();
- }else{
- return Collections.EMPTY_MAP;
- }
- Map result=new HashMap(size);
- try {
- for (Map<K, V> stringIntegerMap : maps) {
- if(Objects.isNull(stringIntegerMap)){
- continue;
- }
- if(stringIntegerMap.get("value") == null){
- continue;
- }
- K key;
- V value;
- if(keyType.isAssignableFrom(BigDecimal.class)){
- key = (K) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(stringIntegerMap.get("key").toString());
- }else if(keyType.isAssignableFrom(String.class)){
- key = (K) stringIntegerMap.get("key").toString();
- }else{
- key = (K) keyType.getMethod("valueOf", String.class).invoke(null,stringIntegerMap.get("key").toString());
- }
- if(valueType.isAssignableFrom(BigDecimal.class)){
- value = (V) BigDecimal.class.getDeclaredConstructor(String.class).newInstance(stringIntegerMap.get("value").toString());
- }else if(valueType.isAssignableFrom(String.class)){
- value = (V) stringIntegerMap.get("value").toString();
- }else{
- value = (V) valueType.getMethod("valueOf", String.class).invoke(null,stringIntegerMap.get("value").toString());
- }
- result.put(key, value);
- };
- }catch (Exception e){
- e.printStackTrace();
- }
- return result;
- }
- /**
- * 经纬度转化成弧度
- * @param d
- * 经度/纬度
- * @return 经纬度转化成的弧度
- */
- private static double radian(double d) {
- return d * Math.PI / 180.0;
- }
- /**
- * 返回两个地理坐标之间的距离
- * @param firsLongitude
- * 第一个坐标的经度
- * @param firstLatitude
- * 第一个坐标的纬度
- * @param secondLongitude
- * 第二个坐标的经度
- * @param secondLatitude
- * 第二个坐标的纬度
- * @return 两个坐标之间的距离,单位:公里/千米
- */
- public static double distance(double firsLongitude, double firstLatitude,
- double secondLongitude, double secondLatitude) {
- double firstRadianLongitude = radian(firsLongitude);
- double firstRadianLatitude = radian(firstLatitude);
- double secondRadianLongitude = radian(secondLongitude);
- double secondRadianLatitude = radian(secondLatitude);
- double a = firstRadianLatitude - secondRadianLatitude;
- double b = firstRadianLongitude - secondRadianLongitude;
- double cal = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
- + Math.cos(firstRadianLatitude) * Math.cos(secondRadianLatitude)
- * Math.pow(Math.sin(b / 2), 2)));
- cal = cal * EARTH_RADIUS;
- return Math.round(cal * 10000d) / 10000d * 1000;
- }
- /**
- * 返回两个地理坐标之间的距离
- * @param firstPoint
- * 第一个坐标 例如:"23.100919, 113.279868"
- * @param secondPoint
- * 第二个坐标 例如:"23.149286, 113.347584"
- * @return 两个坐标之间的距离,单位:公里/千米
- */
- public static double distance(String firstPoint, String secondPoint) {
- String[] firstArray = firstPoint.split(",");
- String[] secondArray = secondPoint.split(",");
- double firstLatitude = Double.valueOf(firstArray[0].trim());
- double firstLongitude = Double.valueOf(firstArray[1].trim());
- double secondLatitude = Double.valueOf(secondArray[0].trim());
- double secondLongitude = Double.valueOf(secondArray[1].trim());
- return distance(firstLatitude, firstLongitude, secondLatitude, secondLongitude);
- }
- }
|