package com.house365.test;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestList {
    public static Map<String, String> toMap(Object javaBean) {
        Map<String, String> result = new LinkedHashMap<String, String>();
        Method[] methods = javaBean.getClass().getDeclaredMethods();

        for (Method method : methods) {
            try {
                if (method.getName().startsWith("get")) {
                    String field = method.getName();
                    field = field.substring(field.indexOf("get") + 3);
                    field = field.toLowerCase().charAt(0) + field.substring(1);

                    Object value = method.invoke(javaBean, (Object[]) null);
                    result.put(field, null == value ? "" : value.toString());
                }
            } catch (Exception e) {
            }
        }

        return result;
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static void main(String[] args) {



        //        Info info1 = new Info();
        //        info1.setCode(1000);
        //        info1.setTitle("测试");
        //        info1.setDescription("描述");
        //
        //        Info info2 = new Info();
        //        info2.setCode(1001);
        //        info2.setTitle("测试1");
        //        info2.setDescription("描述1");
        //
        //        List<Object> list = new ArrayList<Object>();
        //        list.add(info1);
        //        System.out.println(list);
        //        Collections.fill(list, info2);
        //        System.out.println(list);
        //
        //        System.out.println(toMap(info1));


        //        String str = "com.house365.beans.entity.AppdistBusinessPortEntity@5097de1d[\n" + "  Id=24\n" + "  Name=6\n" + "  Description=6\n" + "  Status=true\n" + "  CreatorId=1\n" + "  CreateTime=Mon Feb 15 11:22:17 CST 2016\n" + "]";
        //        str = replaceBlank(str);
        //        Pattern pattern = Pattern.compile("\\[(.*?)\\]");
        //        Matcher matcher = pattern.matcher(str);
        //        while(matcher.find()){
        //            System.out.println(matcher.group(1));
        //        }

//        StringBuilder stringBuilder = new StringBuilder(",");
//        for (int i = 1; i <= 2; i++) {
//            stringBuilder.append(i).append(",");
//        }
//        stringBuilder.append(",");
//
//        List<String[]> result = splitArrayWithSize(stringBuilder.toString(), 10);
//        for (String[] i : result) {
//            for (String j : i) {
//                System.out.print(j);
//            }
//            System.out.println("");
//        }


        List<String> list1 = Lists.newArrayList("1","2","3","4","5","6","7","8","9","10","11");
        List<String> list2 = Lists.newArrayList("1","2","3","4","5","6","7");
        List<String> list3 = Lists.newArrayList("4","5","6");
//        System.out.println(list1.removeAll(list3));
//        System.out.println(list1.removeAll(list2));
        list3.retainAll(list1);
        System.out.println(Joiner.on(",").join(list3));
    }

    private static List<String[]> splitArrayWithSize(String cityTags, int splitSize) {
        List<String[]> splitArray = new ArrayList<>();
        if (!Strings.isNullOrEmpty(cityTags)) {
            if (cityTags.startsWith(",") && cityTags.length() > 1) {
                cityTags = cityTags.substring(1, cityTags.length());
            }
            if (cityTags.endsWith(",")) {
                cityTags = cityTags.substring(0, cityTags.length() - 1);
            }
        }

        int parts ;
        String[] cities = cityTags.split(",");
        if (cities.length > splitSize) {
            parts = cities.length / splitSize;
        } else {
            parts = 0;
        }

        //拆分
        if (parts >= 0) {
            for (int i = 0; i <= parts; i++) {

                String[] j = Arrays.copyOfRange(cities, i * splitSize,
                                                ((i + 1) * splitSize) >= cities.length ? cities.length : (i + 1) * splitSize);

                splitArray.add(j);

            }
        }

        return splitArray;
    }

}
