import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Maps;

public class ReflectUtil {

public static <T> Map<String, PropertyDescriptor> parserPropertyDescs(Class<T> clazz) throws
    IntrospectionException {
    Map<String, PropertyDescriptor> propertyInfos = Maps.newHashMap();
    BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class);
    PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null && pd.getReadMethod() != null) {
            propertyInfos.put(pd.getName(), pd);
        }
    }
    return propertyInfos;
}

public static String parseField(String s) {
    if(StringUtils.isBlank(s)) {
        return s;
    }
    for(int i=0; i<s.length(); i++) {
        if(s.charAt(i) == '(') {
            return s.substring(0, i);
        }
    }
    return s;
}

public static String[] merge(String[][] ss) {
    if(ss == null) {
        return null;
    }
    String[] returnResult = new String[ss.length];
    for(int i=0; i<ss.length; i++) {
        String[] s = ss[i];
        returnResult[i] = s[0] + "(" + s[1] + ")";
    }
    return returnResult;
}

}

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Map;

import com.google.common.collect.Maps;

public class ObjectConvertUtil {

private static final Map<String, Method> CONVERTERS = Maps.newHashMap();
static {
    Method[] methods = ObjectConvertUtil.class.getDeclaredMethods();
    for (Method method : methods) {
        if (method.getParameterTypes().length == 1) {
            CONVERTERS.put(method.getParameterTypes()[0].getName() + "_"
                + method.getReturnType().getName(), method);
        }
    }
}
public static <T> T convert(Object from, Class<T> to) {

    if (from == null) {
        return null;
    }

    if (to.isAssignableFrom(from.getClass())) {
        return to.cast(from);
    }

    String converterId = from.getClass().getName() + "_" + to.getName();
    Method converter = CONVERTERS.get(converterId);
    if (converter == null) {
        throw new UnsupportedOperationException("Cannot convert from "
            + from.getClass().getName() + " to " + to.getName()
            + ". Requested converter does not exist.");
    }

    try {
        return to.cast(converter.invoke(to, from));
    } catch (Exception e) {
        throw new RuntimeException("Cannot convert from "
            + from.getClass().getName() + " to " + to.getName()
            + ". Conversion failed with " + e.getMessage(), e);
    }
}

public static Boolean integerToBoolean(Integer value) {
    return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
}

public static Integer booleanToInteger(Boolean value) {
    return value.booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0);
}
public static BigDecimal doubleToBigDecimal(Double value) {
    return new BigDecimal(value.doubleValue());
}

public static Double bigDecimalToDouble(BigDecimal value) {
    return new Double(value.doubleValue());
}

public static String integerToString(Integer value) {
    return String.valueOf(value);
}

public static Integer stringToInteger(String value) {
    return Integer.parseInt(value);
}

public static String booleanToString(Boolean value) {
    return value.toString();
}

public static Boolean stringToBoolean(String value) {
    return Boolean.valueOf(value);
}

public static Long stringToLong(String value) {
    return Long.parseLong(value);
}

public static String longToString(Long value) {
    return String.valueOf(value);
}

public static Integer objectToInteger(Object value){
    return (Integer)value;
}

public static String objectToString(Object value){
    return (String)value;
}

public static Long objectToLong(Object value){
    return (Long)value;
}

public static Long doubleToLong(Double value) {
    return value.longValue();
}

public static Double longToDouble(Long value) {
    return value.doubleValue();
}

public static Integer doubleToInteger(Double value) {
    return value.intValue();
}

public static Double integerToDouble(Integer value) {
    return value.doubleValue();
}

public static String doubleToString(Double value) {
    return String.valueOf(value);
}

public static Double stringToDouble(String value) {
    return Double.parseDouble(value);
}

}

int count = 0;
        if(matchObject.size()>100){
            int pageSize = 98;
            int pageSum=(matchObject.size()-1)/pageSize+1;
            for(int i=0;i<pageSum;i++){
                int start = i*pageSize;
                int end = (i+1)*pageSize;
                if(end>matchObject.size()){
                    end = matchObject.size();
                }
                //subList 前闭后开
                count += mappingDAO.batchInsertPackage(matchObject.subList(start,end));
            }
            //count += mappingDAO.insert(matchObject.get(matchObject.size()));
        }else{
            return mappingDAO.batchInsertPackage(matchObject);
        }
        return count;

public class DocumentUtil {

private static final Logger log = LoggerFactory.getLogger(DocumentUtil.class);

/**
 * 对象转换成document
 * @param obj 对象
 * @param clazz 对象所属类
 * @return document
 */
public static Document buildDocument(Object obj, Class clazz) {
    Document document = new Document();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        Object value = null;
        try {
            if (field.getType().isAssignableFrom(List.class) ||
                    field.getType().isAssignableFrom(Collection.class) ||
                    field.getType().isAssignableFrom(Map.class)) {
                value = JSON.toJSONString(field.get(obj));
            } else {
                value = field.get(obj);
            }
        } catch (IllegalAccessException e) {
            log.error(e);
        }

        if (value != null) {
            document.append(field.getName(), value);
        }
    }
    return document;
}

/**
 * document转换对象
 * @param document document
 * @param clazz 转换对象类
 * @param <T> 泛型对象
 * @return 对象
 */
public static <T> T convertToObj(Document document, Class<T> clazz) {
    Field[] fields = clazz.getDeclaredFields();
    T t = null;
    try {
        t = clazz.newInstance();
    } catch (Exception e) {
        log.error(e);
    }
    for (Field field : fields) {
        try {
            if (field.getType().isAssignableFrom(Integer.class)) {
                field.setAccessible(true);
                field.setInt(t, document.getInteger(field.getName()));
            } else if (field.getType().isAssignableFrom(Float.class)) {
                field.setAccessible(true);
                field.setFloat(t, document.getDouble(field.getName()).floatValue());
            } else if (field.getType().isAssignableFrom(Double.class)) {
                field.setAccessible(true);
                field.setDouble(t, document.getDouble(field.getName()));
            } else if (field.getType().isAssignableFrom(Long.class)) {
                field.setAccessible(true);
                field.setLong(t, document.getLong(field.getName()));
            } else if (field.getType().isAssignableFrom(String.class)) {
                field.setAccessible(true);
                field.set(t, document.getString(field.getName()));
            } else if (field.getType().isAssignableFrom(List.class) ||
                    field.getType().isAssignableFrom(Collection.class)) {
                field.setAccessible(true);
                field.set(t, JSON.parseArray(document.getString(field.getName())));
            } else if (field.getType().isAssignableFrom(Map.class)) {
                field.setAccessible(true);
                field.set(t, JSON.parseObject(document.getString(field.getName())));
            } else {
                field.setAccessible(true);
                field.set(t, JSON.parseObject(document.getString(field.getName()), field.getType()));
            }
        } catch (Exception e) {
            log.error(e);
        }
    }

    return t;
}

}

@Component
public class MongodbCustomClient {
    private String mongoAddress = SystemConfig.getProperty("mongo.address", "192.168.202.91:27017");

    private int mongoConnectionsPerHost = 100;

    private int mongoThreadsAllowedToBlockForConnectionMultiplier = 5;

    private int mongoConnectionTimeout = 60 * 1000;

    private int mongoMaxWaitTime = 120 * 1000;

    private boolean mongoSocketKeepAlive = true;

    private int mongoSocketTimeout = 0;

    private String auth = SystemConfig.getProperty("mongo.auth", "");

    private String dbName = SystemConfig.getProperty("mongo.dbname", "featureLib");

    private MongoClient mongoClient;


    @PostConstruct
    public void init() {
        MongoClientOptions options = MongoClientOptions.builder()
                .threadsAllowedToBlockForConnectionMultiplier(mongoThreadsAllowedToBlockForConnectionMultiplier)
                .connectTimeout(mongoConnectionTimeout)
                .connectionsPerHost(mongoConnectionsPerHost)
                .maxWaitTime(mongoMaxWaitTime)
                .socketKeepAlive(mongoSocketKeepAlive)
                .writeConcern(WriteConcern.SAFE)
                .socketTimeout(mongoSocketTimeout).build();

        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        String[] auths = auth.split(",");
        for (String a : auths) {
            if (a.equals("")) continue;
            String[] up = a.split(":");
            credentials.add(MongoCredential.createCredential(up[0], dbName, up[1].toCharArray()));
        }

        String[] servers = mongoAddress.split(",");
        List<ServerAddress> addresses = new ArrayList<ServerAddress>();
        for (String server : servers) {
            String[] hp = server.split(":");
            addresses.add(new ServerAddress(hp[0], Integer.valueOf(hp[1])));
        }

        if (credentials.size() > 0) {
            mongoClient = new MongoClient(addresses, credentials, options);
        } else {
            mongoClient = new MongoClient(addresses, options);
        }
    }

    public MongoDatabase getDataBase() {
        return mongoClient.getDatabase(dbName);
    }

    public MongoCollection<Document> getCollection(String collection) {
        return getDataBase().getCollection(collection);
    }

}